问题
I'm struggling with errors on the web.xml where all the pages are comming up as 404, possibly there is a root path but I can not be sure where its set etc..
This is my current 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>
My listener controller is like this
/*
* 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);
}
This is the current error that is coming out. How come its not mapping, how do I go about fixing this?
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']]
回答1:
I answered one of your questions prior off the top of my head. I have now have access to one of my spring apps. Here is a better configuration.
Notice the change to the web.xml, I apologize but mapping to /* causes all of your requests to be resolved by the dispatcher. In a sense your creating a loop, your initial mapping will be forwarded by the dispatcher to the controller which will then use a view resolver to map where your request should be forwarded. Mapping to /* causes the view resolver mapping to be handled by the dispatcher.
Changing to / causes all unmapped urls to be handled by the dispatcher, so your initial mapping is handled by the dispatcher, which sends it to the controller and the mapping created by your viewresolver will be mapped to the .jsp causing it to not be picked up by the dispatcher. My apologies.
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 (You must change the component scan)
<?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>
Controller
@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);
}
回答2:
Thank you Kbm for coming back to me. I've altered my web.xml and the general mappings have resolved. I've ran into the problem you mentioned with the css,js,image files also getting passed.
I've tried to add intercept url's but something is not working still. http is highlighted red in the web.xml. When I hover over it in eclipse it expresses
cvc-complex-type.2.4.a: Invalid content was found starting with element 'http'. One of '{"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/ns/javaee":error- page, "http://java.sun.com/xml/ns/javaee":jsp-config, "http://java.sun.com/xml/ns/javaee":security- constraint, "http://java.sun.com/xml/ns/javaee":login-config, "http://java.sun.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/ 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- destination, "http://java.sun.com/xml/ns/javaee":locale-encoding-mapping-list}' is expected.
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>
来源:https://stackoverflow.com/questions/13184411/friendly-url-mapping-issues-java-spring