首先做好环境配置


在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(三)控制器获取页面请求参数以及将控制器数据传递给页面和实现重定向的方式的更多相关文章

  1. springboot(服务端接口)获取URL请求参数的几种方法

    原文地址:http://www.cnblogs.com/xiaoxi/p/5695783.html 一.下面为7种服务端获取前端传过来的参数的方法  常用的方法为:@RequestParam和@Req ...

  2. SpringBoot 拦截器获取http请求参数

    SpringBoot 拦截器获取http请求参数-- 所有骚操作基础 目录 SpringBoot 拦截器获取http请求参数-- 所有骚操作基础 获取http请求参数是一种刚需 定义拦截器获取请求 为 ...

  3. spring mvc(2):请求地址映射(@RequestMapping)

    @RequestMapping 参数说明 value定义处理方法的请求的 URL 地址.method定义处理方法的 http method 类型,如 GET.POST 等.params定义请求的 UR ...

  4. spring mvc controller中获取request head内容

    spring mvc controller中获取request head内容: @RequestMapping("/{mlid}/{ptn}/{name}") public Str ...

  5. SpringBoot获取http请求参数的方法

    SpringBoot获取http请求参数的方法 原文:https://www.cnblogs.com/zhanglijun/p/9403483.html 有七种Java后台获取前端传来参数的方法,稍微 ...

  6. 使用Spring mvc接收整个url地址及参数时注意事项

    使用Spring mvc接收整个url地址及参数时注意事项:url= http://baidu?oid=9525c1f2b2cd45019b30a37bead6ebbb&td=2015-08- ...

  7. springboot获取URL请求参数的多种方式

    1.直接把表单的参数写在Controller相应的方法的形参中,适用于get方式提交,不适用于post方式提交. /** * 1.直接把表单的参数写在Controller相应的方法的形参中 * @pa ...

  8. springboot获取URL请求参数的几种方法

    原文地址:http://www.cnblogs.com/xiaoxi/p/5695783.html 1.直接把表单的参数写在Controller相应的方法的形参中,适用于get方式提交,不适用于pos ...

  9. JS获取url请求参数

    JS获取url请求参数,代码如下: // 获取url请求参数 function getQueryParams() { var query = location.search.substring(1) ...

随机推荐

  1. TensorFlow从1到2(一)续讲从锅炉工到AI专家

    引言 原来引用过一个段子,这里还要再引用一次.是关于苹果的.大意是,苹果发布了新的开发语言Swift,有非常多优秀的特征,于是很多时髦的程序员入坑学习.不料,经过一段头脑体操一般的勤学苦练,发现使用S ...

  2. 我为什么放弃MySQL?最终选择了MongoDB

    最近有个项目的功能模块,为了处理方便,需要操作集合类型的数据以及其他原因.考虑再三最终决定放弃使用MySQL,而选择MongoDB. 两个数据库,大家应该都不陌生.他们最大的区别就是MySQL为关系型 ...

  3. 用Docker解决坑爹的环境搭建系列——mysql:5.6

    sudo docker pull mysql:5.6 mkdir -p /data/docker/mysql/data /data/docker/mysql/logs /data/docker/mys ...

  4. 补习系列(19)-springboot JPA + PostGreSQL

    目录 SpringBoot 整合 PostGreSQL 一.PostGreSQL简介 二.关于 SpringDataJPA 三.整合 PostGreSQL A. 依赖包 B. 配置文件 C. 模型定义 ...

  5. Web前端-Ajax基础技术(下)

    Web前端-Ajax基础技术(下) 你要明白ajax是什么,怎么使用? ajax,web程序是将信息放入公共的服务器,让所有网络用户可以通过浏览器进行访问. 浏览器发送请求,获取服务器的数据: 地址栏 ...

  6. 关于GITLAB若干权限问题

    访问权限 - Visibility Level 这个是在建立项目时就需要选定的,主要用于决定哪些人可以访问此项目,包含3种 Private - 私有,只有属于该项目成员才有原先查看 Internal ...

  7. NFS挂载异常 mount.nfs: Input/output error

    [root@localhost ~]# vi /etc/exports #增加/nfs 192.168.10.132(rw,no_root_squash,no_all_squash,async) [r ...

  8. NIO的工作方式

    BIO带来的挑战 BIO 就是我们常说的阻塞I/O , 不论磁盘I/O 还是网络/O ,数据在写入OutputStream 或者从 InutStream 读取数据时都有可能会阻塞,一旦有了阻塞,线程就 ...

  9. Storm入门(七)可靠性机制代码示例

    一.关联代码 使用maven,代码如下. pom.xml  参考 http://www.cnblogs.com/hd3013779515/p/6970551.html MessageTopology. ...

  10. 再谈AbstractQueuedSynchronizer3:基于AbstractQueuedSynchronizer的并发类实现

    公平模式ReentrantLock实现原理 前面的文章研究了AbstractQueuedSynchronizer的独占锁和共享锁,有了前两篇文章的基础,就可以乘胜追击,看一下基于AbstractQue ...