例1:springmvc实现Controller接口
package com.pac.springmvc.action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
public class HelloAction implements Controller{
public HelloAction() {
System.out.println("helloAction");
}
public ModelAndView handleRequest(HttpServletRequest arg0,HttpServletResponse arg1) throws Exception {
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("message", "SpringMVC测试");
modelAndView.setViewName("/WEB-INF/jsp/success.jsp");
return modelAndView;
}
} 必须在web.xml中配置DispatcherServlet
,可以配置springmvc的配置文件的存储位置,否则默认就是WEB-INF目录下;另外要想让jsp以及程序的编码为UTF-8,还必须配置Filter
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- 配置springmvc核心控制器以及配置文件存放的位置 -->
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
例2:继承自AbstractCommanderContrller
package com.pac.springmvc.action2;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.validation.BindException;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractCommandController;
/*
* 解决字符输出乱码的问题
* 在web.xml文件中配置CharacterEncodingFilter
* */
@SuppressWarnings("deprecation")
public class EmpAction extends AbstractCommandController{
//调用AbstractCommandController的setCommandClass方法将jsp的数据收集并封装到指定的类中
public EmpAction(){
setCommandClass(Emp.class);
}
/*
* 将String类型转换成一个指定的类型
*String->Date
*/
protected void initBinder(HttpServletRequest request,ServletRequestDataBinder binder) throws Exception {
//向springmvc内部注入一个自定义的类型转换器
//参数一:将String转成什么类型的字节码
//参数二:自定义转换规则
//true表示该日期字段可以为空
binder.registerCustomEditor(
Date.class,
new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"),true));
}
@Override
protected ModelAndView handle(HttpServletRequest request,
HttpServletResponse response, Object obj, BindException error)
throws Exception {
ModelAndView modelAndView = new ModelAndView();
System.out.println(request.getParameter("date").toString());
Emp emp = (Emp) obj;
System.out.println(emp.getName()+"\n"+emp.getGender()+"\n"+emp.getDate().toLocaleString().substring(0, 10));
modelAndView.addObject("message", "员工信息更新完成");
//将Emp封装到ModeAndView对象中
modelAndView.addObject("emp",emp);
modelAndView.setViewName("success");
return modelAndView;
}
}
例3:利用注解
package com.pac.test2;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.support.WebRequestDataBinder;
import com.pac.entity.User;
//添加了注解以后,springmvc就会根据配置文件来扫描对应包下的Action,知道找到或者扫描结束
@Controller
@RequestMapping(value="/user")
public class UserAction {
/**
* 自定义类型转换器
*/
@InitBinder
public void initBinder(WebRequestDataBinder binder) throws Exception {
binder.registerCustomEditor(
Date.class,
new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"),true));
}
/**
* 用户注册,只能接收POST请求
*/
@RequestMapping(method=RequestMethod.POST,value="/register")
public String registerMethod(User user,Model model) throws Exception{
System.out.println("用户注册:" + user.toString());
//将user绑定到model对象中
model.addAttribute("user",user);
//转发到success.jsp页面(逻辑路径,必须配置视图适配器)
return "success";
}
}
利用扫描并配置转发的路径
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<context:component-scan base-package="com.pac.test2"></context:component-scan>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>