浅谈SpringMVC的五种数据提交方式
【摘要】 浅谈SpringMVC的五种数据提交方式
单数据提交
//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="${pageContext.request.contextPath}/placeholder/66/张三.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";
}
【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)