本文主要是介绍Spring Web MVC 的HandlerMapping的使用之-------SimpleUrlHandlerMapping(有三种配法),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
使用背景:第一步>>>
在包com.spring.web.controller下创建一个Controller: LoginConstroller
public class LoginController extends AbstractController{
public ModelAndView handleRequestInternal(HttpServletRequest request,
HttpServletResponse response)throws Exception{
String userName=request.getParameter("username");
String pwd=request.getParameter("pwd");
String msg="";
ModelAndView mav=new ModelAndView("loginResult"); //注意要在/WEB-INF/jsp下面创建loginResult.jsp页面
if(!userName.equals("liuxi")){
msg="用户名不存在!";
}else if(!pwd.equals("8888")){
msg="密码不正确!";
}else{
msg="恭喜您登录成功!";
}
mav.addObject("msg",msg);
return mav;
}
}
第二步>>>>
配置web.xml:
<?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">
<servlet>
<servlet-name>sample</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>sample</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
第三步>>>>
配置sample-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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="loginController" class="com.spring.web.controller.LoginController"/>
<bean id="handlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<!-- 配置方法一
<property name="urlMap">
<map>
<entry key="/user/login.do" value-ref="loginController"/>
</map>
</property>
-->
<!-- 配置方法二
<property name="mappings">
<bean class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="location">
<value>urlMap.properties</value> <!-- 此时urlMap.properties文件应放在WebRoot目录下! -->
</property>
</bean>
</property>
-->
<!-- 配置方法三 -->
<property name="mappings">
<props>
<prop key="/user/login.do">loginController</prop>
</props>
</property>
</bean>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix"><value>/WEB-INF/jsp/</value></property>
<property name="suffix"><value>.jsp</value></property>
<property name="viewClass">
<value>
org.springframework.web.servlet.view.JstlView
</value>
</property>
</bean>
</beans>
转自:http://blog.csdn.net/liuxiit/article/details/5715395
这篇关于Spring Web MVC 的HandlerMapping的使用之-------SimpleUrlHandlerMapping(有三种配法)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!