Spring MVC系列[2]——参数传递及重定向
1.目录结构
  
2.代码
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<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:spring-mvc.xml</param-value>
</init-param>
<load-on-startup></load-on-startup>
</servlet> <servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
web.xml
<?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:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util"
xmlns:jpa="http://www.springframework.org/schema/data/jpa" xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd"> <!-- 开启注解扫描 -->
<context:component-scan base-package="com.java"/> <!-- 开启mvc注解扫描 -->
<mvc:annotation-driven/> <!-- 定义视图解析器 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/"/>
<property name="suffix" value=".jsp"/>
</bean> </beans>
spring-mvc.xml
 package com.java.entity;
 public class User {
     private Integer userId;
     private String userName;
     private String password;
     public Integer getUserId() {
         return userId;
     }
     public void setUserId(Integer userId) {
         this.userId = userId;
     }
     public String getuserName() {
         return userName;
     }
     public void setuserName(String userName) {
         this.userName = userName;
     }
     public String getPassword() {
         return password;
     }
     public void setPassword(String password) {
         this.password = password;
     }
 }
User
package com.java.controller; import java.util.HashMap;
import java.util.Map; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession; import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
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; import com.java.entity.User; @Controller
@RequestMapping("/demo")
public class HelloController { /**
* 接收参数方式一:采用HttpServletRequest
* @param request
* @return
*/
@RequestMapping("/test1.do")
public String test1(HttpServletRequest request){
String userName = request.getParameter("userName");
String password = request.getParameter("password"); System.out.println("userName:"+userName);
System.out.println("password:"+password); return "jsp/success";
} /**
* 接收参数方式二:采用@RequestParam注解方式
* @param userName
* @param password
* @return
*/
@RequestMapping("/test2.do")
public String test2(
@RequestParam String userName,
@RequestParam String password){
System.out.println("userName:"+userName);
System.out.println("password:"+password);
return "jsp/success";
} /**
* 接收参数方式三:采用实体方式
* 前提是: 被提交表单中name 和 实体中的属性完全一致
* @param user
* @return
*/
@RequestMapping("/test3.do")
public String test3(User user){
System.out.println("userName:"+user.getuserName());
System.out.println("password:"+user.getPassword());
return "jsp/success";
} ////////////////////////////////////////////////////////////
/*以下是对【传出参数 的研究】*/ /**
* 传出参数方式一:使用ModelAndView传出参数
*/
@RequestMapping("test4.do")
public ModelAndView test4(){
Map<String, Object> data = new HashMap<String, Object> ();
data.put("success", true);
data.put("message", "操作成功");
return new ModelAndView("jsp/success",data);
} /**
* 传出参数方式二:使用ModelMap传出参数
* @param map
* @return
*/
@RequestMapping("test5.do")
public ModelAndView test5(ModelMap map){
map.addAttribute("success", false);
map.addAttribute("message", "操作失败");
return new ModelAndView("jsp/success");
} /**
* 使用ModelAttribute传出实体属性值
* ModelAttribute 会利用 HttpServletRequest 的 Attribute传递到JSP页面中。
* @return
*/
@ModelAttribute("age")
public int getAge(){
return 25;
} /**
* 传出参数方式三:使用ModelAttribute传出参数
* 需要注意的是:@ModelAttribute括号里面的名字应该与表单name保持一致
* @param userName
* @param password
* @return
*/
@RequestMapping("/test6.do")
public ModelAndView test6(
@ModelAttribute("userName") String userName,
@ModelAttribute("password") String password){ return new ModelAndView("jsp/success");
} ////////////////////////////////////////////////////////////
/*以下是对【session 和 重定向 的研究】*/ /**
* session的使用
* 可以利用session传递参数
*/
@RequestMapping("test7.do")
public ModelAndView test7(HttpServletRequest request){ String userName = request.getParameter("userName");
String password = request.getParameter("password"); HttpSession session = request.getSession(); session.setAttribute("sal", 8000);
session.setAttribute("userName", userName);
session.setAttribute("password", password); return new ModelAndView("jsp/success");
} /**
* 测试String
* @param user
* @param modelmap
* @return
*/
@RequestMapping("test8.do")
public String test8(User user, ModelMap modelmap){
modelmap.addAttribute("user", user);
return "jsp/success";
} /**
* 返回错误页面
* @return
*/
@RequestMapping("test9.do")
public String test9(){
return "jsp/error";
} /**
* 重定向方式一:利用RedirectView重定向
* @param user
* @return
*/
@RequestMapping("test10.do")
public ModelAndView test10(User user){
if(user.getuserName().equals("java")){
return new ModelAndView("jsp/success");
}
return new ModelAndView(new RedirectView("test9.do"));
} /**
* 重定向方式二:利用redirect:前缀重定向
* 结果和方式一完全一样
* @param user
* @return
*/
@RequestMapping("test11.do")
public String test11(User user){
if("java".equals(user.getuserName())){
return "jsp/success";
}
return "redirect:test9.do";
}
}
HelloController【重要】
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head> <body>
<h2>测试</h2>
<!--
<a href="demo/test1.do">点击我试试</a>
-->
<hr> <form action="demo/test11.do" method="post">
<label id="userName">用户名:</label><input type="text" name="userName"/><br/>
<label id="password">密  码:</label><input type="password" name="password"/><br/>
<input type="submit" value="登录"/>
</form>
</body>
</html>
index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'MyJsp.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
--> </head> <body>
<h2>是否成功:${success}</h2>
<h2>提示消息:${message}</h2> <hr/> <h2>年龄:${age}</h2> <h2>用户名:${userName}</h2>
<h2>密码:${user.password}</h2> <h2>薪资:${sal}</h2> <h2>对象:${user.userName}</h2>
</body>
</html>
success.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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
不好,页面被外星人劫持!
</body>
</html>
error.jsp
3.完整项目打包下载
Spring MVC系列[2]——参数传递及重定向的更多相关文章
- spring mvc controller间跳转 重定向 传参(转)
		
