单数据提交
//Index.jsp
<h3>单个数据提交</h3>
<hr/>
<form action="${pageContext.request.contextPath}/one.action">
i d:<input type="text" name="id"><br>
姓名:<input type="text" name="name"><br>
<br>
<input type="submit" value="submit">
</form>
//servlet类
@Controller
public class DataSubmitAction {
/**
*i d:<input type="text" name="id"><br>
*姓名:<input type="text" name="name"><br>
*/
@RequestMapping(value = "/one")
public String oneData(int id,String name){
System.out.println("id:"+id+" name:"+name);
return "main";
}
}
对象数据提交
POJO
Index.jsp
<h3>对象数据提交</h3>
<hr/>
<form action="${pageContext.request.contextPath}/Object.action">
i d:<input type="text" name="id"><br>
姓名:<input type="text" name="name"><br>
<br>
<input type="submit" value="submit">
</form>
Servlet类(controller)
@RequestMapping(value = "/Object")
public String ObjectData(User user){
System.out.println(" id:"+user.getId()+" name:"+user.getName());
return "main";
}
动态占位符数据提交
用于超链接或地址栏提交数据
Index.jsp
<h3>动态占位符提交数据</h3>
<hr/>
<a href="https://www.ctyun.cn/portal/link.html?target=%24%7BpageContext.request.contextPath%7D%2Fplaceholder%2F66%2F%E5%BC%A0%E4%B8%89.action">动态提交数据</a>
Servlet类(controller)
@RequestMapping(value = "/placeholder/{id}/{name}")
public String placeholderData(@PathVariable int id,@PathVariable String name){
System.out.println("id: "+id+" name:"+name);
return "main";
}
或者
请求参数名称映射数据提交
解决映射名称不一致问题(提交请求参数的action的形参名称不一致,使用@RequestParam注解解析)
Index.jsp
<h3>请求参数名称映射提交数据</h3>
<hr/>
<form action="${pageContext.request.contextPath}/Mapping.action">
i d:<input type="text" name="id"><br>
姓名:<input type="text" name="name"><br>
<br>
<input type="submit" value="submit">
</form>
Servlet类(controller)
@RequestMapping("/Mapping")
public String MappingData(@RequestParam("id") int myid,@RequestParam("name") String myname){
System.out.println("id:"+myid+" name:"+myname);
return "main";
}
Servlet手工提取数据
Index.jsp
<br>
<h3>Servlet手工提取数据</h3>
<hr/>
<form action="${pageContext.request.contextPath}/handwork.action">
i d:<input type="text" name="id"><br>
姓名:<input type="text" name="name"><br>
<br>
<input type="submit" value="submit">
</form>
Servlet类(controller)
@RequestMapping("/handwork")
public String handworkData(HttpServletRequest request){
String id = request.getParameter("id");
String name = request.getParameter("name");
System.out.println("id:"+id+" name:"+name);
return "main";
}