SpringMVC之参数传递

    对于页面的参数如何传递到后台,即参数的返回,看如下示例:

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>


4、view.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">
	-->
  </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";
	}
  此种方式同样可以完成参数的传递。



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值