文章目录
SpringMVC框架----ModelAndView(请求转发)
一.model的使用
@Controller
@RequestMapping("/user")
public class UserController {
@RequestMapping("login")
public String login(Model model){
User user = new User("张三", "333");
model.addAttribute("user",user);
return "login";
}
}
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
${user.toString()}
</body>
</html>
二.使用Map或ModelMap
@RequestMapping("login1")
public String login1(ModelMap map){ // 或使用 java.util.Map
User user = new User("张三", "333");
map.put("user",user);
return "login";
}
jsp页面同上
三.使用ModelAndView
@RequestMapping("login2")
public ModelAndView login2(){
ModelAndView modelAndView = new ModelAndView();
User user = new User("张三", "333");
modelAndView.addObject("user",user);
modelAndView.setViewName("login");
return modelAndView;
}
jsp页面同上
案例 : 返回集合
注意: maven 环境 需要导入 jstl 1.2 以上版本的jar包哦
@RequestMapping("login3")
public ModelAndView login3(){
ModelAndView modelAndView = new ModelAndView();
ArrayList<User> users = new ArrayList<>();
users.add(new User("张三", "333"));
users.add(new User("李四", "444"));
users.add(new User("王五", "555"));
modelAndView.addObject("users",users);
modelAndView.setViewName("login");
return modelAndView;
}
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<c:forEach items="${users}" var="user" >
${user.toString()}
</c:forEach>
</body>
</html>
SpringMVC高级应用
一.session的使用
方式一:传统的方式
@RequestMapping("session")
public String sessionTest(HttpServletRequest request){
HttpSession session = request.getSession();
session.setAttribute("user",new User("李四", "444"));
return "session";
}
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
${user.toString()}
</body>
</html>
方式二:注解的方式
@Controller
@RequestMapping("/user")
@SessionAttributes(value = "user") // 写上 session 的名字
public class UserController {
@RequestMapping("session1")
public ModelAndView sessionTest1() {
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("user", new User("张三", "333"));
modelAndView.setViewName("session");
return modelAndView;
}
}
Jsp 页面同上
销毁session
注意:销毁 只能销毁 传统创建 session的方式,所以实际开发中 使用传统的方式哦
@Controller
@RequestMapping("/user")
@SessionAttributes(value = "user1") // 写上 session 的名字
public class UserController {
@RequestMapping("session1")
public ModelAndView sessionTest1() {
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("user1", new User("张三", "333"));
modelAndView.setViewName("session");
return modelAndView;
}
@RequestMapping("session")
public String sessionTest(HttpServletRequest request) {
HttpSession session = request.getSession();
session.setAttribute("user", new User("李四", "444"));
return "session";
}
@RequestMapping("destroySession")
public String destroySession(HttpServletRequest request) {
HttpSession session = request.getSession();
session.invalidate(); // 销毁所有 session 对象
return "session";
}
}
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
${user.toString()} <br>
${user1.toString()} <br>
</body>
</html>
二.cookie的使用
存取cookie 传统方式
@RequestMapping("putCookie")
public String putCookie(HttpServletResponse response){
Cookie cookie = new Cookie("username", "zhansan");
response.addCookie(cookie);
return "success";
}
@RequestMapping("getCookie")
public String getCookie(HttpServletRequest request){
Cookie[] cookies = request.getCookies();
if(cookies != null){
for (Cookie cookie : cookies) {
if(cookie.getName().equals("username")){
System.out.println(cookie.getName()+cookie.getValue());
}
}
}
return "success";
}
使用注解@CookieValue 取出Cookie对象
@RequestMapping("getCookie1")
public String getCookie1(@CookieValue("username") Cookie cookie){
System.out.println(cookie.getName()+cookie.getValue());
return "success";
}
三. 登陆 案例
index.jsp
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<title>JSP - Hello World</title>
</head>
<body>
<c:if test="${sessionScope.user==null}">
<a href="user/login">登录</a>
</c:if>
<c:if test="${sessionScope.user!=null}">
登录成功
</c:if>
</body>
</html>
UserController
@Controller
@RequestMapping("/user")
public class UserController {
@RequestMapping("login")
public String login(){
return "login";
}
@RequestMapping("checklogin")
public String checklogin(User user, HttpServletRequest request){
if(request.getParameter("name").equals("zhangsan") && request.getParameter("password").equals("333")){
HttpSession session = request.getSession();
session.setAttribute("user",user);
return "redirect:/index.jsp"; // 重定向: redirect:全路径
}
return "login";
}
}
User
@Setter@Getter
public class User {
private String name;
private String password;
}
login.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
<%--<%– 协议名 服务器名称 端口名 当前项目名 –%>--%>
<%--String baseurl = request.getScheme() + "://" + request.getServerName() + request.getServerPort() + request.getContextPath();--%>
<%--pageContext.setAttribute("baseurl",baseurl);--%>
<%--%>--%>
<html>
<%--<base href="${baseurl}">--%>
<head>
<title>Title</title>
</head>
<body>
<form action="checklogin"> <%-- 相对路径写法--%>
<%--<form action="${baseurl}/user/checklogin">--%> <%-- 绝对路径写法--%>
<pre>
用户名:
<input type="text" name="name"/>
密码:
<input type="text" name="password">
<input type="submit" value="登录">
</pre>
</form>
</body>
</html>
四.放行静态资源
加配置
<!--
放行静态资源
location:静态资源真实路径
mapping: 访问路径(可以自定义)
放行static 文件夹下的所有文件
-->
<!-- 放行static文件夹下的所有文件-->
<mvc:resources mapping="static/**" location="static/"/>