springmvc快速入门(XML版本)
步一:创建springmvc-day01这么一个web应用
步二:导入springioc,springweb , springmvc相关的jar包
步三:在/WEB-INF/下创建web.xml文件
//web.xml
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
注意:如果这样简单配置的话,需要在/WEB-INF/创建DispatcherServlet-servlet.xml配置文件,xml头部信息与spring.xml相同,该配置文件的命名规则:web.xml文件中配置的的值-servlet.xml。
//DispatcherServlet-servlet.xml
<?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
">
<!-- 控制器(程序员) -->
<bean name="/hello.action" class="TestMvc"></bean>
<!-- 映射器(框架) -->
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean>
<!-- 适配器(框架) -->
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"></bean>
<!-- 视图解析器(框架) -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"></bean>
</beans>
action如下
public class TestMvc implements Controller{
public ModelAndView handleRequest(HttpServletRequest arg0,
HttpServletResponse arg1) throws Exception {
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("message","这是我第一个mvc");
modelAndView.setViewName("success");
return modelAndView;
}
}
注意乱码
这里的映射器是BeanNameUrlHandlerMapping这个类,意思是说明根据bean的名字来进行寻找对应的action,在jsp中如果出现乱码问题的话,如果是get,就需要手动转码name=new String( name.getBytes(“iso8859-1”),”utf-8”); 如果是post的话,需要request.setCharacterEncoding(“utf-8”);因为浏览器是utf-8的编码来进行请求的,而浏览器是用iso8859-1来进行解析的。这里的请求只是简单的请求,不是类属性,这点要注意。
如果是类的属性的话,需要在web.xml中进行这样的配置。如下
<!-- 编码过滤器 -->
<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>
还有,如果是要进行类的请求的话,action不实现Controller接口,而是继承AbstractCommandController这个类。需要在action的构造函数中先声明
public LoginAction() {
this.setCommandClass(User.class);
}
如果遇到日期这类比较复杂的数据时,需要重写这个方法
@Override
protected void initBinder(HttpServletRequest request,
ServletRequestDataBinder binder) throws Exception {
binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"),true));
}
如何实现多个请求对应一个方法
<?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
">
<!-- 控制器(程序员) -->
<bean id="userActionID" class="TestMvc"></bean>
<!-- 映射器(框架) -->
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props >
<prop key="/add.action">userActionID</prop>
<prop key="/update.action">userActionID</prop>
<prop key="/delete.action">userActionID</prop>
</props>
</property>
</bean>
<!-- 适配器(框架) -->
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"></bean>
<!-- 视图解析器(框架) -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/jsp/"></property> //前缀
<property name="suffix" value=".jsp"></property> //后缀
</bean>
</beans>
这里用的映射器是SimpleUrlHandlerMapping,是利用bean的名字来进行寻找对应的action
如何实现在自定义src下配置xml文件
需要在web.xml配置
<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:com/yun/springmvc/config/springmvc03.xml</param-value>
</init-param>**
</servlet>