springMVC配置一个通配的url请求,替代@RequestMapping中指定的方式

第一步、创建一个自己的ControllerUrlHandlerMapping类

MyControllerClassNameHandlerMapping.java

package com.qiyongkang.sys.controller;

import org.springframework.util.ClassUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.mvc.support.AbstractControllerUrlHandlerMapping;

/** * ClassName:MyControllerClassNameHandlerMapping <br/> * Function: TODO ADD FUNCTION. <br/> * Reason: TODO ADD REASON. <br/> * Date: 2015年10月25日 下午5:03:01 <br/> * @author qiyongkang * @version * @since JDK 1.6 * @see */
public class MyControllerClassNameHandlerMapping extends AbstractControllerUrlHandlerMapping {

    /** * 是否区分大小写 */
    private boolean caseSensitive = false;

    /** * ctrl所在包路径的前缀 */
    private String[] frameworkPackagePrefixs = new String[] {"com.qiyongkang."};

    /** * ctrl的类后缀 */
    private String[] actionClassSuffixs = new String[]{"Ctrl", "Controller"};

    /** * ctrl所在包路径的后缀 */
    private String[] actionPackageSuffixs = new String[] {"ctrl", "controller"};

    /** * 构造器 */
    public MyControllerClassNameHandlerMapping() {
        super();
    }

    @SuppressWarnings("rawtypes")
    @Override
    protected String[] buildUrlsForHandler(String beanName, Class beanClass) {
        System.out.println("beanName:" + beanName + ",beanClass:" + beanClass.getName());
        //请求路径前缀
        StringBuilder pathMapping = buildPathPrefix(beanClass);
        System.out.println("pathMapping:" + pathMapping);

        //获取类名
        String className = ClassUtils.getShortName(beanClass);
        //请求路径后缀
        String path = getEndWithSuffixPath(className, actionClassSuffixs);
        System.out.println("path:" + path);

        if (path.length() > 0) {
            if (this.caseSensitive) {
                pathMapping.append(path.substring(0, 1).toLowerCase()).append(path.substring(1));
            }
            else {
                pathMapping.append(path.toLowerCase());
            }
        }

        //最终转换后的请求url路径
        String[] finalPath = null;
        if (isMultiActionControllerType(beanClass)) {
            finalPath = new String[] {pathMapping.toString(), pathMapping.toString() + "/*"};
        }
        else {
            finalPath = new String[] {pathMapping.toString() + "*"};
        }
        for (String str : finalPath) {
            System.out.println(str);
        }
        return finalPath;
    }

    /** * * buildPathPrefix:生成请求前缀. <br/> * * @author qiyongkang * @param beanClass * @return * @since JDK 1.6 */
    @SuppressWarnings("rawtypes")
    private StringBuilder buildPathPrefix(Class beanClass) {
        StringBuilder pathMapping = new StringBuilder();
        pathMapping.append("/");
        //在这里加入自己的URL映射逻辑规范
        pathMapping.append(buildPathByBeanClass(beanClass, frameworkPackagePrefixs, actionPackageSuffixs, caseSensitive));
        return pathMapping;
    }

    /** * * buildPathByBeanClass:根据类名称和包名称来组成自定义的URL映射. <br/> * * @author qiyongkang * @param beanClass * @param frameworkPackagePrefixs * @param actionPackageSuffixs * @param caseSensitive * @return * @since JDK 1.6 */
    @SuppressWarnings("rawtypes")
    private String buildPathByBeanClass(Class beanClass, String[] frameworkPackagePrefixs, String[] actionPackageSuffixs, boolean caseSensitive) {
        String packageName = ClassUtils.getPackageName(beanClass);
        if (StringUtils.isEmpty(packageName)) {
            return "";
        }
        String actionPackageSuffix = "";
        for (String packageSuffix : actionPackageSuffixs) {
            if (packageName.endsWith(packageSuffix)) {
                actionPackageSuffix = packageSuffix;
                break;
            }
        }
        String newpackageName="";
        for (String frameworkPackagePrefix : frameworkPackagePrefixs) {
            if (packageName.startsWith(frameworkPackagePrefix)) {
                newpackageName = StringUtils.replace(packageName, frameworkPackagePrefix, "");
// break;
            }
        }
        newpackageName = StringUtils.replace(newpackageName, actionPackageSuffix, "");
        newpackageName = StringUtils.replace(newpackageName, ".", "/");
        return (caseSensitive ? newpackageName : newpackageName.toLowerCase()) + "/";
    }

