【问题标题】:Friendly Url mapping issues - Java Spring友好的 URL 映射问题 - Java Spring
【发布时间】:2012-10-22 11:21:44
【问题描述】:

我在 web.xml 上遇到错误,所有页面都显示为 404,可能有根路径,但我不确定它的设置位置等。

这是我当前的 web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>Spring3MVC</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
    <servlet-name>spring</servlet-name>
    <servlet-class>
                  org.springframework.web.servlet.DispatcherServlet
              </servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>/*</url-pattern>

  </servlet-mapping>
</web-app>

我的监听控制器是这样的

/*
 * User
*/
@RequestMapping(value={"/user/{id}"}, method=RequestMethod.GET)
public ModelAndView profileDisplay(
        HttpServletRequest request, 
        HttpServletResponse response,
        @RequestParam(value="id", required=false) String id
) throws UnknownHostException, MongoException {
    ServiceSerlvet.appendSesssion(request);
    //get search ALL users
    BasicDBObject searchQuery = new BasicDBObject();
        searchQuery.put("_id", new ObjectId(id));
    List<DBObject> searchResponse = PersonController.searchUsers(searchQuery);      

    //System.out.println("response from search user method: "+searchResponse);

        return new ModelAndView("user", "people", searchResponse);
}

这是当前出现的错误。为什么它没有映射,我该如何解决这个问题?

INFO: Server startup in 5904 ms
01-Nov-2012 19:40:21 org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping found for HTTP request with URI [/springApp21] in DispatcherServlet with name 'spring'
01-Nov-2012 19:40:22 org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver handleNoSuchRequestHandlingMethod
WARNING: No matching handler method found for servlet request: path '/user', method 'GET', parameters map['id' -> array<String>['4fa6eddc0234964172522248']]
01-Nov-2012 19:40:24 org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver handleNoSuchRequestHandlingMethod
WARNING: No matching handler method found for servlet request: path '/user', method 'GET', parameters map['id' -> array<String>['4fa6eddc0234964172522248']]

