我已经创建了一个测试Spring MVC项目。这里控制器正在执行但后来内部视图解析器无法转换逻辑名称,因为当我点击URL时,我得到“HTTP状态404-请求的资源()不可用。”在浏览器上,但控制器内的SOP正在执行。
Web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:applicationContext.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>spring/mvc/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<!-- <welcome-file-list>
<welcome-file>/WEB-INF/views/Welcome.jsp</welcome-file>
</welcome-file-list>
<session-config>
<session-timeout>60</session-timeout>
</session-config> -->
</web-app>
控制器代码:
@Controller
public class WelcomeController {
@RequestMapping(value = "/Welcome")
public String displaywelcomePage(Model model, HttpServletRequest request)
{
System.out.println("Into the controller");
return "Welcome";
}
servlet的context.xml中
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">
<annotation-driven />
<context:component-scan base-package="com.springmvc.prac.springmvcprac" />
<resources mapping="/resources/**" location="/resources/" />
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/view/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
</beans:beans>
servlet-context.xml位于webapp / spring / mvc
中Welcome.jsp位于webapp / WEB-INF / view
中答案 0 :(得分:1)
问题出在web.xml中,用于将appservlet URL模式从/ *更改为/
appServlet /
虽然这个问题已经解决但实际上我并没有理解根本原因,因为无论如何以前控制器也被称为
答案 1 :(得分:0)
您的配置缺少视图类。变化
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/view/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
到
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<beans:property name="prefix" value="/WEB-INF/view/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>