首先做好环境配置
在mvc.xml里进行配置
1.开启组件扫描
2.开启基于mvc的标注
3.配置试图处理器
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:lang="http://www.springframework.org/schema/lang"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-4.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd">
<!-- 开启组件扫描 -->
<context:component-scan base-package="com.xcz"></context:component-scan>
<!-- 开启mvc标注 -->
<mvc:annotation-driven></mvc:annotation-driven>
<!-- 配置视图处理器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
mvc.xml
在web.xml配置
1.配置请求参数如入口
2.配置初始化参数
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
<display-name>SpringMVC-03</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!-- 配置请求入口 -->
<servlet>
<servlet-name>SpringMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 配置初始化参数 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
web.xml
控制器获取页面请求参数方式如下:
1.使用HttpServletRequest获取直接定义 HttpServletRequest参数
2.直接把请求参数的名字定义成控制器的参数名
3.当页面参数和控制器参数不一致可以使用 @RequestParam("页面参数名"),加在控制器方法对应的参数上
package com.xcz.controller; import javax.servlet.http.HttpServletRequest; import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView; @Controller
public class LoginController {
// 获取参数方式一
@RequestMapping("/login.do")
public String login(HttpServletRequest request) {
String username = request.getParameter("username");
String password = request.getParameter("password");
System.out.println(username + ":" + password);
return "login";
} // 获取参数方式二
@RequestMapping("/login2.do")
public String login2(String username, String password) {
System.out.println(username + ":" + password);
return "login";
} // 获取参数方式三
@RequestMapping("/login3.do")
public String login3(@RequestParam("username") String uname, @RequestParam("password") String pwd) {
System.out.println(uname + ":" + pwd);
return "login";
}
}
LoginController
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!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>
<form action="${pageContext.request.contextPath }/login8.do"> <!--${pageContext.request.contextPath }动态获取路径 -->
账号:<input type="text" name="username" ><br>
密码:<input type="text" name="password" ><br>
<input type="submit" value="登录"><br>
</form>
</body>
</html>
login.lsp
控制器中数据传递给页面的方式如下:
1.使用 request session application 这些域对象传输
2.使用ModelAndView来传输数据
//mav.getModel().put("username", username);
mav.getModelMap().addAttribute("username", username);
3.使用 Model 来传输数据
4.使用ModelMap 进行传参
package com.xcz.controller; import javax.servlet.http.HttpServletRequest; import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView; @Controller
public class LoginController {
// 將控制器中的数据传给页面方式一
@RequestMapping("/login4.do")
public String login4(@RequestParam("username") String uname, @RequestParam("password") String pwd,
HttpServletRequest request) {
System.out.println(uname + ":" + pwd);
request.setAttribute("username", uname);
return "main";
} // 將控制器中的数据传给页面方式二
@RequestMapping("/login5.do")
public ModelAndView login5(@RequestParam("username") String uname, @RequestParam("password") String pwd) {
System.out.println(uname + ":" + pwd);
ModelAndView mav = new ModelAndView();
mav.setViewName("main");
mav.getModel().put("username", uname);
return mav;
} // 將控制器中的数据传给页面方式三
@RequestMapping("/login6.do")
public ModelAndView login6(@RequestParam("username") String uname, @RequestParam("password") String pwd,
ModelAndView mav) {
System.out.println(uname + ":" + pwd);
mav.setViewName("main");
mav.getModelMap().addAttribute("username", uname);
// mav.getModelMap().put("username", uname);
return mav;
} // 將控制器中的数据传给页面方式四
@RequestMapping("/login7.do")
public String login7(@RequestParam("username") String uname, @RequestParam("password") String pwd, Model model) {
System.out.println(uname + ":" + pwd);
model.addAttribute("username", uname);
return "main";
} //將控制器中的数据传给页面方式五
@RequestMapping("/login8.do")
public String login8(@RequestParam("username") String uname, @RequestParam("password") String pwd,ModelMap map) {
System.out.println(uname + ":" + pwd);
map.put("username", uname);
return "main";
}
}
LoginController
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!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>
<form action="${pageContext.request.contextPath }/login8.do"> <!--${pageContext.request.contextPath }动态获取路径 -->
账号:<input type="text" name="username" ><br>
密码:<input type="text" name="password" ><br>
<input type="submit" value="登录"><br>
</form>
</body>
</html>
login.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!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>
<h1>欢迎${username }来访</h1>
</body>
</html>
main.jsp
实现重定向方式如下:
1.当控制器方法返回String时return"redirect:路径";
默认是转发,转发的结果直接交给ViewResolver可以通过加forward:来继续处理,而不交给ViewResolver
2.当控制器方法 返回 ModelAndView 时 使用RedirectView 完成重定向 (/代表项目名前面的部分 不包含项目名)
package com.xcz.controller; import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView; @Controller
public class LoginController {
private static final String URL = "toMain.do";
@RequestMapping("/toLogin.do")
public String toLogin() {
return "login";
}
@RequestMapping("/toMain.do")
public String toMain() {
return "main";
}
// 实现重定向方式一
@RequestMapping("/login.do")
public String login(@RequestParam("username") String uname,@RequestParam("password") String pwd,HttpServletRequest request) {
System.out.println(uname + ":" + pwd);
request.setAttribute("usernameq", uname);
request.setAttribute("password", pwd);
//return "redirect:/toMain.do"; //使用redirect: 直接重定向,导致数据丢失,所以页面无法获取
return "forward:/toMain.do"; //使用forward: 先转发后跳转到main.jsp,不会导致数据丢失,所以页面可以获取控制器数据
}
// 实现重定向方式二
@RequestMapping("/login1.do")
public ModelAndView login1(@RequestParam("username") String uname,@RequestParam("password") String pwd,HttpServletRequest request) {
System.out.println(uname + ":" + pwd); //打印后台数据
String path = request.getServletContext().getContextPath(); //获取项目名前面的路径
ModelAndView mView = new ModelAndView();
RedirectView rView = new RedirectView(path + "/" + URL); //将路径和项目名进行拼接起来
mView.setView(rView);
mView.getModelMap().addAttribute("username", uname); //将控制器的数据传给页面
return mView;
}
}
LoginController
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!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>
<form action="${pageContext.request.contextPath }/login1.do"> <!--${pageContext.request.contextPath }动态获取路径 -->
账号:<input type="text" name="username" ><br>
密码:<input type="text" name="password" ><br>
<input type="submit" value="登录"><br>
</form>
</body>
</html>
login.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!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>
<h1>欢迎${param.username }来访</h1>
</body>
</html>
main.jsp