Spring MVC(三)控制器获取页面请求参数以及将控制器数据传递给页面和实现重定向的方式
首先做好环境配置
在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
Spring MVC(三)控制器获取页面请求参数以及将控制器数据传递给页面和实现重定向的方式的更多相关文章
- springboot(服务端接口)获取URL请求参数的几种方法
原文地址:http://www.cnblogs.com/xiaoxi/p/5695783.html 一.下面为7种服务端获取前端传过来的参数的方法 常用的方法为:@RequestParam和@Req ...
- SpringBoot 拦截器获取http请求参数
SpringBoot 拦截器获取http请求参数-- 所有骚操作基础 目录 SpringBoot 拦截器获取http请求参数-- 所有骚操作基础 获取http请求参数是一种刚需 定义拦截器获取请求 为 ...
- spring mvc(2):请求地址映射(@RequestMapping)
@RequestMapping 参数说明 value定义处理方法的请求的 URL 地址.method定义处理方法的 http method 类型,如 GET.POST 等.params定义请求的 UR ...
- spring mvc controller中获取request head内容
spring mvc controller中获取request head内容: @RequestMapping("/{mlid}/{ptn}/{name}") public Str ...
- SpringBoot获取http请求参数的方法
SpringBoot获取http请求参数的方法 原文:https://www.cnblogs.com/zhanglijun/p/9403483.html 有七种Java后台获取前端传来参数的方法,稍微 ...
- 使用Spring mvc接收整个url地址及参数时注意事项
使用Spring mvc接收整个url地址及参数时注意事项:url= http://baidu?oid=9525c1f2b2cd45019b30a37bead6ebbb&td=2015-08- ...
- springboot获取URL请求参数的多种方式
1.直接把表单的参数写在Controller相应的方法的形参中,适用于get方式提交,不适用于post方式提交. /** * 1.直接把表单的参数写在Controller相应的方法的形参中 * @pa ...
- springboot获取URL请求参数的几种方法
原文地址:http://www.cnblogs.com/xiaoxi/p/5695783.html 1.直接把表单的参数写在Controller相应的方法的形参中,适用于get方式提交,不适用于pos ...
- JS获取url请求参数
JS获取url请求参数,代码如下: // 获取url请求参数 function getQueryParams() { var query = location.search.substring(1) ...
随机推荐
- Java 运算符 % 和 /
/ 是除运算符, %是取模运算符 区别: / 是普通的除法运算,如果除数和被除数都是整数,则商是取整 %是求余数 private static void test() { System. / ); S ...
- 基于tcp实现远程执行命令
命令执行服务器: # Author : Kelvin # Date : 2019/1/30 20:10 from socket import * import subprocess ip_conf = ...
- TensorFlow从1到2(七)线性回归模型预测汽车油耗以及训练过程优化
线性回归模型 "回归"这个词,既是Regression算法的名称,也代表了不同的计算结果.当然结果也是由算法决定的. 不同于前面讲过的多个分类算法或者逻辑回归,线性回归模型的结果是 ...
- Spring Boot Web 自定义注解篇(注解很简单很好用)
自从spring 4.0 开放以后,可以添加很多新特性的注解了.使用系统定义好的注解可以大大方便的提高开发的效率. 下面我贴一段代码来讲解注解: 通过小小的注解我们支持了以下功能: 使 spring. ...
- docker(5):数据的管理
Docker的volume卷 为了能持久话保存和共享容器的数据. 使用docker volume卷的两种方式 1:数据卷 2:数据卷容器 1:数据卷 数据卷:数据卷会绕过docker 的ufs 直接写 ...
- SQLServer之修改用户自定义数据库用户
修改用户自定义数据库用户注意事项 默认架构将是服务器为此数据库用户解析对象名时将搜索的第一个架构. 除非另外指定,否则默认架构将是此数据库用户创建的对象所属的架构. 如果用户具有默认架构,则将使用默认 ...
- 多线程总结之旅(1):线程VS进程
一.进程:进程是具有一定独立功能的程序关于某个数据集合上的一次运行活动,也就是应用程序的执行实例,进程是系统进行资源分配和调度的一个独立单位.每个进程是由私有的虚拟地址空间.代码.数据和其它各种系统资 ...
- springcloud之服务注册与发现(zookeeper注册中心)-Finchley.SR2版
新年第一篇博文,接着和大家分享springcloud相关内容:本次主要内容是使用cloud结合zookeeper作为注册中心来搭建服务调用,前面几篇文章有涉及到另外的eureka作为注册中心,有兴趣的 ...
- 从壹开始前后端分离 [ Vue2.0+.NET Core2.1] 十七 ║Vue基础:使用Vue.js 来画博客首页+指令(一)
缘起 书说前两篇文章<十五 ║ Vue前篇:JS对象&字面量&this>和 <十六 ║ Vue前篇:ES6初体验 & 模块化编程>,已经通过对js面向对 ...
- 使用 Moq 测试.NET Core 应用 -- 其它
第一篇文章, 关于Mock的概念介绍: https://www.cnblogs.com/cgzl/p/9294431.html 第二篇文章, 关于方法Mock的介绍: https://www.cnbl ...