Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring welcome-file-list correct mapping

I know that in spring I must define welcome-file, which should be outside of WEB-INF folder, so I define it like this:

web.xml:

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


<servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

But actually my real code is in WEB-INF/jsp/contact.jsp

So I always have to do this:

<jsp:forward page="/index"></jsp:forward>

And in my controller this means:

@RequestMapping("/index")
public String listContacts(Map<String, Object> map) {

    map.put("contact", new Contact());
    map.put("contactList", contactService.listContact());

    return "contact";
}

How can I make it this way, that welcome-file always goes to my index mapping, which leads to contact.jsp?

Feel free to ask questions, if this was confusing...

like image 969
Jaanus Avatar asked Sep 14 '11 10:09

Jaanus


People also ask

Which tag specifies the relative path of a single welcome file?

The tag <welcome-file-list> is used for specifying the files that needs to be invoked by server by default, if you do not specify a file name while loading the project on browser. Based on the welcome file list, server would look for the myhome.

What file specifies the default welcome page?

The server looks for the welcome file in the following sequence by default: welcome-file-list in web. xml.

Which element of the deployment descriptor of a Web application includes the welcome file list element as a subelement?

The welcome-file-list element of web-app, is used to define a list of welcome files. Its sub element is welcome-file that is used to define the welcome file.

What do you understand by servlet mapping?

Servlet mapping specifies the web container of which java servlet should be invoked for a url given by client. It maps url patterns to servlets. When there is a request from a client, servlet container decides to which application it should forward to. Then context path of url is matched for mapping servlets.


2 Answers

@RequestMapping({"/index", "/"})

and

<welcome-file-list>
    <welcome-file></welcome-file>
</welcome-file-list>

worked for me.

like image 184
Bozho Avatar answered Oct 07 '22 19:10

Bozho


See my answer: https://stackoverflow.com/a/15551678/173149 or just:

<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
    <url-pattern>/index.htm</url-pattern>    <<==  *1*
</servlet-mapping>
<welcome-file-list>
    <welcome-file>index.htm</welcome-file>   <<== *2*
</welcome-file-list>
like image 42
gavenkoa Avatar answered Oct 07 '22 18:10

gavenkoa