搭建ssm框架:http://www.cnblogs.com/liyafei/p/7955413.html
1:从表单中接收 普通请求参数
结构目录
role.html
1.1 role.html表单
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <!-- 上级目录../ 本级目录./ 根目录/ 也就是http://localhost:8080/--> <!--role.html访问路径为 http://localhost:8080/html/role.html,html/**映射为WEB-INF/html/ 。在web.xml有配置 --> <!--params/commonParams.do的访问目录为 http://localhost:8080/params/commonParams.do?roleName=li¬e=yafei --> <!-- 所以可以使用../或者/ --> <!-- ./代表 html/ --> <form id="form" action="/params/commonParams.do"> <table> <tr> <td>角色名称</td> <td><input id="roleName" name="roleName" value="" /></td> <!--name的值将会作为参数名传递--> </tr> <tr> <td>备注</td> <td><input id="note" name="note" /></td> </tr> <tr> <td></td> <td align="right"><input type="submit" value="提交"/></td> </tr> </table> </form> </body> </html>
1.2 springmvc使用表现层controller接收
ParamsController.java
package com.li.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; @Controller @RequestMapping("/params") public class ParamsController { @RequestMapping("/commonParams") public ModelAndView commonParams(String roleName,String note){ //参数名一致就会获得http传输过来的参数 System.out.println("roleName>"+roleName); System.out.println("note=>"+note); ModelAndView mv=new ModelAndView(); mv.setViewName("hello"); return mv; //会被视图解析器解析 } }
1.3测试成功。
2:使用pojo接收普通参数,pojo的属性名应该与传过来的参数名相同
RoleParam.java
package com.li.pojo; public class RoleParam { String note; String roleName; public String getNote() { return note; } public void setNote(String note) { this.note = note; } public String getRoleName() { return roleName; } public void setRoleName(String roleName) { this.roleName = roleName; } }
controller中加入方法,修改form表单的action为../params/commonParamPojo.do
@RequestMapping("/commonParamPojo") public ModelAndView commonParamPojo(RoleParam roleParam){ System.out.println("roleName=>"+roleParam.getRoleName()); System.out.println("note=>"+roleParam.getNote()); ModelAndView mv=new ModelAndView(); mv.setViewName("index"); return mv; }
3:传输过来的参数名和方法名中的名字不一致时可以使用requestParam,修改form表单的action为../params/requestParam.do
/** * 当传输的参数名和映射方法中的参数名不一致时,可以使用@RequestParam获取传输过来的参数,后面 的参数名字任意定义 */ @RequestMapping("/requestParam") public ModelAndView requestParam(@RequestParam("roleName") String name,String note){ System.out.println("roleName>"+name); System.out.println("note=>"+note); ModelAndView mv=new ModelAndView(); mv.setViewName("hello"); return mv; //会被视图解析器解析 }
时间: 2024-10-09 19:16:54