spring mvc controller间跳转 重定向 传参 url:http://zghbwjl.blog.163.com/blog/static/12033667220137795252845/ ...
 - Spring MVC(十一)--使用字符串实现重定向
		
Spring MVC中有两种重定向方式: 通过返回字符串,字符串必须以redirect:开头: 通过返回ModelAndView: 重定向的时候如果需要给重定向目标方法传参数,要分字符串参数和pojo ...
 - 【Spring MVC系列】--(4)返回JSON
		
[Spring MVC系列]--(4)返回JSON 摘要:本文主要介绍如何在控制器中将数据生成JSON格式并返回 1.导入包 (1)spring mvc 3.0不需要任何其他配置,添加一个jackso ...
 - Spring mvc系列一之 Spring mvc简单配置
		
Spring mvc系列一之 Spring mvc简单配置-引用 Spring MVC做为SpringFrameWork的后续产品,Spring 框架提供了构建 Web 应用程序的全功能 MVC 模块 ...
 - spring mvc controller间跳转 重定向 传参 (转)
		
转自:http://zghbwjl.blog.163.com/blog/static/12033667220137795252845/ 1. 需求背景 需求:spring MVC框架contr ...
 - Spring Mvc Controller间跳转 重定向 传参 (转)
		
原文链接:http://zghbwjl.blog.163.com/blog/static/12033667220137795252845/ 1. 需求背景 需求:spring MVC框架con ...
 - spring mvc controller间跳转 重定向
		
1. 需求背景 需求:spring MVC框架controller间跳转,需重定向.有几种情况:不带参数跳转,带参数拼接url形式跳转,带参数不拼接参数跳转,页面也能显示. 本来以为挺简单的一 ...
 - Spring MVC—模型数据,转发重定向,静态资源处理方式
		
Spring MVC处理模型数据 添加模型数据的方法 ModelAndView Map及Model SessionAttribute ModelAttribute Spring MVC转发和重定向 S ...
 - 我看Spring MVC系列(一)
		
1.Spring MVC是什么: Spring MVC:Spring框架提供了构建Web应用程序的全功能MVC模块. 2.Spring helloWorld应用(基于Spring 4.2) 1.添加S ...
 
随机推荐
- TModJS:使用tmodjs
			
ylbtech-TModJS:使用tmodjs 1.返回顶部 1. 1.安装 npm install -g tmodjs 2.配置 我的模板都放在tpl文件夹中,htmls用于存放模板页面,每一个后缀 ...
 - docker 学习(四) springboot +  docker
			
下面演示: 在Windows上新建一个简单的Springboot工程,生成docker iamge,然后在本地的docker上运行: (1):登录到 https://start.spring.io/, ...
 - CF-807B
			
B. T-Shirt Hunt time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...
 - TypeScript完全解读(26课时)_4.TypeScript完全解读-接口
			
4.TypeScript完全解读-接口 初始化tslint tslint --init:初始化完成后会生成tslint.json的文件 如果我们涉及到一些规则都会在这个rules里面进行配置 安装ts ...
 - Laravel中使用模型对数据进行操作
			
public function orm(){ //查询表的所有记录 //$user = Admin::all(); //dd($user); //查询某一条记录 //$user = Admin::fi ...
 - HTML学习笔记(三)样式CSS
			
1.样式 外部样式表(通过css文件定义样式,整体样式) 当样式需要被应用到很多页面的时候,使用外部样式表,在 head 部分<link>. <head> <link r ...
 - IT兄弟连 JavaWeb教程 JSP与Servlet的联系
			
Servlet是使用Java Servlet接口(API)运行在Web服务器上的Java程序,其功能十分强大,它不但可以处理HTTP请求中的业务逻辑,而且还可以输出HTML代码来显示指定页面,而JSP ...
 - 笔记-JavaWeb学习之旅7
			
JavaScript基础 概念:一门客户端脚本语言,运行在客户端浏览器中,每一个浏览器都有JavaScript的解析引擎,是一个脚本语言,不需要编译,直接就可以被浏览器解析执行. JavaScript ...
 - c语言里面你不知道的break与switch,contiune的用法
			
前言:最近上完课在宿舍闲来无事,就拿起了C Primer Plus 这本书看,是自己入门编程的第一门语言:看了一些基本语法知识点,最让我需要总一下的是就是标题所说的这个语法知识点,记得大一的时候去考计 ...
 - 一篇文章带你搞懂 SpringBoot与Swagger整合
			
Swagger使用由于不喜欢csdn的markwoen编辑器,对代码样式支持不好,看着不舒服,对审美要求比较高的同学移步github:https://github.com/itguang/swagge ...