友好的URL映射问题 - Java Spring

时间:2012-11-01 19:47:50

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

我正在努力解决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>

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

这是当前出现的错误。为什么不映射,我该怎么做呢?

/*
 * 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);
}

2 个答案:

答案 0 :(得分:2)

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

注意对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);
}

答案 1 :(得分:0)

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

我试图添加拦截网址,但有些东西仍无法正常工作。 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/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“: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-  目的地,“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>

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

相关问题