Spring MVC无法解析视图名称

时间:2013-12-30 15:02:46

标签: java spring tomcat spring-mvc

我正在编写一个应用程序,我希望能够打包到一个可执行的Jar中,并使用嵌入式Web服务器来同时提供REST API和一组使用API​​的配置页面(允许API为由服务器外的其他客户端访问,例如Android应用程序。)

到目前为止,我有一个基本的REST API工作,但在视图中使用它时仍遇到问题,我尝试按照指南here进行操作,但无法获取视图名称以解析到JSP页面。以下是一些日志行,似乎表明它无法解决它:

15:37:23.559 [http-nio-8080-exec-1] DEBUG org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Looking up handler method for path /UserPage
15:37:23.560 [http-nio-8080-exec-1] DEBUG org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Did not find handler method for [/UserPage]
15:37:23.561 [http-nio-8080-exec-1] DEBUG org.springframework.web.servlet.handler.SimpleUrlHandlerMapping - Matching patterns for request [/UserPage] are [/**]
15:37:23.561 [http-nio-8080-exec-1] DEBUG org.springframework.web.servlet.handler.SimpleUrlHandlerMapping - URI Template variables for request [/UserPage] are {}
15:37:23.562 [http-nio-8080-exec-1] DEBUG org.springframework.web.servlet.handler.SimpleUrlHandlerMapping - Mapping [/UserPage] to HandlerExecutionChain with handler [org.springframework.web.servlet.resource.ResourceHttpRequestHandler@37f9ecb0] and 1 interceptor
15:37:23.563 [http-nio-8080-exec-1] DEBUG org.springframework.web.servlet.DispatcherServlet - Last-Modified value for [/UserPage] is: -1
15:37:23.564 [http-nio-8080-exec-1] DEBUG org.springframework.web.servlet.resource.ResourceHttpRequestHandler - Trying relative path [UserPage] against base location: ServletContext resource [/]
15:37:23.564 [http-nio-8080-exec-1] DEBUG org.springframework.web.servlet.resource.ResourceHttpRequestHandler - Trying relative path [UserPage] against base location: class path resource [META-INF/resources/]
15:37:23.565 [http-nio-8080-exec-1] DEBUG org.springframework.web.servlet.resource.ResourceHttpRequestHandler - Trying relative path [UserPage] against base location: class path resource [resources/]
15:37:23.565 [http-nio-8080-exec-1] DEBUG org.springframework.web.servlet.resource.ResourceHttpRequestHandler - Trying relative path [UserPage] against base location: class path resource [static/]
15:37:23.566 [http-nio-8080-exec-1] DEBUG org.springframework.web.servlet.resource.ResourceHttpRequestHandler - Trying relative path [UserPage] against base location: class path resource [public/]
15:37:23.566 [http-nio-8080-exec-1] DEBUG org.springframework.web.servlet.resource.ResourceHttpRequestHandler - No matching resource found - returning 404

我有一种感觉,因为我试图使用嵌入式Tomcat服务器运行它(而不是将其部署到Tomcat服务器)我错过了将/webapp/目录配置为webapp的步骤,但是我对Spring很新,所以不确定我错过了什么。

的src /主/ web应用/ web.xml中:

<web-app id="WebApp_ID" version="2.4" 
xmlns="http://java.sun.com/xml/ns/j2ee" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

<display-name>IeNoTenshi</display-name>
<description>IeNoTenshi internal webserver</description>

<servlet>
    <servlet-name>mvc-dispatcher</servlet-name>
    <servlet-class>
        org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>mvc-dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
</context-param>

<listener>
    <listener-class>
        org.springframework.web.context.ContextLoaderListener
    </listener-class>
</listener>

的src /主/ web应用/ WEB-INF / MVC-调度-servlet.xml中

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
    http://www.springframework.org/schema/beans     
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<context:component-scan base-package="com.dan_nixon.ienotenshi.web.service" /> 
<mvc:annotation-driven/>

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/pages/" />
    <property name="suffix" value=".jsp" />
</bean>

</beans>

的src / COM / dan_nixon /.../网/服务/ UsersController.java

@Controller
@RequestMapping("/service")
public class UsersController {
private static final Logger log = LoggerFactory.getLogger(UsersController.class);
public static final int DEFAULT_COUNT = 10;

@RequestMapping(method=RequestMethod.GET)
public String test(ModelMap model) {
    model.addAttribute("username", "test");
    model.addAttribute("name", "Test User");
    return "UserPage";
}

@RequestMapping(value="/user/{user_id}", method=RequestMethod.GET)
public @ResponseBody ModelAndView getUser(@PathVariable long user_id,
        Model model) {
    INTObjectDB db = new INTObjectDB();
    db.open();
    User user = db.getUser(user_id);
    user.getPermissions();
    db.close();
    ModelAndView mav = new ModelAndView();
    mav.setViewName("UserPage");
    mav.addObject("username", user.getUsername());
    mav.addObject("name", user.getName());
    log.error(String.format("%s", mav));
    return mav;
}

1 个答案:

答案 0 :(得分:0)

出现此错误消息

  

15:37:23.559 [http-nio-8080-exec-1] DEBUG   org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping    - 查找path / UserPage的处理程序方法

您似乎正在尝试访问类似

的内容
localhost:8080/yourApp/UserPage

但你显然没有映射到该URL的处理程序方法。你有一个映射到/service处理GET请求的处理程序方法

@RequestMapping(method=RequestMethod.GET)
public String test(ModelMap model) {
    model.addAttribute("username", "test");
    model.addAttribute("name", "Test User");
    return "UserPage";
}

您应该将请求发送到

localhost:8080/yourApp/service