Spring servlet-mapping

时间:2012-03-09 16:26:12

标签: spring spring-mvc

朋友。 我英语说的不好。请理解它。

我被要求制作一个xml文件。

当我使用浏览器访问以下地址时。我应该看到下面的xml。 的的http://本地主机:8080 /更新/ update.jsp

<update_info app_name="ktp_app" version="2012031001" count="">
  <apache_start version="20120310011255" type="bat" reg="123123" size="423">
  <remote>http://192.168.0.1/ka/apache_start.bat</remote>
  <remote>apache_start.bat</remote>
  </apache_start>
</update_info>

但是,在web.xml中 我有这个设置。

<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>*.byto</url-pattern>
</servlet-mapping>

上面,我可以使用弹簧控制器处理我的所有其他请求,如下所示。

@Controller
public class DownloadManageController {

    @Autowired
    private DownloadManageService downService;

    @RequestMapping("/ajax/add_download_hour")
    public void addDownloadHour(HttpServletRequest request, HttpServletResponse response) throws IllegalArgumentException, SecurityException, InvalidHourRangeException, IOException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
        downService.addDownloadLimit(
            request.getParameter("app_cmd"),
            request.getParameter("start_hour"),
            request.getParameter("end_hour"), 
            request.getParameter("limit_count"));
    }
    ...
}

但我也有这个控制器。

@Controller
public class UpdateXmlController extends HttpServlet {

    @Autowired
    private UpdateXmlService updateService;

    @RequestMapping("/update")
    protected ModelAndView getUpdateData(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String cmd = request.getParameter("cmd");
        Map resultMap = null;
        PrintWriter writer = response.getWriter();
        try {
            resultMap = updateService.getUpdateData(cmd);
            request.setAttribute("server", resultMap.get("server"));
            request.setAttribute("package", resultMap.get("package"));
            request.setAttribute("file", resultMap.get("file"));
        } catch(NoCmdException ex) {
            return new ModelAndView("error_page", "errorMsg", ex.getMessage());
        } catch(DownloadCountExceedException ex) {
            return new ModelAndView("error_page", "errorMsg", ex.getMessage());
        } catch(Exception ex) {
            //System.out.println(ex.getMessage());
            //response.getWriter().write();
            ex.printStackTrace();
        }

        return new ModelAndView("update", "result", resultMap);
    }

}

我需要使用 http:// localhost:8080 / update / update.jsp 进行访问。

但是,我只能使用http://localhost:8080/update/update.byto

来访问该控制器

所以,这是我的问题

如何使用.jsp地址访问该控制器。

非常感谢。

3 个答案:

答案 0 :(得分:1)

你能添加另一个servlet-mapping吗?

<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>*.byto</url-pattern>
</servlet-mapping>

<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>*.jsp</url-pattern>
</servlet-mapping>

为什么网址需要 http:// localhost:8080 / update / update.jsp

编辑 - 正如skaffman指出的那样,这将导致无限循环。我会留下这个答案作为要做的事情的一个例子!

作为Tomcat中发生的事情的一个例子:

09-Mar-2012 16:49:11 org.apache.catalina.core.ApplicationDispatcher invoke
SEVERE: Servlet.service() for servlet Test threw exception
java.lang.StackOverflowError
    at java.util.HashMap.getEntry(HashMap.java:344)
    at java.util.HashMap.containsKey(HashMap.java:335)
    at org.apache.catalina.connector.Request.removeAttribute(Request.java:1335)
    at org.apache.catalina.connector.RequestFacade.removeAttribute(RequestFacade.java:514)
    at org.apache.catalina.core.ApplicationHttpRequest.removeAttribute(ApplicationHttpRequest.java:256)
    at org.apache.catalina.core.ApplicationHttpRequest.removeAttribute(ApplicationHttpRequest.java:256)

...

at test.Test.doGet(Test.java:27)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:646)
at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:436)
at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:374)
at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:302)
at test.Test.doGet(Test.java:27)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)

答案 1 :(得分:0)

改变这个:

@Controller("/update")
...

  @RequestMapping("/update.jsp")

答案 2 :(得分:0)

暂时,我用JSP文件解决了这个问题。

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<%
    getServletContext().getRequestDispatcher("/update.byto").forward(request, response);
%>
上面的

会将所有信息发送给Spring控制器。