使用HttpServletRequest对象获取参数
@RequestMapping("/test01")
public String test01(HttpServletRequest request){
System.out.println(request.getParameter("name"));
System.out.println(request.getParameter("age"));
return "hello";
}
请求参数和方法的形参同名即可
@RequestMapping("/test02")
public String test02(String name,int age){
System.out.println(name);
System.out.println(age);
return "hello";
}
重点
】请求参数和实体的属性 同名即可
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
private String name;
private Integer age;
private Double money;
}
@RequestMapping("/test03")
public String test03(User user){
System.out.println(user);
return "hello";
}
<form action="/springmvc02__war_exploded/test03" method="post">
用户名:<input name="name" type="text"><br>
年龄:<input name="age" type="text"><br>
金额:<input name="money" type="text"><br>
<input name="hobby" type="checkbox" value="lq">打篮球<br>
<input type="submit" value="提交">
form>
在一个类中包含另一个类的对象
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
private String name;
private Integer age;
private Double money;
private Product product;
}
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Product {
private String name;
private Double price;
}
前端传参
后端收值
@RequestMapping("/test03")
public String test03(User user){
System.out.println(user);
return "hello";
}
前端传参
<form>
.....
爱好:
<input name="hobby" type="checkbox" value="song">唱
<input name="hobby" type="checkbox" value="tiao">跳
<input name="hobby" type="checkbox" value="rap">rap
<input name="hobby" type="checkbox" value="lq">打篮球<br>
.....
form>
后端收值
@RequestMapping("/test04")
public String test04(String[] hobby){
System.out.println(Arrays.toString(hobby));
return "hello";
}
定义自定义转换类
@Component
public class DateConverter implements Converter<String, Date> {
@Override
public Date convert(String s) {
if(s != null){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
Date date = sdf.parse(s);
return date;
} catch (ParseException e) {
e.printStackTrace();
//这里可以指定其他情况
}
}
return null;
}
}
配置自定义类型转换器
<bean id="myConverter" class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<set>
<ref bean="dateConverter">ref>
set>
property>
bean>
配置SpringMVC使用自定义类型转换器
<mvc:annotation-driven conversion-service="myConverter"/>
在类的属性中或者方法形参上直接添加注解
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
private String username;
private String password;
private Integer age;
private String[] hobby;
private String sex;
@DateTimeFormat(pattern = "yyyy-MM-dd")
private Date birthday;
private Book book;
}
在web.xml文件中配置编码过滤器
<filter>
<filter-name>encodingFilterfilter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
<init-param>
<param-name>encodingparam-name>
<param-value>utf-8param-value>
init-param>
filter>
<filter-mapping>
<filter-name>encodingFilterfilter-name>
<url-pattern>/*url-pattern>
filter-mapping>
Controller中方法的返回值
- void (利用原生的ServletAPI实现跳转)
- String (利用视图解析器实现跳转、利用两个关键字实现页面跳转)
- ModelAndView (利用视图解析器实现跳转)
通过原生ServletApi的方式实现跳转,未来少用但是不能忘记
@RequestMapping("test01")
public void test01(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//转发
//request.getRequestDispatcher("/pages/hello.jsp").forward(request,response);
//重定向
response.sendRedirect(request.getContextPath()+"/pages/hello.jsp");
}
通过String视图解析器实现跳转
前后端分离常用
@RequestMapping("test02")
public String test02(){
return "hello"; // 视图解析器(转发)
}
通过两个关键字实现跳转
不走视图解析器
- forward 转发
- redirect 重定向
@RequestMapping("test03")
public String test03(){
//return "forward:/pages/hello.jsp"; //不走视图解析器 转发
return "redirect:/pages/hello.jsp"; //不走视图解析器 重定向
}
通过ModelAndView实现跳转
@RequestMapping("test04")
public ModelAndView test04(){
ModelAndView modelAndView = new ModelAndView();
//设置视图
modelAndView.setViewName("hello");//走视图解析器 转发
//设置数据模型
//modelAndView.addObject(key,value);
return modelAndView;
}
Controller得到数据后,跳转到View,并向View传递数据。
进而View中可以渲染数据,让用户看到含有数据的页面
Servlet中的三大域对象
- request域
- session域
- application域
@RequestMapping("/h1")
public String test01(HttpServletRequest request){
request.setAttribute("requestMsg","request对象 携带的shuju...");
request.getSession().setAttribute("sessionMsg","session对象");
request.getServletContext().setAttribute("applicationMsg","application对象");
return "hello";
}
相当与request域
- 通过Model携带数据(相当于request)
@RequestMapping("h2")
public String test02(Model model){
model.addAttribute("msg","Model域对象存储的数据");
return "hello";
}
相当与request域
- 通过ModelAndView携带数据(相当于request)
@RequestMapping("h3")
public ModelAndView test03(){
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("hello");
modelAndView.addObject("modelandview","ModelAndView存储的数据...");
return modelAndView;
}
将model中的指定数据保存到Session中.可以实现共享
//将保存在model中属性值,保存到session域中,而且在其他的地方可以通过model可以获取。实现共享
@SessionAttributes({"name","age"})
public class TestController {
//保存数据
@RequestMapping("/test04")
public String test04(String name,int age,Model model){
model.addAttribute("name",name);
model.addAttribute("age",age);
return "hello";
}
@RequestMapping("/test02")
public String test02(Model model){
System.out.println(model.getAttribute("name"));
return "hello";
}
}
主要通过EL表达式和JSTL
${requestMsg}-----> ${sessionMsg}-----> ${applicationMsg}
${requestScope.model}----->${sessionScope.model}----->${applicationScope.model}
${requestScope.modelandview}----->${sessionScope.modelandview}----->${applicationScope.modelandview}
${requestScope.name}----->${sessionScope.name}----->${applicationScope.name}