首页 > 代码库 > springmvc__SimpleUrlHandlerMapping(对访问地址进行加工,以键值对的形式)

springmvc__SimpleUrlHandlerMapping(对访问地址进行加工,以键值对的形式)

1.配置web.xml(这里配置url-pattern为/)

<!-- 编码过滤器 -->
    <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>
        <init-param>
            <!-- 任何情况下强制使用此编码 -->
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>

    <filter-mapping>
        <filter-name>characterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <!-- 中央调度器 -->
    <servlet>
        <servlet-name>springMvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:applicationContext.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>springMvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

2.在applicationContext.xml中对静态资源进行处理

<!-- 使用MVC的默认servlet(该处理器其实就是调用tomcat的defaultservlet) -->
    <mvc:default-servlet-handler />

3.配置处理器

public class MyController extends AbstractController {

    @Override
    public ModelAndView handleRequestInternal(HttpServletRequest arg0,
            HttpServletResponse arg1) throws Exception {
        
        ModelAndView mv = new ModelAndView();
        
        mv.addObject("msg", "处理器返回的model");
        mv.setViewName("one");
        
        return mv;
    }

}

4.注册处理器

<!-- 注册处理器 -->
    <bean id="frist" class="cn.cnsdhzzl.controller.MyController">
        <property name="supportedMethods" value="GET,POST"></property>
    </bean>

5.配置SimpleUrlHandlerMapping设置访问路径

<!-- 使用simpleURLhandlermapping -->
    <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <props>
                <prop key="/simple">frist</prop>
            </props>
        </property>
    </bean>

6.访问地址

技术分享 

 

springmvc__SimpleUrlHandlerMapping(对访问地址进行加工,以键值对的形式)