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) ...
随机推荐
- 了解Scala 宏
前情回顾 了解Scala反射介绍了反射的基本概念以及运行时反射的用法, 同时简单的介绍了一下编译原理知识, 其中我感觉最为绕的地方, 就属泛型的几种使用方式了. 而最抽象的概念, 就是对于符号和抽象树 ...
- Java8新特性之一:Lambda表达式
Java8是自java5之后最重大的一次更新,它给JAVA语言带来了很多新的特性(包括编译器.类库.工具类.JVM等),其中最重要的升级是它给我们带来了Lambda表达式和Stream API. 1. ...
- Python进阶:设计模式之迭代器模式
在软件开发领域中,人们经常会用到这一个概念——“设计模式”(design pattern),它是一种针对软件设计的共性问题而提出的解决方案.在一本圣经级的书籍<设计模式:可复用面向对象软件的基础 ...
- FreeSql 与 SqlSugar 性能测试(增EFCore测试结果)
这篇文章受大家邀请,与 SqlSugar 做一次简单的性能测试对比.主要针对插入.批量插入.批量更新.读取性能的测试: 测试环境 .net core 2.2 FreeSql 0.3.17 sqlSug ...
- 分享一个.NET平台开源免费跨平台的大数据分析框架.NET for Apache Spark
今天早上六点半左右微信群里就看到张队发的关于.NET Spark大数据的链接https://devblogs.microsoft.com/dotnet/introducing-net-for-apac ...
- [翻译 EF Core in Action 1.11] 何时不应该使用EF Core
Entity Framework Core in Action Entityframework Core in action是 Jon P smith 所著的关于Entityframework Cor ...
- Python爬虫使用lxml模块爬取豆瓣读书排行榜并分析
上次使用了BeautifulSoup库爬取电影排行榜,爬取相对来说有点麻烦,爬取的速度也较慢.本次使用的lxml库,我个人是最喜欢的,爬取的语法很简单,爬取速度也快. 本次爬取的豆瓣书籍排行榜的首页地 ...
- Centos7 nginx 虚拟主机、反向代理服务器及负载均衡,多台主机分离php-fpm实验,之强化篇,部署zabbix为例
一.简介 1.由于zabbix是php得,所有lnmp环境这里测试用的上一个实验环境,请查看https://www.cnblogs.com/zhangxingeng/p/10330735.html : ...
- 结合JDK源码看设计模式——桥接模式
前言: 在我们还没学习框架之前,肯定都学过JDBC.百度百科对JDBC是这样介绍的[JDBC(Java DataBase Connectivity,java数据库连接)是一种用于执行SQL语句的Jav ...
- Windows Server 2016 安装虚拟机版黑群晖
硬件配置 Dell R730 CPU: Intel(R) Xeon(R) CPU E5-2603 v4 @1.70GHz(6 cores) Ram: 16Gb HDD: 系统-600GB SAS X2 ...