使用int类型进行接收
- 在Spring MVC里面对于参数的接收也都是按照字符串接收
- 而后在帮助用户自动进行转型控制
测试方法:
public class EmpAction extends AbstractAction{
private Logger log = Logger.getLogger(EmpAction.class) ;
@RequestMapping("remove")
public ModelAndView remove(int eid){
System.out.println("输出的eid结果:"+ eid * 2);
return null;
}
}
测试url-1:
http://localhost:8080/springdemo/pages/emp/remove.action?eid=123
输出结果:
输出的eid结果:246
测试url-2:
http://localhost:8080/springdemo/pages/emp/remove.action?eid=liang
如果输入的类型不同,则出现报错,输出结果:
2018-12-07 14:14:36,648 WARN [org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver] - Resolved [org.springframework.web.method.annotation.MethodArgumentTypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'int'; nested exception is java.lang.NumberFormatException: For input string: "liang"]
设置参数名称
@RequestParam的作用:
- 可以指定参数名称
- 如果没有设置参数内容,可自动使用一个默认值来替换
测试方法:
import org.springframework.web.bind.annotation.RequestParam;
public class EmpAction extends AbstractAction{
private Logger log = Logger.getLogger(EmpAction.class) ;
@RequestMapping("remove")
public ModelAndView remove(@RequestParam(value="empno",defaultValue="10") int eid){
System.out.println("输出的eid结果:"+ eid * 2);
return null;
}
}
测试url-1:
http://localhost:8080/springdemo/pages/emp/remove.action?empno=1121
输出结果:
输出的eid结果:2242
测试url-2:
http://localhost:8080/springdemo/pages/emp/remove.action
输出结果:
输出的eid结果:20
分页处理
测试方法:
public class EmpAction extends AbstractAction{
private Logger log = Logger.getLogger(EmpAction.class) ;
@RequestMapping("list")
public ModelAndView list(
@RequestParam(value = "cp", defaultValue = "1") int currentPage,
@RequestParam(value = "ls", defaultValue = "5") int lineSize ,
@RequestParam(value = "col", defaultValue = "ename") String column,
@RequestParam(value = "kw", defaultValue = "") String keyWord) {
log.info("*** currentPage = " + currentPage);
log.info("*** lineSize = " + lineSize);
log.info("*** column = " + column);
log.info("*** keyWord = " + keyWord);
return null ;
}
}
测试url-1:
http://localhost:8080/springdemo/pages/emp/list.action
输出结果:
2018-12-07 14:30:02,393 INFO [cn.liang.action.EmpAction] - *** currentPage = 1
2018-12-07 14:30:02,393 INFO [cn.liang.action.EmpAction] - *** lineSize = 5
2018-12-07 14:30:02,393 INFO [cn.liang.action.EmpAction] - *** column = ename
2018-12-07 14:30:02,393 INFO [cn.liang.action.EmpAction] - *** keyWord =
测试url-2:
http://localhost:8080/springdemo/pages/emp/list.action?cp=3&ls=10&col=ename&kw=liang
输出结果:
2018-12-07 14:31:09,188 INFO [cn.liang.action.EmpAction] - *** currentPage = 3
2018-12-07 14:31:09,193 INFO [cn.liang.action.EmpAction] - *** lineSize = 10
2018-12-07 14:31:09,193 INFO [cn.liang.action.EmpAction] - *** column = ename
2018-12-07 14:31:09,193 INFO [cn.liang.action.EmpAction] - *** keyWord = liang
内置对象配置
测试方法:
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class EmpAction extends AbstractAction{
private Logger log = Logger.getLogger(EmpAction.class) ;
@RequestMapping("get")
public ModelAndView get(HttpServletRequest request,int eid,HttpServletResponse response) {
HttpSession session = request.getSession(); // 取得Session对象
ServletContext application = request.getServletContext() ;
log.info("*** contextPath = " + request.getContextPath());
log.info("*** sessionId = " + session.getId());
log.info("*** realPath = " + application.getRealPath("/"));
try {
response.getWriter().println("Hello World !") ;
} catch (IOException e) {
e.printStackTrace();
}
return null ;
}
}
测试url:
http://localhost:8080/springdemo/pages/emp/get.action?eid=123
输出结果:
2018-12-07 14:34:41,106 INFO [cn.liang.action.EmpAction] - *** contextPath = /springdemo
2018-12-07 14:34:41,106 INFO [cn.liang.action.EmpAction] - *** sessionId = D94C3830438FE3F8D28DD4B28FB0276B
2018-12-07 14:34:41,106 INFO [cn.liang.action.EmpAction] - *** realPath = /Users/liang/Workspaces/MyEclipse 2017 CI/.metadata/.me_tcat85/webapps/springdemo/
参数与VO转换
编写Emp的VO类:
package cn.liang.vo;
import java.io.Serializable;
import java.util.Date;
@SuppressWarnings("serial")
public class Emp implements Serializable{
private Integer empno ;
private String ename ;
private Double sal ;
private Date hiredate;
private Dept dept ;
public Date getHiredate() {
return hiredate;
}
public void setHiredate(Date hiredate) {
this.hiredate = hiredate;
}
public Integer getEmpno() {
return empno;
}
public void setEmpno(Integer empno) {
this.empno = empno;
}
public String getEname() {
return ename;
}
public void setEname(String ename) {
this.ename = ename;
}
public Double getSal() {
return sal;
}
public void setSal(Double sal) {
this.sal = sal;
}
public Dept getDept() {
return dept;
}
public void setDept(Dept dept) {
this.dept = dept;
}
@Override
public String toString() {
return "Emp [empno=" + empno + ", ename=" + ename + ", sal=" + sal + ", hiredate=" + hiredate + ", dept=" + dept
+ "]";
}
}
编写Dept的VO类:
package cn.liang.vo;
import java.io.Serializable;
@SuppressWarnings("serial")
public class Dept implements Serializable {
private Integer deptno ;
private String dname ;
public Integer getDeptno() {
return deptno;
}
public void setDeptno(Integer deptno) {
this.deptno = deptno;
}
public String getDname() {
return dname;
}
public void setDname(String dname) {
this.dname = dname;
}
@Override
public String toString() {
return "Dept [deptno=" + deptno + ", dname=" + dname + "]";
}
}
- 由于日期的参数传递需要有一个转换器来控制
- 转换器是一个公共的配置,所以需要Action去继承一个父类的Action
编写AbstractAction类:
package cn.liang.util.action;
import java.text.SimpleDateFormat;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
public abstract class AbstractAction {
@InitBinder
public void initBinder(WebDataBinder binder) { // 方法名称自己随便写
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss") ;
// 本方法的处理指的是追加有一个自定义的转换编辑器,如果遇见的操作目标类型为java.util.Date类
// 则使用定义好的SimpleDateFormat类来进行格式化处理,并且允许此参数的内容为空
binder.registerCustomEditor(java.util.Date.class, new CustomDateEditor(sdf, true));
}
}
编写测试程序:
package cn.liang.action;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import cn.liang.util.action.AbstractAction;
import cn.liang.vo.Emp;
@Controller
@RequestMapping("/pages/emp/*")
public class EmpAction extends AbstractAction{
private Logger log = Logger.getLogger(EmpAction.class) ;
@RequestMapping("add")
public ModelAndView add(Emp emp) { // 此时作为一个参数,并且没有实例化
log.info(emp);
return null ;
}
}
测试url:
http://localhost:8080/springdemo/pages/emp/add.action?empno=100&ename=liang&sal=100.12&hiredate=1999-12-12%2012:23:12&dept.deptno=30&dept.dname=dv
输出结果:
2018-12-07 14:48:37,706 INFO [cn.liang.action.EmpAction] - Emp [empno=100, ename=liang, sal=100.12, hiredate=Sun Dec 12 12:23:12 CST 1999, dept=Dept [deptno=30, dname=dv]]
配置资源文件
- Spring MVC针对资源文件可以通过配置的模式实现
编辑三个资源文件
vo.edit.msg = {0} \u4fe1\u606f\u7f16\u8bd1\u5b8c\u6210\uff01
emp.add.page=/pages/back/emp/emp_add.jsp
emp.add.rules=empno:int|ename:string|sal:double|hiredate:date
配置applicationContext-mvc.xml文件
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basenames">
<array>
<value>Messages</value>
<value>Pages</value>
<value>Validations</value>
</array>
</property>
</bean>
在公共类AbstractAction类进行自动注入的配置模式
package cn.liang.util.action;
import java.util.Locale;
import javax.annotation.Resource;
import org.springframework.context.MessageSource;
public abstract class AbstractAction {
@Resource
private MessageSource msgSource ; // 表示此对象直接引用配置好的类对象(根据类型匹配)
/**
* 根据指定的key的信息进行资源数据的读取控制
* @param msgKey 表示要读取的资源文件的key的内容
* @return 表示资源对应的内容
*/
public String getValue(String msgKey,Object ...args) {
return this.msgSource.getMessage(msgKey, args, Locale.getDefault()) ;
}
}
编写测试方法:
package cn.liang.action;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import cn.liang.util.action.AbstractAction;
@Controller
@RequestMapping("/pages/emp/*")
public class EmpAction extends AbstractAction{
private Logger log = Logger.getLogger(EmpAction.class) ;
@RequestMapping("info")
public ModelAndView info() {
log.info(super.getValue("vo.edit.msg", "雇员"));
log.info(super.getValue("emp.add.page"));
log.info(super.getValue("emp.add.rules"));
return null ;
}
}
测试url:
http://localhost:8080/springdemo/pages/emp/info.action
输出结果:
2018-12-07 15:20:22,107 INFO [cn.liang.action.EmpAction] - 雇员 信息编译完成!
2018-12-07 15:20:22,110 INFO [cn.liang.action.EmpAction] - /pages/back/emp/emp_add.jsp
2018-12-07 15:20:22,113 INFO [cn.liang.action.EmpAction] - empno:int|ename:string|sal:double|hiredate:date