开发者

URL Pattern 404

开发者 https://www.devze.com 2023-02-14 13:52 出处:网络
Having a problem with url-pattern in servlet mapping. I am using spring 3.0 and using annotated controllers. I have mapped *.do with the Spring\'s DispatcherServlet. I was trying to use @PathVariable

Having a problem with url-pattern in servlet mapping. I am using spring 3.0 and using annotated controllers. I have mapped *.do with the Spring's DispatcherServlet. I was trying to use @PathVariable with url - /test.do/{username} This is throwing a 404. Tried few trial and error and it did not help. url-patter "/" works but my other parts of the application are broken (CSS, images 开发者_如何转开发etc)


When using url with out mapping extension (such .do, .html, etc) to spring, all of your URL will be handled by spring. so you will need to map your static resource to another URL and handled with other servlet.

example : your old css will map to http://localhost:8080/css/style.css and your css will mapped to http://localhost:8080/static/css/style.css

and add servlet mapping for /static/* url

     <!-- Servlet for static resource -->
     <servlet>
        <servlet-name>static</servlet-name>
        <servlet-class>com.jegbagus.servlet.DefaultServlet</servlet-class>      
     </servlet>

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

    <!-- Mapper for static resource --> 
    <servlet-mapping>
        <servlet-name>static</servlet-name>
        <url-pattern>/static/*</url-pattern>
    </servlet-mapping>

and add servlet for handling those static request.

package com.jegbagus.servlet;

import java.io.*;

import javax.servlet.*;
import javax.servlet.http.*;

public class DefaultServlet extends HttpServlet
{   
    private static final long serialVersionUID = 1L;

    // Tomcat, Jetty, JBoss, and GlassFish 
    private static final String COMMON_DEFAULT_SERVLET_NAME = "default";

    // Resin 
    private static final String RESIN_DEFAULT_SERVLET_NAME = "resin-file";

    // WebLogic 
    private static final String WEBLOGIC_DEFAULT_SERVLET_NAME = "FileServlet";

    // WebSphere 
    private static final String WEBSPHERE_DEFAULT_SERVLET_NAME = "SimpleFileServlet";


    public String scanDefaultServlet(){
        if(this.getServletContext().getNamedDispatcher(COMMON_DEFAULT_SERVLET_NAME) != null) {
            return COMMON_DEFAULT_SERVLET_NAME;
        } else if(this.getServletContext().getNamedDispatcher(RESIN_DEFAULT_SERVLET_NAME) != null) {
            return RESIN_DEFAULT_SERVLET_NAME;
        } else if(this.getServletContext().getNamedDispatcher(WEBLOGIC_DEFAULT_SERVLET_NAME) != null){
            return WEBLOGIC_DEFAULT_SERVLET_NAME;
        } else if(this.getServletContext().getNamedDispatcher(WEBSPHERE_DEFAULT_SERVLET_NAME) != null){
            return WEBSPHERE_DEFAULT_SERVLET_NAME;
        } else {
            throw new IllegalStateException("Cannot determine what Server you currently use");
        }       
    }

    public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }

    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
    {
        RequestDispatcher rd = getServletContext().getNamedDispatcher(this.scanDefaultServlet());
        HttpServletRequest wrapped = new HttpServletRequestWrapper(req) {
                public String getServletPath() {return "";}
        };
        rd.forward(wrapped, resp);
    }
}


I don't think the URI template in 3.0 supports the @pathvariable in url with extension like .do.

http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/mvc.html#mvc-ann-requestmapping-uri-templates

You need to use directory url patterns like you have done / or /soft/ in your web.xml.

You can either setup a path that spring servlet will ignore the mapping like in the following example, https://src.springframework.org/svn/spring-samples/mvc-basic/trunk/ or use web.xml so that a request to a path such as /soft/* will only be handled by the spring servlet.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号