好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

基于spring mvc请求controller访问方式

spring mvc请求controller访问

1.一个Controller里含有不同的请求url

?

1

2

3

4

5

6

7

8

9

10

11

12

@Controller   //类似Struts的Action

public class TestController {

     @RequestMapping ( "test/login.do" )  // 请求url地址映射,类似Struts的action-mapping

     public String testLogin( @RequestParam (value= "username" )String username, String password, HttpServletRequest request) {

         // @RequestParam是指请求url地址映射中必须含有的参数(除非属性required=false)

         // @RequestParam可简写为:@RequestParam("username")

         if (! "admin" .equals(username) || ! "admin" .equals(password)) {

             return "loginError" ; // 跳转页面路径(默认为转发),该路径不需要包含spring-servlet配置文件中配置的前缀和后缀

         }

         return "loginSuccess" ;

     }

}

2.采用一个url访问

通过url参数来区分访问不同的方法

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

@Controller

@RequestMapping ( "/test2/login.do" )  // 指定唯一一个*.do请求关联到该Controller

public class TestController2 {

         @RequestMapping

     public String testLogin(String username, String password, int age) {

         // 如果不加任何参数,则在请求/test2/login.do时,便默认执行该方法

        

         if (! "admin" .equals(username) || ! "admin" .equals(password) || age < 5 ) {

             return "loginError" ;

         }

         return "loginSuccess" ;

     }

     @RequestMapping (params = "method=1" , method=RequestMethod.POST)

     public String testLogin2(String username, String password) {

         // 依据params的参数method的值来区分不同的调用方法

         // 可以指定页面请求方式的类型,默认为get请求

        

         if (! "admin" .equals(username) || ! "admin" .equals(password)) {

             return "loginError" ;

         }

         return "loginSuccess" ;

     }

    

     @RequestMapping (params = "method=2" )

     public String testLogin3(String username, String password, int age) {

         if (! "admin" .equals(username) || ! "admin" .equals(password) || age < 5 ) {

             return "loginError" ;

         }

         return "loginSuccess" ;

     }

}

3.RequestMapping在Class上

可看做是父Request请求url,而RequestMapping在方法上的可看做是子Request请求url,父子请求url最终会拼起来与页面请求url进行匹配

?

1

2

3

4

5

6

7

8

9

10

11

@Controller

@RequestMapping ( "/test3/*" )  // 父request请求url

public class TestController3 {

     @RequestMapping ( "login.do" )  // 子request请求url,拼接后等价于/test3/login.do

     public String testLogin(String username, String password, int age) {

         if (! "admin" .equals(username) || ! "admin" .equals(password) || age < 5 ) {

             return "loginError" ;

         }

         return "loginSuccess" ;

     }

}

4.在SpringMVC中常用的注解

还有@PathVariable,@RequestParam,@PathVariable标记在方法的参数上,利用它标记的参数可以利用请求路径传值

?

1

2

3

4

5

6

@Controller   //类似Struts的Action

public class TestController {

  @RequestMapping (value= "/comment/{blogId}" , method=RequestMethod.POST)

  public void comment(Comment comment, @PathVariable int blogId) throws IOException {   

  }

}

springmvc请求一次,访问多个controller方法

有一个需求:请求一次,访问多个controller中的方法

比如:先执行查询操作,再将查询出来的内容更新(当然也可以将方法写到bo中,在controller中直接调用bo的方法,这里只是举个例子)

举例

JSP页面

?

1

2

3

4

5

6

7

8

9

10

<%@ page contentType="text/html;charset=UTF-8" language="java" %>

< html >

< head >

< title >在一个action中执行两个方法</ title >

</ head >

< body >

1 哈哈 7000

< a href = "${pageContext.request.contextPath}/emp/find?id=1" rel = "external nofollow"   style = "text-decoration:none" >编辑</ a >

</ body >

</ html >

Controller页面

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

@Controller

@RequestMapping ( "/emp" )

public class EmpAction {

     @RequestMapping (value= "/find" )

     public String findEmpById( int id) throws Exception{

         System.out.println( "查询" +id+ "号员工信息" );

 

         //转发到EmpAction的另一个方法中去,即再次发送请求

//        return "forward:/emp/update";

 

         //重定向到EmpAction的另一个方法中去,即再次发送请求

         return "redirect:/emp/update.action?id=" + id;

     }

 

     @RequestMapping (value= "/update" )

     public String updateEmpById( int id,Model model) throws Exception{

         System.out.println( "更新" + id + "号员工信息" );

         model.addAttribute( "message" , "更新员工信息成功" );

         return "success" ;

     }

}

结论

1. ModelAndView并不能实现两个方法之间的数据传递;

2. 可以通过Session来进行传递。

有多种方法可以实现Session传递

方法1:将HttpServletRequest作为方法形参 通过request.getSession().addAttribute

方法2:将HttpSession作为方法形参

方法3:通过@SessionAttribute+@ModelAttribute来进行传递

使用HttpSession来存取数据,不过这样又在springmvc中使用了servlet的内容,并不好

3. 使用转发。在转发情况下,共享request域对象,会将参数从第一个业务控制方法传入第二个业务控制方法

?

1

return "forward:/emp/update.action" ;

4. 重定向不共享参数,所以要带参数才行

?

1

return "redirect:/emp/update.action?id=" + id;

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。

原文链接:https://blog.csdn.net/u011332918/article/details/50600958

查看更多关于基于spring mvc请求controller访问方式的详细内容...

  阅读:22次