没有找到带有URI的HTTP请求的映射

时间:2014-08-26 15:49:17

标签: java spring-mvc

我最近开始学习spring mvc并且我在开始时遇到困难。 我有一个新手问题,但我还没有找到任何解决方案,或者在其他世界中;没有找到解决方案。 所以,我尝试制作我的第一个简单的spring mvc应用程序,在点击链接后打印一些文本,但是当我运行应用程序时,我得到http 404和glassfish日志; No mapping found for HTTP request with URI [/testApp/] in DispatcherServlet with name 'example'

我认为我搞砸了配置,但我无法弄清楚是什么; / 这是我的web.xml:

<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">

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

<servlet-mapping>
    <servlet-name>example</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

例如-servlet.xml中;

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
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="controller" />

<bean id="viewResolver"
    class="org.springframework.web.servlet.view.UrlBasedViewResolver">
    <property name="viewClass"
        value="org.springframework.web.servlet.view.JstlView" />
    <property name="prefix" value="/WEB-INF/jsp/" />
    <property name="suffix" value=".jsp" />
</bean>

和HelloWorldController类;

@Controller
public class HelloWorldController {

    @RequestMapping("/hello")
    public ModelAndView helloWorld() {
        String message = "Hello world spring";
        return new ModelAndView("hello", "message", message);
    }
}

hello.jsp内有WEB-INF/jspindex.jsp内有web.xmlexample-servlet.xmlWEB-INF。 我非常感谢任何帮助。 谢谢你提前。 @编辑: 请求映射的工作方式类似于魅力,但仍然欢迎页面无效。 我的项目结构如下所示:

-webapp
-WEB-INF
--jsp
---hello.jsp
--example-servlet.xml
--web.xml
-index.jsp

TL;博士 我在index.jsp目录中有我的欢迎页面webapp,并在WEB-INFWEB-INF/jsp中休息 我已将welcome-page添加到web.xml:

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

仍然在输入http://localhost:8080/testApp/后获取http 404。 @编辑: 当我请求http://localhost:8080/testApp/index.jsp时,一切正常。

2 个答案:

答案 0 :(得分:2)

你错过了

<mvc:annotation-driven />

及其上下文配置中的相应命名空间声明。没有它,Spring就不会注册你的控制器(除其他外)。

此外,您的控制器处理程序方法已映射到

@RequestMapping("/hello")

所以请求应该转到

/your-context-path/hello

答案 1 :(得分:0)

如果有人在解决我的问题(欢迎页面没有显示),我找到了一些解决方案。 如果你有像这样配置的视图解析器:

<bean id="viewResolver"
    class="org.springframework.web.servlet.view.UrlBasedViewResolver">
    <property name="viewClass"
        value="org.springframework.web.servlet.view.JstlView" />
    <property name="prefix" value="/WEB-INF/jsp/" />
    <property name="suffix" value=".jsp" />
</bean>

index.jsp目录中创建WEB-INF/jsp并为其创建控制器类,如下所示:

@Controller
public class IndexController {

    @RequestMapping("/")
    public String index() {
        return "index";
    }
} 

现在如果你调用http://address/yourapp/将映射index.jsp。

相关问题