java - Spring MVC 找不到 URI 映射

标签 java spring spring-mvc intellij-idea

这是web.xml,它应该是正确的,因为它是由Intellij生成的。

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/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>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>*.form</url-pattern>
    </servlet-mapping>
</web-app>

这是 Controller

@Controller
public class IndexController {

    @RequestMapping("/index")
    public String index(ModelMap model) {

        System.out.print("index");
        return "index";
    }
}

当我在 Intellij 中创建新项目时,我检查了这些选项。

Java EE > Web Application
    > Spring MVC
    > Hibernate

我无法正确访问页面

如果我使用“localhost:8080/test”

我可以看到页面,但是 Controller 没有执行,因为这个URL将访问“localhost:8080/test/index.jsp”

如果我使用“localhost:8080/test/index”,结果是 404

如果我使用“localhost:8080/test/index”,结果是404,服务器日志将显示此错误

    WARNING: No mapping found for HTTP request with URI [/test/index.form] in DispatcherServlet with name 'dispatcher'

我将“.form”更改为“/”,但仍然遇到相同的错误。

我的项目还缺少什么?

<小时/>

更新一下,这些是applicationContext.xml和dispatcher-servlet.xml,这些文件也是由Intellij生成的

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       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.xsd">

</beans>

调度程序-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       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.xsd">

</beans>

最佳答案

Spring 未找到您的 IndexController。 您添加了一个 @Controller 但没有告诉 Spring 加载它们。 更改您的dispatcher-servlet.xml 并添加它。 这里重要的部分是 context:component-scan ,它告诉 Spring 在包内查找带注释的类。

这是引用。 http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-ann-controller

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="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.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <context:component-scan base-package="<your controller package here>" />

    <mvc:annotation-driven />
    <mvc:resources mapping="/resources/**" location="/resources/" />

    <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/pages/" />
        <property name="suffix" value=".jsp" />
        <property name="order" value="1" />
    </bean>

</beans>

关于java - Spring MVC 找不到 URI 映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26209743/

相关文章:

java - IntelliJ IDEA Community Edition 中的 Spring 未绑定(bind) @Autowired

spring - 使用 jersey 2 和 Spring 编写 "clean"集成测试 - REST

java - 如何在 Spring MVC Controller 中接收多部分/表单数据?

java - Jackson 忽略抽象类的 xml 根元素

java - 修改calculateFittingCost()方法,以便将可变劳动力成本乘以适当的成本乘数

mysql - 使用 Spring、hibernate 和 C3P0 的设置重现 com.mysql.jdbc.exceptions.jdbc4.CommunicationsException

5亿(双)值的Java数据结构?

java - 在 UI 端处理 ResponseEntity 的最佳方法是什么?

java - 为什么我们需要 ACT_RU_IDENTITYLINK?该表的用途是什么?为什么 ACTIVITI 创建它?

java - 如何计算 libsvm 中的最佳 C/Gamma 参数?