2012-03-21 138 views
2

这可能很简单,但我错过了一些我猜的东西。问题归结为:我试图使用HelloController来显示“/WEB-INF/hello.jsp”。不幸的是,当我试图访问时,我得到了一个404条目。http://example.com/app/hello未找到弹簧MVC控制器

这是代码。可能是一个简单的修复。

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>app</display-name> 

<welcome-file-list> 
    <welcome-file>index.jsp</welcome-file> 
</welcome-file-list> 

<context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value>/WEB-INF/classes/applicationContext.xml</param-value> 
    </context-param> 

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

<servlet> 
    <servlet-name>dispatcher</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    <load-on-startup>1</load-on-startup> 
    <init-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>/WEB-INF/classes/applicationContext.xml</param-value> 
    </init-param> 
</servlet> 

    <servlet-mapping> 
     <servlet-name>dispatcher</servlet-name> 
     <url-pattern>/*</url-pattern> 
    </servlet-mapping> 
</web-app> 

applicationContext.xml中:

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
xmlns:security="http://www.springframework.org/schema/security" 
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" 
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:p="http://www.springframework.org/schema/p" 
xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx-3.1 
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.1.xsd 
        http://www.springframework.org/schema/security 
        http://www.springframework.org/schema/security/spring-security-3.1.xsd 
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd"> 

<context:component-scan base-package="web.controller" /> 

<bean id="viewResolver" 
    class="org.springframework.web.servlet.view.InternalResourceViewResolver" 
    p:prefix="/WEB-INF/" p:suffix=".jsp" /> 

</beans> 

HelloController.java:

@Controller 
public class HelloController { 

    @RequestMapping(value="/hello", method=RequestMethod.GET) 
    public ModelAndView helloWorld() { 
     ModelAndView mv = new ModelAndView(); 
     mv.setViewName("hello"); 
     return mv; 
    } 
} 

的hello.jsp:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" 
pageEncoding="ISO-8859-1"%> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 
    <title>Hello</title> 
</head> 
<body> 
<p>Hello</p> 
</body> 
</html> 

更新:每个请求添加错误消息。

错误404 - 未找到从RFC 2068超文本传输​​协议 - HTTP/1.1: 10.4.5 404未找到

服务器没有找到任何匹配的Request-URI。否 指示条件是暂时的还是 永久性的。

如果服务器不希望将此信息提供给客户端 ,则可以使用状态码403(禁止)。如果服务器通过 知道一些内部可配置的机制,旧资源 永久不可用并且没有转发地址,则应使用410(Gone)状态代码。

+0

你用''试过了吗? – 2012-03-21 14:32:42

+0

我之前在那里没有运气,我只是把它放回到applicationContext.xml中,没有运气 – Jason 2012-03-21 14:36:55

+0

在应用程序启动时你看到了吗?事端如: 'INFO:org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping - 映射的URL路径[/ hello/*]到处理程序'helloController''上? – vacuum 2012-03-21 15:19:00

回答

7

这个问题(在web.xml):

<url-pattern>/*</url-pattern> 

这将重定向所有请求到春天的servlet,包括从控制器到JSP您的要求。实质上,控制流程将从您的控制器循环回到Spring。您需要缩小范围,以便JSP的请求直接发送到底层容器,而不是Spring。

尝试将其更改为

<url-pattern>/app*</url-pattern> 

,然后再试一次。你可能需要用前导和尾随来弄清楚它是否工作(例如<url-pattern>/app*</url-pattern>@RequestMapping("hello")等)

+0

仍然没有任何运气。在这一点上,我想知道Spring是否正在拿起控制器类。 – Jason 2012-03-21 14:52:40

+0

@Jason:向我们展示404页面。它的风格给出了什么没有找到的线索。 – skaffman 2012-03-21 15:04:47

+0

已被添加到原始信息 – Jason 2012-03-21 15:11:21

0

正如你在评论中提到的,日志不会给你信息,Controller映射到特定的URL。所以我认为箴是在控制器中。

请确保控制器位于“mil.army.retain.web。对注解的配置控制器”包,然后打开:

<context:annotation-config /> 
+0

那是我尝试过的另一件事,没有运气。而且它似乎也忘了编辑我的软件包名称... – Jason 2012-03-21 15:51:39

+0

什么日志说,当你去http:// localhost:nnnn /你好? – vacuum 2012-03-21 15:54:40

+1

Eclipse控制台中的Web服务器日志倾向于说没有找到/app/hello.do的映射。我没有设置Log4J监听器,所以我无法监视应用程序启动以查看弹簧初始化。 – Jason 2012-03-21 16:04:38

1

请检查您的所有控制器类/子包或其他类如你在下面一行都提到同一个包中:

<context:annotation-config /> 
<context:component-scan base-package="com.kfs" /> 
+2

意味着'annotation-config'标记的效果 – shevchyk 2012-10-05 10:44:18