    /** * * getEndWithSuffixPath:根据类后缀数组筛选出path的结果. <br/> * * @author qiyongkang * @param className * @param suffixs * @return * @since JDK 1.6 */
    private String getEndWithSuffixPath(String className, String[] suffixs) {
        String path = "";
        for (String suffix : suffixs) {
            if (className.endsWith(suffix)) {
                path = className.substring(0, className.lastIndexOf(suffix));
                break;
            }
        }
        return StringUtils.isEmpty(path) ? className : path;
    }

    public boolean isCaseSensitive() {
        return caseSensitive;
    }

    public void setCaseSensitive(boolean caseSensitive) {
        this.caseSensitive = caseSensitive;
    }

    public String[] getFrameworkPackagePrefixs() {
        return frameworkPackagePrefixs;
    }

    public void setFrameworkPackagePrefixs(String[] frameworkPackagePrefixs) {
        this.frameworkPackagePrefixs = frameworkPackagePrefixs;
    }

    public String[] getActionClassSuffixs() {
        return actionClassSuffixs;
    }

    public void setActionClassSuffixs(String[] actionClassSuffixs) {
        this.actionClassSuffixs = actionClassSuffixs;
    }

    public String[] getActionPackageSuffixs() {
        return actionPackageSuffixs;
    }

    public void setActionPackageSuffixs(String[] actionPackageSuffixs) {
        this.actionPackageSuffixs = actionPackageSuffixs;
    }



}

第二步、在springMVC配置文件中配置UrlHandlerMap

spring-mvc.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:beans="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd 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 ">
  <!-- 设置controller注解所在包 -->
  <context:component-scan base-package="com.qiyongkang.**.controller" />

  <!-- 主要作用于@Controller,激活该模式,下面是一种简写形式,完全可以手动配置替代这种简写形式,它会自动注册DefaultAnnotationHandlerMapping与AnnotationMethodHandlerAdapter,是spring MVC为@Controllers分发请求所必须的 -->
  <!-- <mvc:annotation-driven/> -->

  <!-- 配置js,css,img等静态文件直接映射到对应的文件夹,不被DispatcherServlet处理 -->
  <mvc:resources location="/resources/**" mapping="/resources/**" />

  <!-- 配置一个缺省映射的UrlHandlerMap,尝试通配URL -->
  <beans:bean id="myControllerClassNameHandlerMapping" class="com.qiyongkang.sys.controller.MyControllerClassNameHandlerMapping">
      <!-- 过滤器 -->
      <beans:property name="interceptors">
          <beans:array>
              <beans:bean id="sysLogInterceptor" class="com.qiyongkang.sys.interceptor.SysLogInterceptor"></beans:bean>
          </beans:array>
      </beans:property>
      <!-- 打开URL大小写区分 -->
      <beans:property name="caseSensitive" value="true" />
      <!-- 配置框架的包名前缀,默认值是com.qiyongkang.,这里是区分大小写的 -->
      <beans:property name="frameworkPackagePrefixs" value="com.qiyongkang." />
      <!-- 配置Controller包后缀,默认值是controller,这里是区分大小写的 -->
      <beans:property name="actionPackageSuffixs" value=".ctrl,.controller" />
      <!-- 配置Controller类后缀,默认值是Controller,这里是区分大小写的 -->
      <beans:property name="actionClassSuffixs" value="Ctrl,Controller" />
  </beans:bean>

  <!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->
  <beans:bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
      <!-- 配置转换器集合 -->
      <beans:property name="messageConverters">
          <beans:list>
              <beans:bean class="org.springframework.http.converter.BufferedImageHttpMessageConverter" />
              <beans:bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter" />
              <beans:bean class="org.springframework.http.converter.StringHttpMessageConverter">
                <!-- 解决响应文本乱码问题 -->
                <beans:property name="supportedMediaTypes">
                    <beans:list>
                        <beans:value>text/html;charset=UTF-8</beans:value>
                    </beans:list>
                </beans:property>
              </beans:bean>
              <beans:bean class="org.springframework.http.converter.xml.XmlAwareFormHttpMessageConverter" />
              <beans:bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
                <beans:property name="supportedMediaTypes">
                  <beans:list>
                    <beans:value>application/json;charset=UTF-8</beans:value>
                  </beans:list>
                </beans:property>
              </beans:bean>
          </beans:list>
      </beans:property>
  </beans:bean>

  <!-- 对转向页面的路径解析。prefix:前缀, suffix:后缀 -->
  <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <beans:property name="prefix" value="/WEB-INF/views/" />
    <beans:property name="suffix" value=".jsp" />
  </beans:bean>

  <!-- 配置文件上传组件 -->
  <beans:bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
      <!-- 配置编码,默认编码 (ISO-8859-1) -->
      <beans:property name="defaultEncoding" value="UTF-8" />
      <!-- 配置最大内存大小 (10240) -->
      <beans:property name="maxInMemorySize" value="10240" /> 
      <!-- 配置上传时的临时文件存放目录 <beans:property name="uploadTempDir" value="upload/temp" /> -->
      <!-- 配置最大文件大小,-1为无限止(-1) -->
      <beans:property name="maxUploadSize" value="-1" /> 
  </beans:bean>
