页面存放位置问题
- WEB-INF下的页面是不可以通过URL来调用的,所以把需要验证才能加入的页面放到WEB-INF目录下
- WebRoot下是可以直接URL调用的
获取表单属性
@RequestParam 的 required属性:false允许为空 用于获取表单与pojo属性名称不一样的属性
俩种:表单属性名要和pojo属性名对应
- public ModelAndView getData(String name,String password)
- public String getData(Student student)
乱码:post请求+web配置过滤器解决中文乱码问题
<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
页面跳转
注意:ModelAndView引入的是 import org.springframework.web.servlet.ModelAndView;
去WEB-INF下
- 在springMVC配置了视图解析器
-
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/jsp/" p:suffix=".jsp"></bean>
所以在跳转的时候直接 modelAndView.setViewName("success"); 然后return就可以了
package com.qyck.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.qyc.pojo.Student;
@Controller
@RequestMapping("/show")
public class ShowStudent {
@RequestMapping("/student")
public ModelAndView showStudent(Student student) {
ModelAndView modelAndView = new ModelAndView();
System.out.println("跳转:"+student.toString());
modelAndView.setViewName("success");
return modelAndView;
}
}
不去WEB-INF目录下
- 在视图名前加上“forward:”(转发)或“redirect:”(重定向)
- 链接为http://localhost:8080/conformitySSM/index.jsp
重定向
package com.qyck.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.qyc.pojo.Student;
@Controller
@RequestMapping("/show")
public class ShowStudent {
@RequestMapping("/student")
public ModelAndView showStudent(Student student) {
ModelAndView modelAndView = new ModelAndView();
System.out.println("重定向:"+student.toString());
modelAndView.setViewName("redirect:/index.jsp");
return modelAndView;
}
}
跳转
package com.qyck.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.qyc.pojo.Student;
@Controller
@RequestMapping("/show")
public class ShowStudent {
@RequestMapping("/student")
public ModelAndView showStudent(Student student) {
ModelAndView modelAndView = new ModelAndView();
System.out.println("转发:"+student.toString());
modelAndView.setViewName("forward:/index.jsp");
return modelAndView;
}
}
-
我认为要是不经过视图解析器的话就没有new它的必要了,直接返回值为String, return "forward:/index.jsp";
重定向
- 方法返回值为字符串
- RedirectAttributes参数
- ra.addFlashAttribute相当于把student存入session,会话结束后,清楚session保存的数据
- return "redirect:/show/student.do";要是在同一/下可以直接 .student.do
package com.qyck.controller;
import java.io.UnsupportedEncodingException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import com.qyc.pojo.Student;
@Controller
@RequestMapping("/params")
public class ParamsController {
@RequestMapping("/getData")
public String getData(RedirectAttributes ra,Student student) {
ra.addFlashAttribute("student",student);
ModelAndView modelAndView = new ModelAndView();
return "redirect:/show/student.do";
}
}
@RequestAttribute
package com.qyck.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
@RequestMapping("/requestAttribute_zs")
public class RequestAttribute_zs {
@RequestMapping("/method")
public ModelAndView testRequestAttribute_zs(@RequestAttribute("name") String name) {
System.out.println("获取requssetAttribute"+name);
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("success");
return modelAndView;
}
}
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'RequestAttribute.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%
request.setAttribute("name", "菜鸡儿强月城");
request.getRequestDispatcher("/requestAttribute_zs/method.do").forward(request, response);
%>
</body>
</html>
@SessionAttributes 设置
package com.qyck.controller;
import java.io.UnsupportedEncodingException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;
import com.qyc.mapper.Mapper;
import com.qyc.pojo.Student;
import com.sun.mail.handlers.message_rfc822;
@Controller
@RequestMapping("/SessionAttributes_zs")
@SessionAttributes(names= {"name"},types= {Student.class})
public class SessionAttributes_zs {
@Autowired
private Mapper mapper;
@RequestMapping("/method")
public ModelAndView testSessionAttributes_zs(String name) {
byte bb[];
try {
bb = name.getBytes("ISO-8859-1"); //以"ISO-8859-1"方式解析name字符串
System.out.println(bb);
name= new String(bb, "UTF-8"); //再用"utf-8"格式表示name
System.out.println("-----------"+name);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Student student = mapper.selectName(name);
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("name",name);
modelAndView.addObject("student",student);
modelAndView.setViewName("SessionAttributes");
return modelAndView;
}
}
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'SessionAttributes.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%
out.println(session.getAttribute("name"));
out.println(session.getAttribute("student"));
%>
</body>
</html>
- URL传中文乱码问题我还没解决,等解决后会更新!
@SessionAttribute 取
package com.qyck.controller;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttribute;
import org.springframework.web.servlet.ModelAndView;
import com.qyc.pojo.Student;
@Controller
@RequestMapping("/SessionAttribute_zs")
public class SessionAttribute_zs {
@RequestMapping("/method")
private ModelAndView testSessionAttribute_zs(@SessionAttribute("qyc") Student student) {
ModelAndView modelAndView = new ModelAndView();
System.out.println(student);
modelAndView.setViewName("success");
return modelAndView;
}
}
<%@page import="com.qyc.pojo.Student"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'SessionAttribute_zs.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%
Student student = new Student("强月城","bxxaql");
session.setAttribute("qyc", student);
request.getRequestDispatcher("/SessionAttribute_zs/method.do").forward(request, response);
%>
</body>
</html>