springMVC自动获取jsp页面input中的值,封装成对象传入后台
最近做公司的项目,项目采用的是ssm框架,发现后台在采集jsp页面表单里面的数据时没有用到传统的request.getParameter(),而是在controller的传入参数多了几个对象。因此发现springMVC的这个功能。
具体例子如下所示:
建立一个jsp页面vacate.jsp
后台接受的controller代码:
[java] import java.util.Map; 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.servlet.ModelAndView; import com.zyl.nonglin.base.BaseController; import com.zyl.nonglin.base.entity.Vacate; @Controller @RequestMapping(value="/admin/qingjia") public class VacateController extends BaseController{ /** * 跳转到请假页面 * @param req * @param resp * @return */ @RequestMapping(value="/toVacate") public ModelAndView toVacate(HttpServletRequest req,HttpServletResponse resp){ Map<String,Object> context = getRootMap(); return forword("/qingjia/vacate", context); } /** * 提交请假信息 * @param vacate * @param req * @param resp */ @RequestMapping(value="/addVacate") public void addVacate(Vacate vacate,HttpServletRequest req,HttpServletResponse resp){ System.out.println(vacate.getClassNum());// System.out.println(vacate.getReason()); System.out.println(vacate.getsName()); System.out.println(vacate.getsNum()); System.out.println(vacate.getsPhone()); } }
Vacate实体类
[java] /** * 请假信息类 * @author * */ public class Vacate { private String id; private String sNum;//学生学号 private String sName;//学生姓名 private String classNum;//班级编号 private String ctime;//请假时间 private String sPhone;//学生电话 private String reason;//理由 public String getId() { return id; } public void setId(String id) { this.id = id; } public String getsNum() { return sNum; } public void setsNum(String sNum) { this.sNum = sNum; } public String getsName() { return sName; } public void setsName(String sName) { this.sName = sName; } public String getClassNum() { return classNum; } public void setClassNum(String classNum) { this.classNum = classNum; } public String getCtime() { return ctime; } public void setCtime(String ctime) { this.ctime = ctime; } public String getsPhone() { return sPhone; } public void setsPhone(String sPhone) { this.sPhone = sPhone; } public String getReason() { return reason; } public void setReason(String reason) { this.reason = reason; } }
前端页面样式:
提交后可以看到控制台输出:
转载自:http://blog.csdn.net/qq_28391549/article/details/50470268