对于页面的参数如何传递到后台,即参数的返回,看如下示例:
1、Springmvc 配置文件
<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!--
使Spring支持自动检测组件,如注解的Controller
-->
<context:component-scan base-package="cn.com.yy.controller"/>
<!-- 开启注解配置 -->
<mvc:annotation-driven/>
<!-- 支持JSP JSTL的解析器 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/page/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
2、Controller
package cn.com.yy.controller;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/data")
public class ParameterTransferController {
@RequestMapping("/addUser")
public String add(String username,String password,HttpServletRequest request){
request.setAttribute("username1", username);
request.setAttribute("password1", password);
return "view";
}
@RequestMapping("/jump")
public String toPage(){
return "add";
}
}
3、add.jsp
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
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">
-->
<script type="text/javascript">
function addUser(){
var form = document.forms[0];
form.action = "/TestSpringMVC3/data/addUser";
form.method = "post";
form.submit();
}
</script>
</head>
<body>
<form action="/data/addUser">
username:<input type="text" name="username"><br>
password:<input type="text" name="password"><br>
<input type="button" value="add user" οnclick="addUser()">
</form>
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
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>
username:${username1}<br>
username:${password1}
</body>
</html>
5、结果
(1)首先访问http://localhost:8080/TestSpringMVC3/data/jump,跳转到add.jsp页面
(2)在add.jsp中,输入内容,提交
(3)会跳转到view.jsp中
6、分析
(1)从页面到Controller参数传递
关键在于页面的参数命名和controller方法中的参数命名一致。
(2)从Controller到页面展示
就是Servlet参数传递。
将进一步优化,引入实体类:
package cn.com.yy.entity;
public class User {
private String username;
private String password;
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;
}
}
在Controller中,使用如下方式:
@RequestMapping("/addUser2")
public String add2(User user,HttpServletRequest request){
request.setAttribute("username1", user.getUsername());
request.setAttribute("password1", user.getPassword());
return "view";
}
此种方式同样可以完成参数的传递。