【发布时间】:2014-10-06 03:41:32
【问题描述】:
这是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
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”
可以看到页面,但是控制器没有执行,因为这个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>
【问题讨论】:
-
您是否尝试将方法类型置于方法级别,即 GET/POST?
-
请问您也可以发布调度程序弹簧配置吗?以及使用的spring版本?
-
@Neeraj 我试过
method = RequestMethod.GET,但它不起作用。 -
@KevinBayes 我添加了applicationContext.xml和dispatcher-servlet.xml,这些文件是Intellij生成的,Spring版本是“Spring MVC-4.1.1.RELEASE”
标签: java spring spring-mvc intellij-idea