第1节 映射请求
RequestMapping
1)此注解可以写在类级别也可以写在方法级别,最后路径是类加径+方法级别路径
@RequestMapping("/user")
public class TestController {
@RequestMapping("/login.action")
public ModelAndView test(){
ModelAndView mav = new ModelAndView();
mav.setViewName("login");
return mav;
}
比如上述代码,它映射的路径就是http://ip:port/contextpath/user/login.action
2)路径的写法
@RequestMapping("/user") ------>支持所有的请求方式
如下的写法会导致该方法只支持post请求,如果通过get方式发请求时就会报405异常
HTTP Status 405 - Request method 'GET' not supported
@RequestMapping(value="reg.action",method={RequestMethod.POST})
public ModelAndView reg(){
ModelAndView mav = new ModelAndView();
mav.setViewName("hello.jsp");
return mav;
}
方法返回值
方法返回值种类
1)ModelAndView
设置视图名字
//第一种写法
ModelAndView mav = new ModelAndView();
mav.setViewName("hello");
//第二种写法 有参构造方法里传视图名
ModelAndView mav = new ModelAndView("hello");
设置model
mav.addObject("result", "success");
mav.addObject("msg", "成功");
hello.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" isELIgnored="false"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
${result}
<h2>Hello SpringMVC</h2>
</body>
</html>
问题解决:el表达式不能正确显示的问题(用了老旧的DTD)
解决方案一:在每个jsp页里,设置isELIgnored="false"
解决方案二:web.xml头改为如下(建议使用这种方法,改一次即可,原来jsp不用动)
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
第二节
2)String(三种)
2.1字符串(视图名)
return "hello" 返回视图名。参数通过单独model推荐回去
样例代码
@Controller
public class TestReturnController {
@RequestMapping("/ret1")
public String ret1(Model m){
m.addAttribute("msg", "字符串测试");
return "hello";
}
}
测试地址:
http://localhost:8081/ch01-springmvc01/ret1.action
2.2请求转发
@RequestMapping("/ret2")
public String ret2(Model m){
m.addAttribute("msg", "字符串测试");
return "forward:/user/login.action";
}
请求转发带参数
/**
* 返回类型为String
* forward:string 请求转发
* @return
*/
@RequestMapping("/testforward.action")
public String testforward(){
System.out.println("TestController的test方法被调用");
return "forward:/user/add.action?username=wangqj&password=123456";
}
2.3重定向(了解)
注意:带参数RedirectAttributes 和@ModelAttribute("msg")
@RequestMapping("/ret3.action")
public String ret3(RedirectAttributes reattr){
reattr.addAttribute("username", "ggg");
reattr.addAttribute("password", "123456");
return "redirect:/user/add.action";
}
@RequestMapping(value="/login.action",method={RequestMethod.POST,RequestMethod.GET})
public ModelAndView login(String username,String password){
System.out.println(username);
System.out.println(password);
ModelAndView mv=new ModelAndView();
mv.addObject("msg", "成功");
mv.setViewName("hello");
return mv;
}
或者:
@Controller
@RequestMapping("/user")
public class TestController {
@RequestMapping("/login.action")
public ModelAndView test(@ModelAttribute("username") String username,@ModelAttribute("password") String password){
ModelAndView mav = new ModelAndView();
mav.addObject("msg", message);
mav.setViewName("login");
return mav;
}
。。。。。。
}
第三节
void
为了用传统方式返回
返回值为void时,通过原始的request,repsponse操作数据。实现转发和重定向
@RequestMapping("/ret4")
public void ret4(HttpServletRequest req,HttpServletResponse res){
req.setAttribute("msg", "sssss");
try {
req.getRequestDispatcher("/user/login.action").forward(req,res);
} catch (ServletException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
网友评论