gpt4 book ai didi

java - InternalViewResolver 在 Spring 中不工作

转载 作者:行者123 更新时间:2023-12-01 13:08:35 25 4
gpt4 key购买 nike

我创建了一个测试 Spring MVC 项目。这里 Controller 正在执行,但稍后内部 View 解析器无法转换逻辑名称,因为当我点击 URL 时,我得到“HTTP 状态 404 - 请求的资源 () 不可用”。在浏览器上,但 Controller 内的 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 代码:

@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

最佳答案

问题出在 web.xml 中,将 appservlet URL 模式从/* 更改为/

应用程序Servlet/

虽然这个问题已经解决,但实际上我不明白根本原因,因为无论如何以前 Controller 也被调用

关于java - InternalViewResolver 在 Spring 中不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23079511/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com