Spring Web MVC 的HandlerMapping的使用之-------SimpleUrlHandlerMapping(有三种配法)

本文主要是介绍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(有三种配法)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1020515

相关文章

Java中Map的五种遍历方式实现与对比

《Java中Map的五种遍历方式实现与对比》其实Map遍历藏着多种玩法,有的优雅简洁,有的性能拉满,今天咱们盘一盘这些进阶偏基础的遍历方式,告别重复又臃肿的代码,感兴趣的小伙伴可以了解下... 目录一、先搞懂:Map遍历的核心目标二、几种遍历方式的对比1. 传统EntrySet遍历(最通用)2. Lambd

Spring Boot 中 RestTemplate 的核心用法指南

《SpringBoot中RestTemplate的核心用法指南》本文详细介绍了RestTemplate的使用,包括基础用法、进阶配置技巧、实战案例以及最佳实践建议,通过一个腾讯地图路线规划的案... 目录一、环境准备二、基础用法全解析1. GET 请求的三种姿势2. POST 请求深度实践三、进阶配置技巧1

springboot+redis实现订单过期(超时取消)功能的方法详解

《springboot+redis实现订单过期(超时取消)功能的方法详解》在SpringBoot中使用Redis实现订单过期(超时取消)功能,有多种成熟方案,本文为大家整理了几个详细方法,文中的示例代... 目录一、Redis键过期回调方案(推荐)1. 配置Redis监听器2. 监听键过期事件3. Redi

Spring Boot 处理带文件表单的方式汇总

《SpringBoot处理带文件表单的方式汇总》本文详细介绍了六种处理文件上传的方式,包括@RequestParam、@RequestPart、@ModelAttribute、@ModelAttr... 目录方式 1:@RequestParam接收文件后端代码前端代码特点方式 2:@RequestPart接

C#中checked关键字的使用小结

《C#中checked关键字的使用小结》本文主要介绍了C#中checked关键字的使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学... 目录✅ 为什么需要checked? 问题:整数溢出是“静默China编程”的(默认)checked的三种用

SpringBoot整合Zuul全过程

《SpringBoot整合Zuul全过程》Zuul网关是微服务架构中的重要组件,具备统一入口、鉴权校验、动态路由等功能,它通过配置文件进行灵活的路由和过滤器设置,支持Hystrix进行容错处理,还提供... 目录Zuul网关的作用Zuul网关的应用1、网关访问方式2、网关依赖注入3、网关启动器4、网关全局变

SpringBoot全局异常拦截与自定义错误页面实现过程解读

《SpringBoot全局异常拦截与自定义错误页面实现过程解读》本文介绍了SpringBoot中全局异常拦截与自定义错误页面的实现方法,包括异常的分类、SpringBoot默认异常处理机制、全局异常拦... 目录一、引言二、Spring Boot异常处理基础2.1 异常的分类2.2 Spring Boot默

C#中预处理器指令的使用小结

《C#中预处理器指令的使用小结》本文主要介绍了C#中预处理器指令的使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录 第 1 名:#if/#else/#elif/#endif✅用途:条件编译(绝对最常用!) 典型场景: 示例

基于SpringBoot实现分布式锁的三种方法

《基于SpringBoot实现分布式锁的三种方法》这篇文章主要为大家详细介绍了基于SpringBoot实现分布式锁的三种方法,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录一、基于Redis原生命令实现分布式锁1. 基础版Redis分布式锁2. 可重入锁实现二、使用Redisso

SpringBoot的全局异常拦截实践过程

《SpringBoot的全局异常拦截实践过程》SpringBoot中使用@ControllerAdvice和@ExceptionHandler实现全局异常拦截,@RestControllerAdvic... 目录@RestControllerAdvice@ResponseStatus(...)@Except