gpt4 book ai didi

java - Spring MVC : Unable to access HTML page via InternalResourceViewResolver

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

我有两个文件index.html和user.jsp,位置是WebContent/WEB-INF/。

此外,我可以使用以下代码访问 user.jsp:

<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/" />
<property name="suffix" value=".jsp" />
</bean>

Controller 代码:

@RequestMapping(value="/usertimepass",method=RequestMethod.GET)
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public ModelAndView time(){

ModelAndView mv=new ModelAndView();
mv.setViewName("user");
System.out.println("returned user");
return mv;
}

但是,我无法使用以下修改后的代码访问index.html:

<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/" />
<property name="suffix" value=".html" />
</bean>

Controller 代码:

@RequestMapping(value="/usertimepass",method=RequestMethod.GET)
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public ModelAndView time(){

ModelAndView mv=new ModelAndView();
mv.setViewName("index");
System.out.println("returned index");
return mv;
}

Web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>SampleApp</display-name>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

<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>

谢谢!!

最佳答案

WEB-INF 中的任何内容都无法从外部访问。您需要创建两个 View 解析器,或者将您的index.html 放在应用程序的根目录下。它的设置方式是其中之一。

关于java - Spring MVC : Unable to access HTML page via InternalResourceViewResolver,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21229839/

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