1

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

Touchstone
  • 5,575
  • 7
  • 41
  • 48
  • Could you please tell me the full path of ModelAndView class which you are using with complete package name? – Mudit Shukla Jan 20 '14 at 09:28
  • are you using the following class: import org.springframework.web.servlet.ModelAndView; – Mudit Shukla Jan 20 '14 at 09:40
  • yes, i am using org.springframework.web.servlet.ModelAndView – Touchstone Jan 20 '14 at 09:47
  • When you say you're unable to access `index.html` - what do you get? – Will Keeling Jan 20 '14 at 09:56
  • @Will HTTP status 404, and WARNING: No mapping found for HTTP request with URI [/TermBreak/WEB-INF/index.html] in DispatcherServlet with name 'dispatcher' – Touchstone Jan 20 '14 at 09:59
  • I tested the same scenario on my machine it worked. Please check you must be missing something either html file is not at correct place or name might be not correct. – Mudit Shukla Jan 20 '14 at 10:03
  • @Mudit the location of both the files i.e index.html & user.jsp are WEB-INF/ – Touchstone Jan 20 '14 at 10:08
  • Can you post your `web.xml`? – Will Keeling Jan 20 '14 at 10:18
  • So just to confirm, using the revamped code, when you make a request for `http://hostname/TermBreak/usertimepass` - you see '404 no mapping for /TermBreak/WEB-INF/index.html'? – Will Keeling Jan 20 '14 at 10:33
  • I see HTTP status 404 in the browser, and WARNING: No mapping found for HTTP request with URI [/TermBreak/WEB-INF/index.html] in DispatcherServlet with name 'dispatcher' in the console. – Touchstone Jan 20 '14 at 10:45
  • Please refer http://stackoverflow.com/questions/15479213/how-to-serve-html-files-with-spring, it is a similar question with an accepted answer. – Touchstone Jan 20 '14 at 18:38

2 Answers2

1

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.

Marty
  • 174
  • 1
  • 9
0

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

tshenolo
  • 463
  • 2
  • 9