简体   繁体   中英

Spring MVC: Unable to access HTML page via InternalResourceViewResolver

I have two files index.html and user.jsp, there location is WebContent/WEB-INF/.

In addition, i am able to access user.jsp with the following code:

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

Controller code:

@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;
}

However, i am unable to access index.html with the following revamped code:

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

Controller code:

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

Thanks!!

Anything in WEB-INF isn't accessible from the outside. You either need to create two view resolvers or put your index.html at the root of the application. The way it's setup is one or the other.

Try this:

Create the folder: /WEB-INF/pages/ and put you jsp files into it eg. user.jsp

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

    ModelAndView mv = new ModelAndView("user");
    return mv;
}

web.xml

<servlet-mapping>
    <servlet-name>mvc-dispatcher</servlet-name>
    <url-pattern>*.htm</url-pattern>
  </servlet-mapping>

    <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext.xml</param-value>
  </context-param>

applicationContext.xml

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

Access your view at PROJECTNAME/usertimepass.htm

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM