首页 > 代码库 > SpringMVC入门笔记一

SpringMVC入门笔记一

 
SpringMVC优势
    性能比struts2好
    简单 便捷 易学
    和Spring无缝集成(使用spring ioc aop)
    约定优于配置
    能够简单进行Junit测试
    支持Restful风格
    本地化、国际化、数据验证、类型转换
    拦截器
    
   
1、导入包
  技术分享技术分享

2、配置springMVC
   <!--1.配置springMVC前端控制器(是总的控制中心  被拦截的url会汇聚到该servlet) --> 
<servlet> 
        <servlet-name>springmvc</servlet-name>   
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <init-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>classpath:springmvc-servlet.xml</param-value>
      </init-param>
        <load-on-startup>1</load-on-startup>  <!-- tomcat启动后立即加载 -->     
   </servlet> 
    <servlet-mapping> 
        <servlet-name>springmvc</servlet-name> 
        <url-pattern>*.do</url-pattern> 
    </servlet-mapping>    
     
   <!--2.配置handler Mapping-->
    <bean class=" org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
 
 
   <!--3.配置handler Adapter-->
    <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
    
  
   <!--4.配置渲染器-->
    <bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="viewClass" value="http://www.mamicode.com/org.springframework.web.servlet.view.JstlView"/>
            <property name="prefix" value="http://www.mamicode.com/WEB-INF/jsp/"/>
            <property name="suffix" value="http://www.mamicode.com/.jsp"/>
    </bean>
   
   <!--5.配置请求处理器-->
  <bean name="/hello.do" class="wym.controler.FirstSpringMvc"></bean>
  

 


web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
  <display-name></display-name>    
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  
   <!--1.配置springMVC前端控制器(是总的控制中心  被拦截的url会汇聚到该servlet) -->  
    <servlet>  
        <servlet-name>springmvc</servlet-name>    
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
     <init-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>classpath:springmvc-servlet.xml</param-value>
      </init-param>
        <load-on-startup>1</load-on-startup>  <!-- tomcat启动后立即加载 -->      
   </servlet>  
      
      
      <!--配置Spring -->
      <context-param> 
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:springmvc-servlet.xml</param-value>
      </context-param>

      
    <!--2. 配置spring拦截的url模板  以.do结尾的url-->  
    <servlet-mapping>  
        <servlet-name>springmvc</servlet-name>  
        <url-pattern>*.do</url-pattern>  
    </servlet-mapping>  
      
      
    <!--3. 注册配置文件读取器,监听spring配置文件的变化 -->  
    <listener>  
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
    </listener>   
   
  <!-- 指定spring配置文件位置 -->
  <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath:springmvc-servlet.xml</param-value>
  </context-param>
     
    
    <!--5. 编码字符集统一为UTF-8(过滤器) -->  
    <filter>  
        <display-name>encodingFilter</display-name>  
        <filter-name>encodingFilter</filter-name>  
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
        <init-param>  
            <param-name>encoding</param-name>  
            <param-value>UTF-8</param-value>  
        </init-param>  
    </filter>  
    
</web-app>

 

 

springmvc-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>  
    <beans xmlns="http://www.springframework.org/schema/beans"  
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"  
        xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"  
        xmlns:util="http://www.springframework.org/schema/util"  
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd              
        http://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context.xsd               
          http://www.springframework.org/schema/mvc  http://www.springframework.org/schema/mvc/spring-mvc.xsd                   
        http://www.springframework.org/schema/util  http://www.springframework.org/schema/util/spring-util.xsd">  
        
     
    <bean class=" org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
    <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
           
 <bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
    <property name="prefix" value="/WEB-INF/jsp/"/>
    <property name="suffix" value=".jsp"/>
</bean>
   
   
   <mvc:annotation-driven/>
   <context:component-scan base-package="wym.controler"/>
   <bean name="/hello.do" class="wym.controler.FirstSpringMvc"></bean>
   
   
   
    </beans>

 

 

FirstSpringMvc.java

package wym.controler;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

public class FirstSpringMvc implements Controller
{

    @Override
    public ModelAndView handleRequest(HttpServletRequest arg0,
            HttpServletResponse arg1) throws Exception {
        // TODO Auto-generated method stub
        
        ModelAndView mv=new ModelAndView();
        
        mv.addObject("msg","你好。。 springmvc");///WEB-INF/jsp/hello.jsp
        mv.setViewName("hellow");
        return mv;
    }

}

SpringMVC入门笔记一