Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring 3: map page.html to page.jsp

I'm using Spring 3 and i don't know how to map somepage.htm to somepage.jsp without a controller. That is: if i go to somepage.htm, i want it to show me the jsp. But of course without redirect. I dontw want anyone to see ".jsp" only ".htm"

 <servlet>
    <servlet-name>Training01</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
 </servlet>

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

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

like image 229
AdrianS Avatar asked May 20 '11 08:05

AdrianS


1 Answers

The way to do is to use the <mvc:view-controller..> tag in combination with a view resolver.

See here for more documentation:

The <mvc:view-controller..> tag maps urls to views. So if you want to map the relative url /login to a view names login you would do it by adding the following line to you webmvc-context.xml file:

<mvc:view-controller path="/login" view-name="login" />

Of course to get this to work you'll have to have a view resolve - something that maps logic names to specific views - setup in your context. In your case since you are using straight jsps for you view layer you'll want to add something like this to your configuration:

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

So with this setup if you had a jsp login.jsp located in you /WEB-INF/jsp directory then you would be able to directly reference that jsp from the url www.myapp.com/mycontenxtroot/login

See here for some more info on view resolvers:

like image 177
Karthik Ramachandran Avatar answered Sep 23 '22 15:09

Karthik Ramachandran