【问题讨论】:

  • 你在浏览器上输入的网址是什么
  • 当我从 Eclipse 作为“我的 Eclipse 应用程序”运行时,它也会打开页面 robot-oi772f3re:8080/springApp21
  • 你是主页还是404
  • 是的,它是 404,如果我将 /* 更改为 springApp21/* 但我确信这是不对的,然后它只会将我带到主页 - 我的其他粒度映射不起作用
  • 我缺少一个属性参数吗?我想在 web.xml 中只有一个 url-pattern 元素,并在控制器中处理粒度映射。 - 我需要添加异常以确保图像、js 和 css 路径也不会被控制器处理。它们从 web.xml 中也列出的某种安全标签开始。我对 Java spring 很陌生 - 不知道如何正确设置它以实现这种可伸缩性。

标签: java spring spring-mvc mapping friendly-url


【解决方案1】:

感谢 Kbm 回到我身边。我已经改变了我的 web.xml 并且一般映射已经解决。我遇到了你提到的 css、js、图像文件也通过的问题。

我尝试添加拦截网址,但有些东西仍然无法正常工作。 http 在 web.xml 中以红色突出显示。当我在 Eclipse 中将鼠标悬停在它上面时,它表示

cvc-complex-type.2.4.a:发现以元素“http”开头的无效内容。 '{"http:// 之一 java.sun.com/xml/ns/javaee”:描述,“http://java.sun.com/xml/ns/javaee”:显示名称,“http:// java.sun.com/xml/ns/javaee":icon, "http://java.sun.com/xml/ns/javaee":distributable, "http:// java.sun.com/xml/ns/javaee":context-param, "http://java.sun.com/xml/ns/javaee":filter, "http:// java.sun.com/xml/ns/javaee":filter-mapping, "http://java.sun.com/xml/ns/javaee":listener, "http:// java.sun.com/xml/ns/javaee":servlet, "http://java.sun.com/xml/ns/javaee":servlet-mapping, "http:// java.sun.com/xml/ns/javaee":session-config, "http://java.sun.com/xml/ns/javaee":mime-mapping, "http://java.sun.com/xml/ns/javaee":welcome-file-list, "http://java.sun.com/xml/ns/javaee":error- 页面,“http://java.sun.com/xml/ns/javaee”:jsp-config,“http://java.sun.com/xml/ns/javaee”:security- 约束, "http://java.sun.com/xml/ns/javaee":login-config, "http://java.sun.com/xml/ns/ javaee”:安全角色,“http://java.sun.com/xml/ns/javaee”:env-entry,“http://java.sun.com/xml/ns/ javaee":ejb-ref, "http://java.sun.com/xml/ns/javaee":ejb-local-ref, "http://java.sun.com/xml/ns/ javaee”:服务引用,“http://java.sun.com/xml/ns/javaee”:资源引用,“http://java.sun.com/xml/ns/ javaee":resource-env-ref, "http://java.sun.com/xml/ns/javaee":message-destination-ref, "http:// java.sun.com/xml/ns/javaee":persistence-context-ref, "http://java.sun.com/xml/ns/ javaee":persistence-unit-ref, "http://java.sun.com/xml/ns/javaee":post-construct, "http:// java.sun.com/xml/ns/javaee":pre-destroy, "http://java.sun.com/xml/ns/javaee":message- 目的地,“http://java.sun.com/xml/ns/javaee”:locale-encoding-mapping-list}' 是预期的。

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>Spring3MVC</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
    <servlet-name>spring</servlet-name>
    <servlet-class>
                  org.springframework.web.servlet.DispatcherServlet
              </servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

  <http auto-config='true'>
   <intercept-url pattern="/css/**" filters="none"  />
    <intercept-url pattern="/images/**" filters="none" />
    <intercept-url pattern="/js/**" filters="none" />
  </http>
</web-app>

【讨论】:

  • 请作为新问题发布或修改您的问题...不要在人们看到未回答的页面时在同一个线程中提出问题..
  • 您好 Pratap,我很想问一个新问题,但我的帐户被禁止提出新问题。我似乎只能评论或添加另一个答案。我很想解决这个模式问题 - 但现在我必须跳过这个并继续尝试学习和完成项目的其他部分
  • 你好@divine no - 自从
【解决方案2】:

我事先回答了你的一个问题。我现在可以访问我的一个 spring 应用程序。这是一个更好的配置。

请注意对 web.xml 的更改,我很抱歉,但映射到 /* 会导致您的所有请求都由调度程序解决。从某种意义上说,您创建了一个循环,您的初始映射将由调度程序转发到控制器,然后控制器将使用视图解析器映射您的请求应转发到的位置。映射到 /* 会导致视图解析器映射由调度程序处理。

更改为 / 会导致所有未映射的 url 由调度程序处理,因此您的初始映射由调度程序处理,调度程序将其发送到控制器,您的 viewresolver 创建的映射将映射到 .jsp 导致它不被调度员接走。我很抱歉。

Web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>Spring3MVC</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
    <servlet-name>spring</servlet-name>
    <servlet-class>
                  org.springframework.web.servlet.DispatcherServlet
              </servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>/</url-pattern>

  </servlet-mapping>
</web-app>

spring-config.xml(必须更改组件扫描)

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
    xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    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.1.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">

    <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->

    <!-- Enables the Spring MVC @Controller programming model -->   
    <annotation-driven/>

        <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
    <resources location="/resources/" mapping="/resources/**"/> 

    <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
    <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>

    <context:component-scan base-package="package.with.controllers" />

</beans:beans>

控制器

@RequestMapping(value={"/user/{id}"}, method=RequestMethod.GET)
public ModelAndView profileDisplay(
        HttpServletRequest request, 
        HttpServletResponse response,
        @RequestParam(value="id", required=false) String id
) throws UnknownHostException, MongoException {
    ServiceSerlvet.appendSesssion(request);
    //get search ALL users
    BasicDBObject searchQuery = new BasicDBObject();
        searchQuery.put("_id", new ObjectId(id));
    List<DBObject> searchResponse = PersonController.searchUsers(searchQuery);      

    //System.out.println("response from search user method: "+searchResponse);

        //This should display "WEB-INF/views/user.jsp" you may need to adjust.
        return new ModelAndView("user", "people", searchResponse);
}

【讨论】:

  • 我在上一个问题上帮助了他,但不小心误导了他如何映射调度程序 servlet。 {id} 是用于表示 uri 模板的语法。本质上id参数会从请求的url中拉出来,我相信这被称为restful url。
猜你喜欢
  • 2012-10-21
  • 2011-01-18
  • 2012-09-14
  • 1970-01-01
  • 2011-01-18
  • 1970-01-01
  • 2016-05-17
  • 2016-10-05
  • 2011-11-02
相关资源
最近更新 更多