springMVC 传递参数。

从一个页面输入数据传输到另一个页面。

 

1.User

 

package com.tgb.web.controller.entity;

public class User {
	
	private String userName;
	
	private String age;

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getAge() {
		return age;
	}

	public void setAge(String age) {
		this.age = age;
	}
}

Controller:

 

package com.tgb.web.controller.annotation;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/user2")
public class User2Controller {
	
	@RequestMapping("/addUser")
	public String addUser(HttpServletRequest request){
		
		String result ="this is addUser------优化版";
		request.setAttribute("result", result);
		return "/jquery";
	}
	
	@RequestMapping("/delUser")
	public String delUser(HttpServletRequest request){
		String result ="this is delUser------优化版";
		request.setAttribute("result", result);
		return "/jquery";
	}
	@RequestMapping("/toUser")
	public String toUser(HttpServletRequest request){
		return "/jquery";
	}
}


add.jsp

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script type="text/javascript" src="../js/jquery-1.7.1.min.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
	function addUser(){
		var form = document.forms[0];
		form.action="/springMVC6/user/data/addUser";
		form.method="post";
		form.submit();
	}
	{age:"11"}
</script>
</head>
<body>
	<h>添加用户</h>
	<form name="userForm" action="">
		姓名:<input type="text" name="userName">
		年龄:<input type="text" name="age">
		
		<input type="button" value="添加" onclick="addUser()">
		
		
	
	</form>
	
	
	
</body>
</html>


jquery.jsp

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script type="text/javascript" src="../js/jquery-1.7.1.min.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>

</head>
<body>
	<form action="/springMVC5/user/addUser" method="post">
	<h>springMVC注解1</h>
	<br/>
	<div>
		${result }
	</div>
		<input type="submit" value="post请求">
	</form>
</body>
</html>



 

二。。

user

 

package com.tgb.web.controller.entity;

public class User {
	
	private String userName;
	
	private String age;

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getAge() {
		return age;
	}

	public void setAge(String age) {
		this.age = age;
	}
}

User,jsp

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script type="text/javascript" src="../js/jquery-1.7.1.min.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
	function addUser(){
		var form = document.forms[0];
		form.action="/springMVC6/user/data/addUser";
		form.method="post";
		form.submit();
	}
	{age:"11"}
</script>
</head>
<body>
	<h>添加用户</h>
	<form name="userForm" action="">
		姓名:<input type="text" name="userName">
		年龄:<input type="text" name="age">
		
		<input type="button" value="添加" onclick="addUser()">
		
		
	
	</form>
	
	
	
</body>
</html>


DateController

 

package com.tgb.web.controller.annotation;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.tgb.web.controller.entity.User;

@Controller
@RequestMapping("/user/data")
public class DataController {
	
	@RequestMapping("/addUser")
	public String addUser(User user,HttpServletRequest request){
		request.setAttribute("userName", user.getUserName());
		request.setAttribute("age", user.getAge());
		
		return "/userManager";
	}
	
	@RequestMapping("/addUserJson")
	public void addUserJson(User user,HttpServletRequest request,HttpServletResponse response){
		//{"username":"userName","age","age"}
		String result = "{\"userName\":\" "+ user.getUserName() +" \",\"age\":\" "+ user.getAge()+" \"}";
		PrintWriter out = null;
		response.setContentType("application/json");
		
		try {
			out = response.getWriter();
			out.write(result);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	
	@RequestMapping("/delUser")
	public String delUser(){
		return "";	}
	
	@RequestMapping("/toUser")
	public String toUser(){
		return "/json";	
		}
	
	@RequestMapping("/toImg")
	public String toIMg(){
		return "/staticFile";	
		}

}




 

 

你可能感兴趣的:(springMVC)