SpringMVC注解及实现页面跳转详解

Spring MVC 

第一讲:SpringMVC 页面跳转实例,通过请求访问start.jsp页面 

1.1 加包 

1.2 web.xml 

<servlet> 

          <servlet-name>springMVC</servlet-name> 

          <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 

          <load-on-startup>1</load-on-startup> 

      </servlet> 

      <servlet-mapping> 

          <servlet-name>springMVC</servlet-name> 

          <url-pattern>*.do</url-pattern> 

      </servlet-mapping> 

1.3 建springMVC-servlet.xml(注:spring默认要求建立在WEB-INF目录下,并且名称为servlet-name_servlet.xml 

    
<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:context="http://www.springframework.org/schema/context" 
        xsi:schemaLocation=" 
    http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 
        <context:component-scan base-package="com.wdl.cn.controllers" /> 
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
            <property name="prefix" value="/WEB-INF/page/"/> 
            <property name="suffix" value=".jsp"/> 
        </bean> 
    </beans> 


1.4 StartController.java 

package com.wdl.cn.controllers; 

    import org.springframework.stereotype.Controller; 
    import org.springframework.web.bind.annotation.RequestMapping; 

    @Controller 
    public class StartController { 

        @RequestMapping(value="/start") 
        public String start(){ 
            System.out.println("start method invoked..."); 
            return "start"; 
        } 
    } 



1.5 新建 WEB-INF/page/start.jsp 

1.6 访问:http://localhost:8080/springMVC_001/start.do 
总结:本例子搭建一个简单的spingmvc实例,通过本例学习springMVC最简单的流程及搭建元素。 
第二讲 灵活指定springmvc映射文件及路径 
    更多情况下,对于第一讲中的springMVC-servlet.xml 文件,我们并不想去放在WEB-INF,或者说我们可能在一个项目中并不是只有这一个文件,所以本节我们来讲述如何灵活配置此文件及文件名。 
2.1 修改web.xml 

<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*:/com/wdl/cn/config/mvc/*.xml</param-value> 
          </init-param> 
          <load-on-startup>1</load-on-startup> 
      </servlet> 
      <servlet-mapping> 
          <servlet-name>springMVC</servlet-name> 
          <url-pattern>*.do</url-pattern> 
      </servlet-mapping> 

2.2 将WEB-INF下面的springMVC-servlet.xml 移动到com.wdl.cn.config.mvc包下并将其修改名称为springMVC-first.xml 

2.3 测试原来的路径 : http://localhost:8080/springMVC_002/start.do 

总结:本讲主要学习了如何灵活配置springMVC配置文件。下节将重点讲述@RequestMapping的使用。 

第三讲 springMVC_003  @RequestMapping的使用 

3.1 @RequestMapping 使用位置 
    a.使用于方法,此方法则可以被请求访问; 
    b.使用于类,则可以将请求分(又叫分路径,分模块)包处理 
3.2 将controller类上加@RequestMapping,修改StartController.java 

package com.wdl.cn.controllers; 

    import org.springframework.stereotype.Controller; 
    import org.springframework.web.bind.annotation.RequestMapping; 

    @Controller 
    @RequestMapping(value="/test") 
    public class StartController { 

        @RequestMapping(value="/start") 
        public String start(){ 
            System.out.println("start method invoked..."); 
            return "start"; 
        } 
    } 


3.3 访问:http://localhost:8080/springMVC_003/test/start.do ,由此可见,可以通过在类上加@RequestMapping将请求分路径。 

3.4 @RequestMapping 参数详解 
    a.value表示访问的路径 
    b.method 表示请求的方式 GET | POST 
    step1:修改StartController.java 

 import org.springframework.stereotype.Controller; 
    import org.springframework.web.bind.annotation.RequestMapping; 
    import org.springframework.web.bind.annotation.RequestMethod; 

    @Controller 
    @RequestMapping(value="/test") 
    public class StartController { 

        @RequestMapping(value="/start",method=RequestMethod.GET) 
        public String start(){ 
            System.out.println("start method invoked..."); 
            return "start"; 
        } 
        @RequestMapping(value="/start",method=RequestMethod.POST) 
        public String start2(){ 
            System.out.println("start2 method invoked..."); 
            return "start"; 
        } 
    }          

step2:修改start.jsp模拟POST请求 
3.5 访问http://localhost:8080/springMVC_003/test/start.do,接着转到start.jsp页面,接着点击页面的中按钮,发送POST请求,则可以看出不同的请求方式可以调用不同的方法。 
3.6 参数传递(常用参数传递,restful风格参数传递) 

      a.httpservletreqeust方式参数传递 
        step1:修改StartController.java 
      
package com.wdl.cn.controllers; 

    import javax.servlet.http.HttpServletRequest; 

    import org.springframework.stereotype.Controller; 
    import org.springframework.web.bind.annotation.RequestMapping; 
    import org.springframework.web.bind.annotation.RequestMethod; 

    @Controller 
    @RequestMapping(value="/test") 
    public class StartController { 

        @RequestMapping(value="/start",method=RequestMethod.GET) 
        public String start(HttpServletRequest request){ 
            System.out.println("start method invoked..."); 
            String name = request.getParameter("name"); 
            int age = Integer.parseInt(request.getParameter("age")); 
            System.out.println("name=" + name + " age = " + age); 
            return "start"; 
        } 
        @RequestMapping(value="/start",method=RequestMethod.POST) 
        public String start2(){ 
            System.out.println("start2 method invoked..."); 
            return "start"; 
        } 
    } 

    step2:访问:http://localhost:8080/springMVC_003/test/start.do?name=zhangsan&age=45 

      b.restfull风格参数传递 
        step1:修改StartController.java 
 
package com.wdl.cn.controllers; 

    import org.springframework.stereotype.Controller; 
    import org.springframework.web.bind.annotation.PathVariable; 
    import org.springframework.web.bind.annotation.RequestMapping; 
    import org.springframework.web.bind.annotation.RequestMethod; 

    @Controller 
    @RequestMapping(value="/test") 
    public class StartController { 

        @RequestMapping(value="/start/{name}/{age}",method=RequestMethod.GET) 
        public String start(@PathVariable("name") String name,@PathVariable("age") int age){ 
            System.out.println("start method invoked..."); 
            System.out.println("name="+name+" age="+age); 
            return "start"; 
        } 
        @RequestMapping(value="/start",method=RequestMethod.POST) 
        public String start2(){ 
            System.out.println("start2 method invoked..."); 
            return "start"; 
        } 
    } 

    step2:访问时,将参数值加入请求路径 http://localhost:8080/springMVC_003/test/start/zhangsan/45.do 
总结:本讲主要讲述请求参数传递,主要掌握HttpServletRequest方式与restfull风格参数传递,下讲主要讲述form表单提交 

第四讲 form表单提交 
    使用struts2后,我们习惯将form表单的数据封装成一个对象,这也是面向对象编程的一个思维,本节讲述form表单提交。 
    具体代码见UserController.java 
 
 package com.wdl.cn.controllers; 

    import java.text.SimpleDateFormat; 
    import java.util.Date; 

    import javax.servlet.http.HttpSession; 

    import org.springframework.beans.propertyeditors.CustomDateEditor; 
    import org.springframework.stereotype.Controller; 
    import org.springframework.web.bind.WebDataBinder; 
    import org.springframework.web.bind.annotation.InitBinder; 
    import org.springframework.web.bind.annotation.RequestMapping; 
    import org.springframework.web.bind.annotation.RequestMethod; 
    import org.springframework.web.servlet.ModelAndView; 

    import com.wdl.cn.entity.User; 

    @Controller 
    @RequestMapping(value="/form") 
    public class UserController { 

        @RequestMapping(value="/add",method=RequestMethod.GET) 
        public String toUserAddPage(){ 
            System.out.println("toUserAddPage method invoked..."); 
            return "useradd"; 
        } 
       
        @RequestMapping(value="/add",method=RequestMethod.POST) 
        public ModelAndView addUser(HttpSession session,User user){ 
            System.out.println("addUser method invoked..."); 
            System.out.println(user); 
            ModelAndView mav = new ModelAndView(); 
            session.setAttribute("abc", user); 
            mav.setViewName("redirect:/form/add.do"); 
            mav.addObject("abc", user); 
            //return "useradd"; 
            return mav; 
        } 
       
        @InitBinder 
        public void initBinder(WebDataBinder binder){ 
            binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), false)); 
        } 
    } 

     注:addUser方法中参数User则是接收请求发过来的参数;initBinder方法则是用来绑定用户提交的数据,前台提交过来的日期,在这里后面并不知情,则由这个方法来处理。 
总结:本讲主要讲述如何来提交表单数据,并将其封装成一个对象。 

第五节 一个Controller处理多个请求 
      很多情况下,我们都想把有关联的请求放在一个controller中,比如,对于一个用户的增删改查,都可以放在一个controller类中。对于springMVC注解的方式有两种方式可以实现。 
    1.一个controller类中每个处理请求的方法的@requestMapping的param中可以带参数,比如:@RequestMapping(param="method=method1"),在类级别的@RequestMapping上面指定请求路径。 
package com.wdl.cn.controllers; 

    import org.springframework.stereotype.Controller; 
    import org.springframework.web.bind.annotation.RequestMapping; 
    import org.springframework.web.servlet.ModelAndView; 

    @Controller 
    @RequestMapping(value="/manyRequest") 
    public class ReceiveManyRequestController { 
        @RequestMapping(params="method=method1") 
        public ModelAndView methodOne(){ 
            System.out.println("method methodOne invoked..."); 
            ModelAndView mav = new ModelAndView("ReceiveManyRequest"); 
            mav.addObject("msg", "请求第一个方法"); 
            return mav; 
        } 
        @RequestMapping(params="method=method2") 
        public ModelAndView methodTwo(){ 
            System.out.println("method methodTwo invoked..."); 
            ModelAndView mav = new ModelAndView("ReceiveManyRequest"); 
            mav.addObject("msg", "请求第二个方法"); 
            return mav; 
        } 
       
    } 

      2.每个接收请求的方法的@RequestMapping的value中指定请求路径,看ReceveManyRequestController2.java 

    
package com.wdl.cn.controllers; 

    import org.springframework.stereotype.Controller; 
    import org.springframework.web.bind.annotation.RequestMapping; 
    import org.springframework.web.servlet.ModelAndView; 

    @Controller 
    @RequestMapping(value="/manyRequest") 
    public class ReceiveManyRequestController2 { 
        @RequestMapping(value="/request1") 
        public ModelAndView methodOne(){ 
            System.out.println("method methodOne invoked..."); 
            ModelAndView mav = new ModelAndView("ReceiveManyRequest"); 
            mav.addObject("msg", "请求第一个方法"); 
            return mav; 
        } 
        @RequestMapping(value="/request2") 
        public ModelAndView methodTwo(){ 
            System.out.println("method methodTwo invoked..."); 
            ModelAndView mav = new ModelAndView("ReceiveManyRequest"); 
            mav.addObject("msg", "请求第二个方法"); 
            return mav; 
        } 
    } 


总结:本讲讲述如何让一个Controller接收多个请求的两种处理方式
阅读更多
换一批

没有更多推荐了,返回首页