码迷,mamicode.com
首页 > 编程语言 > 详细

SpringMVC入门笔记一

时间:2016-12-19 22:03:32      阅读:401      评论:0      收藏:0      [点我收藏+]

标签:rate   exception   pat   ota   读取   ade   port   handler   cccccc   

 
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="org.springframework.web.servlet.view.JstlView"/>
            <property name="prefix" value="/WEB-INF/jsp/"/>
            <property name="suffix" value=".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入门笔记一

标签:rate   exception   pat   ota   读取   ade   port   handler   cccccc   

原文地址:http://www.cnblogs.com/J-wym/p/6200561.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!