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

查看:121
本文介绍了友好的URL映射问题 - Java Spring的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力解决web.xml上的错误,其中所有页面都以404的形式出现,可能有根路径,但我无法确定它的设置等等。

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

这是我当前的web.xml

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


推荐答案

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

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.

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

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.

更改为/导致所有未映射的URL由调度程序处理,因此您的初始映射由调度程序处理,调度程序将其发送到控制器并由您的viewresolver将映射到.​​jsp,导致调度程序无法获取它。我很抱歉。

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 (您必须更改组件扫描)

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>

控制器

@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);
}

这篇关于友好的URL映射问题 - Java Spring的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