各种参数传递方式
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("test")
public class TestParam {
@RequestMapping("/testParam1.do")
public ModelAndView testParam() {
RedirectView view = new RedirectView("testParam.do");
return new ModelAndView(view);
}
@RequestMapping("/testParam.do")
public String testParam() {
return "error";
return "redirct:testParamX.do";
}
@RequestMapping("/testParam.do")
public ModelAndView testParam(@ModelAttribute("username")String username) {
System.out.println(username);
return new ModelAndView("hello");
}
@RequestMapping("/testParam.do")
public ModelAndView testParam(ModelMap map) {
map.addAttribute("success",false);
map.addAttribute("message","操作失败");
return new ModelAndView("hello");
}
@RequestMapping("/testParam.do")
public ModelAndView testParam() {
Map<String, Object> data=new HashMap<>();
data.put("success",true);
data.put("message", "操作成功");
return new ModelAndView("hello",data);
}
@RequestMapping("/testParam.do")
public ModelAndView testParam(User user) {
System.out.println(user.getUsername()+" "+user.getPassword());
return new ModelAndView("hello");
}
@RequestMapping("testParam.do")
public ModelAndView testParam(@RequestParam("username")String
name,@RequestParam("password")String pwd) {
System.out.println(name+" "+pwd);
return new ModelAndView("hello");
}
@RequestMapping("testParam.do")
public ModelAndView testParam(
HttpServletRequest request) {
try {
request.setCharacterEncoding("utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
String username=request.getParameter("username");
String password=request.getParameter("password");
System.out.println(username+" "+password);
return new ModelAndView("hello");
}
}
HTML
<%@ page contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<h1>表单</h1>
<form action="${pageContext.request.contextPath }/test/testParam.do"
method="post">
账号:<input type="text" name="username"/>
密码:<input type="password" name="password"/>
<input type="submit" value="提交"/>
</form>
</body>
</html>
HTML
<%@ page contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<h1>2020,您好</h1>
<h1>是否成功:${success}</h1>
<h1>提示信息:${message}</h1>
<h1>姓名:${username }</h1>
</body>
</html>