blitzqwe
blitzqwe

Reputation: 2040

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>

Upvotes: 5

Views: 4329

Answers (2)

Karthik Ramachandran
Karthik Ramachandran

Reputation: 12175

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:

Upvotes: 5

Anonymoose
Anonymoose

Reputation: 5992

You might be interested in UrlRewriteFilter. This is the approach I would recommend. If you're serious about clean URLs you'll likely need it at some point anyway.

On the other hand, if it's a one-off, a minimal controller might be easier:

@Controller
public class Somepage {
    @RequestMapping("/somepage")
    public String handler() {
        return "somepage.jsp";
    }
}

Upvotes: 0

Related Questions