Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/358.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring MVC找不到URI映射_Java_Spring_Spring Mvc_Intellij Idea - Fatal编程技术网

Java Spring MVC找不到URI映射

Java Spring MVC找不到URI映射,java,spring,spring-mvc,intellij-idea,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

这是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>
在Intellij中创建新项目时,我选中了这些选项

Java EE > Web Application
    > Spring MVC
    > Hibernate
我无法正确访问该页面

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

我可以看到这个页面,但是控制器没有执行,因为这个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>

dispatcher-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 load。 更改dispatcher-servlet.xml并添加此内容。 这里的重要部分是
上下文:组件扫描
,它告诉Spring在包中查找带注释的类

这是参考资料。



您是否尝试将方法类型置于方法级别,即GET/POST?是否也可以发布dispatcher spring配置?使用的spring版本?@Neeraj我尝试了
method=RequestMethod.GET
,但它不起作用。@KevinBayes我添加了applicationContext.xml和dispatcher-servlet.xml,这些文件由Intellij生成,spring版本是“spring MVC-4.1.1.RELEASE”
<?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>
<?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>