首先做好环境配置


在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. 从壹开始 [ Nuxt.js ] 之二 || 项目搭建 与 接口API

    前言 哈喽大家周一好,今天的内容比较多,主要就是包括:把前端页面的展示页给搭出来,然后调通接口API,可以添加数据,这两天我也一直在开发,本来想一篇一篇的写,发现可能会比较简单,就索性把项目搭建的过程 ...

  2. 从壹开始前后端分离[.NetCore] 37 ║JWT完美实现权限与接口的动态分配

    缘起 本文已经有了对应的管理后台,地址:https://github.com/anjoy8/Blog.Admin 哈喽大家好呀!又过去一周啦,这些天小伙伴们有没有学习呀,已经有一周没有更新文章了,不过 ...

  3. NIO(生活篇)

    今晚是个下雨天,写完今天最后一行代码,小鲁班起身合上电脑,用滚烫的开水为自己泡制了一桶老坛酸菜牛肉面.这大概是苦逼程序猿给接下来继续奋战的自己最好的馈赠.年轻的程序猿更偏爱坐在窗前,在夜晚中静静的享受 ...

  4. 基于 websocket 实现的 im 实时通讯案例

    分享利用 redis 订阅与发布特性,巧妙的现实高性能im系统.为表诚意,先贴源码地址:https://github.com/2881099/im 下载源码后的运行方法: 运行环境:.NETCore ...

  5. 目标检测 anchor 理解笔记

    anchor在计算机视觉中有锚点或锚框,目标检测中常出现的anchor box是锚框,表示固定的参考框. 目标检测的任务: 在哪里有东西 难点: 目标的类别不确定.数量不确定.位置不确定.尺度不确定 ...

  6. VirtualAPK的简单使用

    VirtualApk引入步骤: 一.宿主应用引入VirtualApk 1.在项目的build.gradle文件中加入依赖: dependencies { classpath 'com.didi.vir ...

  7. sql server replace的替换字符,replace的使用

    sql server replace的替换字符,replace的使用 select REPLACE(name,'张','') * from entity_5c7a578c05c7042958d9148 ...

  8. navicat导入.sql文件

    用Navicat for Mysql导入.sql文件   虽然这算不上什么难事,但是对于新手来说(比如说我),Navicat for MySQL里的导出连接.运行SQL文件.导入向导.还原备份.这些功 ...

  9. Luogu P5284 [十二省联考2019]字符串问题

    好难写的字符串+数据结构问题,写+调了一下午的说 首先理解题意后我们对问题进行转化,对于每个字符串我们用一个点来代表它们,其中\(A\)类串的点权为它们的长度,\(B\)类串的权值为\(0\) 这样我 ...

  10. C# ListView 控件和 INotifyPropertyChanged 接口

    ListView 控件和 DataGridView 控件 ListView 是跟 Winform 中 DataGridView 用法以及显示效果差不多的一个 WPF 控件,可以通过列表的方式方便的显示 ...