Question

I am new to SPring 3 MVC and trying to configure a java based Spring 3 MVC application. I am using servlet 2.5 container for my application. Below is what I tried.

Below is my Maven Based project structure:

enter image description here

web.xml

<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
      http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"  
version="2.5"> 

<display-name>Archetype Created Web Application</display-name>
<welcome-file-list>  
  <welcome-file>index.jsp</welcome-file>  
</welcome-file-list>

<servlet>
    <servlet-name>spring</servlet-name>
    <servlet-class>
        org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <init-param>
       <param-name>contextClass</param-name>
       <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
    </init-param>
    <init-param>
       <param-name>contextConfigLocation</param-name>
       <param-value>com.sprmvc.init.WebAppConfig</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>*.htm</url-pattern>
</servlet-mapping>

</web-app>

WebAppConfig.java

@Configuration //Specifies the class as configuration  
@ComponentScan("com.sprmvc") //Specifies which package to scan  
@EnableWebMvc //Enables to use Spring's annotations in the code  
public class WebAppConfig {

@Bean
public UrlBasedViewResolver setupViewResolver() {  
    UrlBasedViewResolver resolver = new UrlBasedViewResolver();  
    resolver.setPrefix("/WEB-INF/pages/");  
    resolver.setSuffix(".jsp");  
    resolver.setViewClass(JstlView.class);  
    return resolver;
}

}

LinkController.java

@Controller
public class LinkController {

@RequestMapping(value="/hello")  
public ModelAndView goToHelloPage() {  
    ModelAndView view = new ModelAndView();  
    view.setViewName("hello"); //name of the jsp-file in the "page" folder  

    String str = "MVC Spring is here!";  
    view.addObject("message", str); //adding of str object as "message" parameter  

    return view;  
} 

}

index.jsp

<html>
<body>
<h1>Home page</h1>  
<p>This is a Home Page.</p>  
<p><a href="hello.html">Hello world link</a></p>  
</body>
</html>

hello.jsp

<html>
<body>
<p>Hello world: ${message}</p>  
<p>Well done!</p>
</body>
</html>

When I run the application, index.jsp is getting displayed properly. But on click of the link in index.jsp page I am getting an 404.

The URL triggered on clicking the link of index.jsp page is http://localhost:8055/SpringMVCConfig/hello.html and since the resource is not available, hence this 404 error.

There is some mistake in my java based configuration as a result the prefix and suffix are not working. Please advise.

Was it helpful?

Solution

you have maaped your controller with the value "/hello" but inside index.jsp you have

used href="hello.html".

so it wont map, all your mapping config is fine.

change in your index.jsp the href value to just hello.

index.jsp

<html>
<body>
<h1>Home page</h1>  
<p>This is a Home Page.</p>  
<p><a href="hello">Hello world link</a></p>  
</body>
</html>

also change url pattern in web.xml to map any request like below(/ instead of *.htm)

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

in case you want to use the same *.html and /hello.html for your example, you need to do redirect:url in your controller.

also make sure *.htm and *.html are different.

use like below

@RequestMapping("/hello.html")
public ModelAndView processForm(HttpServlet request, HttpServletResponse response){
    //process form data etc
    ModelAndView modelAndView = new ModelAndView("redirect:hello");                
    Map<Object, Object> model = modelAndView.getModel();
    modelAndView.addObject("error", "this.is.my.error.code");
    return modelAndView;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top