17

I want to set one of jsp files in my jsps folder as the default view for the application. Is there any way we can tell in <welcome-file-list> that abc.jsp needs to be default and it can be found in such and such path. Also the url pattern is html so is there a way it can be mapped in Spring MVC.

For example - When a user types www.example.com , I want the application to direct to abc.jsp page and also when someone types www.example.com/something, even then application should direct to abc.jsp, but the url pattern shouldnt be compromised.

4 Answers 4

34

Add

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

to the config file. Then the ROOT will resolve to the abc view. Then add

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

to the config file. This will resolve the view to /WEB-INF/view/abc.jsp.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks Rpond. I am trying to use the solution you mentioned. I guess <mvc:view-controller path="/" view-name="abc"/> this is on spring security config file right ?
no, thats a new configuration with spring 3.0. should go in your spring config files.
I want to use this approach but I don't know which is the spring config files. Is that the app-servlet.xml?
Can we achieve that same in spring-boot by configuring in application.properties?
17

For example - When a user types www.example.com , I want the application to direct to abc.jsp page

Configure it as a <welcome-file> in the web.xml like follows:

<welcome-file-list>
    <welcome-file>/abc.jsp</welcome-file>
</welcome-file-list>

and also when someone types www.example.com/something, even then application should direct to abc.jsp, but the url pattern shouldnt be compromised.

In other words, you'd like to forward non-existing resources (which thus would result in a HTTP 404 Page Not Found error) to the same file? Then define it as an <error-page> in the web.xml as well:

<error-page>
    <error-code>404</error-code>
    <location>/abc.jsp</location>
</error-page>

But your question is actually a bit ambiguous. If you actually didn't mean the above and actually want to use /abc.jsp as the "page controller", then you need to define it as a <servlet> in web.xml instead:

<servlet>
    <servlet-name>controller</servlet-name>
    <jsp-file>/abc.jsp</jsp-file>
</servlet>
<servlet-mapping>
    <servlet-name>controller</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

(both the <welcome-file> and the <error-page> are not needed here)

This is however a flaw in the MVC design (using a view as controller). But if you're actually asking for it..

4 Comments

Thank you BalusC. That <error-page> was the mechanism I was looking for. Thanks again.
I tried <welcome-file-list> <welcome-file>/abc.jsp</welcome-file> </welcome-file-list> but this doesnot initiate the bean associated in servlet-context.xml and using /abc.html in welcome-file-list , doesnot help either
abc.html? You need JSP here. HTML knows nothing about <jsp:useBean> and that kind of things :)
your solution does not help. abc.jsp in welcome-file doesnot invoke the bean associated with it like I said before. And abc.html that I specified in welcome list was because my url pattern is .html.
2

Solved -

A file in the welcome file list has to be a real file , hence abc.htm did not work. The only way to accomplish was to create an index.jsp and redirect it using

<jsp:forward page="abc.html" />

also in yourapplication-servlet.xml you need to specify the bean mapping like

<bean name="/abc.html" class="package.mypack.someController">  
    <property name="commandClass" value="package.mypack..something"/>
    <property name="formView" value="abc"/>
    <property name="successView" value="abc.htm"/>
</bean>  

A welcome-file has to be a REAL file on the file system it doesn't work with mapped urls. You can create an index.jsp which redirects to the mapped url, but it has to be an actual file. Nothing to do with spring it is stated in the servlet spec that it has to be this way, although behavior might vary on different app servers in general it has to be a real file.

I hope this help someone

2 Comments

Yes, you're right. Less or more. It doesn't necessarily have to point to that file though. Just <welcome-file>/abc.html</welcome-file> and an empty abc.html file would have worked as good.
This helped me with my issue. @Walker Your redirection will never be called. I just used the empty file technique that BalusC mentioned, it gets redirected by Spring before it ever reachs your physical file.
0

This all behavior can be resolved by adding

<mvc:default-servlet-handler>

to your spring ../WEB-INF/dispatcher-servlet.xml
This will arrange it all for you.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.