Java Spring的友好Url映射问题[英] Friendly Url mapping issues - Java Spring

问题描述

我在Web.xml上遇到错误,其中所有页面都启动为404,可能有一个根路径,但我不确定它的设置等等.

这是我当前的web.xml

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']]

我的听众控制器就像这样

<?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>

这是当前出现的错误.它怎么不映射,我该如何修复?

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']]

推荐答案

我在头顶上回答了您的一个问题之一.我现在可以访问我的一个春季应用程序.这是一个更好的配置.

请注意更改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);
}

其他推荐答案

谢谢KBM回到我身边.我已经更改了我的web.xml,并且已经解决了一般映射.我已经遇到了您在CSS,JS,图像文件中提到的问题.

我试图添加拦截URL,但某些东西仍无法正常工作. HTTP在Web.xml中突出显示了红色.当我悬停在Eclipse中时,它会表达

CVC-Complex-type.2.4.a:从元素" HTTP"开始发现无效的内容. '{" http://之一 java.sun.com/xml/ns/javaee":description," http://java.sun.com/xml/ns/javaee":display-name," 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/xml/ns/javaee":error- 页面," http://java.sun.com/xml/ns/javaee":jsp-config," http://java.sun.com/xml/xml/ns/javaee":security- 约束," http://java.sun.com/xml/ns/javaee":login-config," http://java.sun.com.com/xml/ns/ javaee":security-role",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/ns/ javaee":service-ref," http://java.sun.com/xml/ns/javaee":Resource-ref," 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

本文地址:https://www.itbaoku.cn/post/978423.html