首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

怎么配置才能使struts中的*do 映射 去掉 .do ,直接用没有后缀名的映射

2012-10-08 
怎样配置才能使struts中的*.do 映射 去掉 .do ,直接用没有后缀名的映射1.一直想用struts弄成没有后缀名的

怎样配置才能使struts中的*.do 映射 去掉 .do ,直接用没有后缀名的映射
1.一直想用struts弄成没有后缀名的请求,跟javaeye的一样,今天看了下struts的源代码,发现
它截取的是最后一个/和最后一个.之间的字符串当做struts-config.xml里配的action。如:

 <servlet-mapping>    <servlet-name>action</servlet-name>    <!--      <url-pattern>/usermgr/*</url-pattern>    -->    <url-pattern>/</url-pattern>  </servlet-mapping>

其实写一个
public class LoginAction extends Action

继承action,
主要是ActionServlet,阅读它的源代码可以看到它主要一直调用的是RequestProcessor类,这个类会调用你写的action的里的execute()方法。这就是继承的妙用,覆盖方法,它会直接调用子类的方法。
2.那么我们要做的就是覆盖RequestProcessor这个类,并配置到struts-config.xml
 <!--用于定义RequestProcessor类它截取URI最后一个/后面的字符串,就当做action --><controller processorname="code">public class allController extends RequestProcessor{@Overrideprotected String processPath(HttpServletRequest request,HttpServletResponse response) throws IOException {// TODO Auto-generated method stub String path;     // Set per request the original path for postback forms     if (request.getAttribute(Globals.ORIGINAL_URI_KEY) == null) {         request.setAttribute(Globals.ORIGINAL_URI_KEY, request.getServletPath());     }     // For prefix matching, match on the path info (if any)     path = (String) request.getAttribute(INCLUDE_PATH_INFO);     if (path == null) {         path = request.getPathInfo();     }     if ((path != null) && (path.length() > 0)) {         return (path);     }     // For extension matching, strip the module prefix and extension     path = (String) request.getAttribute(INCLUDE_SERVLET_PATH);     if (path == null) {         path = request.getServletPath();     }     String prefix = moduleConfig.getPrefix();     if (!path.startsWith(prefix)) {         String msg = getInternal().getMessage("processPath");         log.error(msg + " " + request.getRequestURI());         response.sendError(HttpServletResponse.SC_BAD_REQUEST, msg);         return null;     }     path = path.substring(prefix.length());     int slash = path.lastIndexOf("/");     //int period = path.lastIndexOf(".");     path = path.substring(0);System.out.println(path);     return (path);}}{

这样的话,actionServlet就会调用allController 对象,并会调用processPath();
这个方法就是截取action的,源代码是这样写的:
    protected String processPath(HttpServletRequest request,        HttpServletResponse response)        throws IOException {        String path;        // Set per request the original path for postback forms        if (request.getAttribute(Globals.ORIGINAL_URI_KEY) == null) {            request.setAttribute(Globals.ORIGINAL_URI_KEY, request.getServletPath());        }        // For prefix matching, match on the path info (if any)        path = (String) request.getAttribute(INCLUDE_PATH_INFO);        if (path == null) {            path = request.getPathInfo();        }        if ((path != null) && (path.length() > 0)) {            return (path);        }        // For extension matching, strip the module prefix and extension        path = (String) request.getAttribute(INCLUDE_SERVLET_PATH);        if (path == null) {            path = request.getServletPath();        }        String prefix = moduleConfig.getPrefix();        if (!path.startsWith(prefix)) {            String msg = getInternal().getMessage("processPath");            log.error(msg + " " + request.getRequestURI());            response.sendError(HttpServletResponse.SC_BAD_REQUEST, msg);            return null;        }        path = path.substring(prefix.length());        int slash = path.lastIndexOf("/");        int period = path.lastIndexOf(".");        if ((period >= 0) && (period > slash)) {            path = path.substring(0, period);        }        return (path);    }

从最后几行可以看到是截取最后一个/和.之间的部分作为action,所以web.xml的urlpattern如果配置成/的话,比方说你的uri是http://localhost:8080/springStudy15/login.hhhhhhhhh
它照样可以访问到login 这个action,
所以覆盖这个方法,只截取/后面的东西。这样就成了~,哈哈~,真有成就感!,虽然还没找到工作~,哎~悲惨~

热点排行