Spring welcome-file-list correct mapping
Asked Answered
H

5

9

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

Hyrcania answered 14/9, 2011 at 10:48 Comment(0)
C
22
@RequestMapping({"/index", "/"})

and

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

worked for me.

Cathern answered 14/9, 2011 at 11:0 Comment(2)
No mapping found for HTTP request with URI [/web/] in DispatcherServlet with name 'spring', updated first post(added servlet-mapping in web.xml).Hyrcania
It worked, thanks! Is it reasonable thing to do, or I should usually have welcome-file?Hyrcania
C
4

See my answer: https://mcmap.net/q/1170396/-tomcat-6-can-welcome-page-be-inside-web-inf 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>
Catechin answered 22/3, 2013 at 12:46 Comment(0)
R
3

In case of java configuration you can override two methods in class that extends WebMvcConfigurerAdapter

@Override
public void addViewControllers(ViewControllerRegistry registry) {
    registry.addViewController("/").setViewName("/index");
}

@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
    configurer.enable();
}

If you wanna serve index.html explicitly, turn it into a resource override a method in the same class as below:

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/index.html").addResourceLocations("/WEB-INF/views/index.html");
}

Of course addResourceLocations must follows the folder choosen to hold your views.

See these samples

Rural answered 4/5, 2014 at 16:32 Comment(0)
P
0

Try using

<welcome-file-list>
  <welcome-file>/index</welcome-file>
</welcome-file-list>
Pontic answered 14/9, 2011 at 11:9 Comment(1)
No mapping found for HTTP request with URI [/web/] in DispatcherServlet with name 'spring', updated first post(added servlet-mapping in web.xml).Hyrcania
Q
0

You can achieve your workflow as below:

Basically your web application will look into welcome page and /redirect is fired which is captured by controller and execute the logic;

In web.xml add the following code and drop the index.jsp in webapp directory.

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

In index.jsp , add below code:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<body>
<c:redirect url="/redirect"/>
</body>
</html>

Finally in the Controller , you could add the code below:

@Controller
public class HomePageController {

    @RequestMapping(value = "/redirect", method = RequestMethod.GET)
    public String myMethod(HttpServletRequest servletRequest, HttpServletResponse servletResponse) throws IOException {
       // add your logic here
     return "results"; // return results.jsp if you need; 
    }
}
Quodlibet answered 6/5, 2023 at 5:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.