- jsp页面代码
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="<%=request.getContextPath() %>/test/index" method="post">
<input name="name"type="text"/>
<input name="passWord"type="text"/>
<input type="submit" value="submit">
</form>
<form action="<%=request.getContextPath() %>/test/index2" method="post">
<input name="name"type="text" value="${name }" />
<input name="passWord"type="text" value="${passWord}"/>
<input type="submit" value="submit" />
</form>
<form action="<%=request.getContextPath() %>/test/index3" method="post">
<input name="name"type="text" value="${name }" />
<input name="passWord"type="text" value="${passWord}"/>
<input type="submit" value="submit" />
</form>
<form action="<%=request.getContextPath() %>/test/index4" method="post">
<input name="name"type="text" value="${name1 }" />
<input name="passWord"type="text" value="${passWord1}"/>
<input type="submit" value="submit" />
</form>
<form action="<%=request.getContextPath() %>/test/index5" method="post">
<input name="name"type="text" value="${name }" />
<input name="passWord"type="text" value="${passWord}"/>
<input type="submit" value="submit" />
</form>
</body>
</html>
这里需要一个Model文件,代码是这样的
package com.qcby.entity;
public class TestModel {
private String name;
private String passWord;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassWord() {
return passWord;
}
public void setPassWord(String passWord) {
this.passWord = passWord;
}
}
Controller代码
package com.qcby.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.qcby.entity.Article;
import com.qcby.entity.TestModel;
/*
* @author:严天贺
* @time:2020/2/1 10:40
*
**/
@Controller
@RequestMapping("/test")
public class TestController {//requestPerm方式
@RequestMapping("index")
public String index(HttpServletRequest request,HttpServletResponse response) {
String name=request.getParameter("name");
String passWord=request.getParameter("passWord");
System.out.println(name);
System.out.println(passWord);
return "index";
}
@RequestMapping("index2")//springMvc方式传参
public String index2(@RequestParam("name") String name,@RequestParam("passWord") String passWord,HttpServletRequest request) {
request.setAttribute("name",name);
request.setAttribute("passWord",passWord);
return "index";
}
@RequestMapping("index3")
public String index3(String name,String passWord,HttpServletRequest request) {
request.setAttribute("name",name);
request.setAttribute("passWord",passWord);
return "index";
}
@RequestMapping("index4")
public String index4(String name,String passWord,Model model) {
model.addAttribute("name1",name);
model.addAttribute("passWord1",passWord);
return "index";
}
@RequestMapping("index5")
public String index5(Model model,TestModel testmodel)
{
model.addAttribute("name",testmodel.getName());
model.addAttribute("passWord",testmodel.getPassWord());
System.out.print(testmodel.getName());
System.out.print(testmodel.getPassWord());
return "index";
}
@RequestMapping("index6")
@ResponseBody
public Article index6( Article article) {
return article;
}
}
小结:不同的传参方式有不同的使用优势,酌情使用就好。