</beans:beans>

beanId为myControllerClassNameHandlerMapping的类,便是我们将所有controller的请求按照自己的约定方式生成请求路径的处理类。

第三步,生成的url格式

首先,我们来看看,我们的两个controller类。
UserController.java:

package com.qiyongkang.sys.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import com.qiyongkang.sys.model.User;
import com.qiyongkang.sys.service.UserService;
import com.qiyongkang.sys.util.Page;

@Controller
@RequestMapping
public class UserController extends BaseController{

    private static Logger log = Logger.getLogger(UserController.class);

    @Autowired
    private UserService userService;

    @RequestMapping
    public ModelAndView login(HttpServletRequest request) {
        ModelAndView mav = new ModelAndView(new RedirectView(request.getContextPath() + "/index.jsp"));
        HttpSession session = request.getSession();
        session.setAttribute("user", new User());
        return mav;
    }

    @RequestMapping
    @ResponseBody
    public Page<User> getPageModel(User user, int page, int rows) {
        Page<User> pageModel = new Page<User>();
        try {
            user.setCurrentPage(page);
            user.setPageSize(rows);

            //Get total
            int total = userService.getUserCount(user);
            pageModel.setTotal(total);

            //Get list
            List<User> users = userService.listUser(user);
            pageModel.setRows(users);

        } catch (Exception e) {
            log.error("分页信息失败",e);
        }
        return pageModel;
    }

    @RequestMapping
    @ResponseBody
    public List<User> getUserList() {
        User user = new User();
        user.setCurrentPage(1);
        user.setPageSize(10);
        return userService.listUser(user);
    }

    @RequestMapping
    @ResponseBody
    public User getEntity(int id) {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return userService.getUserById(id);
    }

    @RequestMapping
    @ResponseBody
    public void editEntity(User user) {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("Edit user:" + user);
        userService.updateUser(user);
    }

    @RequestMapping
    @ResponseBody
    public void addEntity(User user) {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("Add user:" + user);
        userService.addUser(user);
    }

    @RequestMapping
    @ResponseBody
    public void deleteEntity(@RequestParam(value = "ids[]", required=true) int[] ids) {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(ids);
        List<Integer> nums = new ArrayList<Integer>();
        for (int i : ids) {
            nums.add(i);
        }
        userService.deleteUser(nums);
    }
}

RedirectCtrl.java:

package com.qiyongkang.sys.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping
public class RedirectCtrl extends BaseController {

    @RequestMapping
    public ModelAndView redirectHomePage(String path) {
        ModelAndView mav = new ModelAndView(path);
        return mav;
    }
}

再来看看项目的包结构,如下图:
springMVC配置一个通配的url请求,替代@RequestMapping中指定的方式_第1张图片

最后,再来看看控制台输出的请求路径格式:

这样,我们在前端请求后台时,只需发送如下格式的请求,例如:
http://localhost:8080/qyk_testSpringMVC/sys/user/getPageModel.do

关于通配url请求就讲到这儿啦,它的好处可想而知,以后无需在@RequestMapping中指定值,同时也可以按照一定的规则来配置整个后台请求的格式,方便前端调用。
另外,哪位小伙伴有啥疑问的,都可以给我留言,谢谢!

你可能感兴趣的:(spring,mvc,通配url,统一约定url,UrlHandler)