0
votes

I've been experimenting with Java Servlets for web applications, in this application I was able to hit a Servlet and correctly load a .jsp page, having done this I have moved onto Spring MVC. I've run into a problem where my servlet controller class is called, however it will not load the view.

I've ruled out an the resources not being visible, because it worked correctly with a plain java servlet. I've also read just about every resource/tutorial out there in an effort to attempt to identify the problem without any luck, my problem remains the same. In addition in an effort to trouble-shoot I've added an error page tag () in order to see if when I attempt to hit my page, it would correctly redirect me, but its unable to find the page specified for 404 errors.

Can anyone identify what I've missed?

Web.xml

Variations: Changed url-pattern, init-params, context config location etc.

<servlet>
  <servlet-name>LoginServlet</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
 <load-on-startup>1</load-on-startup>
 <init-param>
     <param-name>contextConfigLocation</param-name>
     <param-value>/WEB-INF/LoginServlet-servlet.xml</param-value>
 </init-param>
</servlet>   
<servlet-mapping>
  <servlet-name>LoginServlet</servlet-name>
  <url-pattern>/</url-pattern>
</servlet-mapping>

LoginServlet-servlet.xml

Variations: I've tried moving the declarations into different positions as has been suggested on other posts, to no result. In addition typically I've had the prefix set to /WEB-INF/jsp/

<context:component-scan base-package="plan.route.server" />

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

<context:annotation-config/>
<mvc:annotation-driven />

LoginServlet.java

Variations: Different requestMapping path, marking the methods not the class, returning string from methods, returning ModelAndView class

package plan.route.server;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller()
@RequestMapping("/")
public class LoginServlet extends org.springframework.web.servlet.mvc.AbstractController {

@RequestMapping(method = RequestMethod.GET)
public String forwardTo() {
    return "index";
}

@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response)
        throws Exception {
    return new ModelAndView("login", "login", "login");
}

}

Project setup

Variations: different locations for the servlet xml, .jsp files etc

Project Setup

Can anyone see what I've missed? all I'm trying to do, despite all the variations is load a .jsp page.

Edit: The following error is displayed after my java servlet method is called:

WARNING: No mapping found for HTTP request with URI [/Root/Login] in DispatcherServlet with name 'LoginServlet'
3

3 Answers

2
votes

I see one thing that is wrong and is the jsp configuration in LoginServlet-servlet.xml, try change prefix value as follows:

<context:component-scan base-package="plan.route.server" />

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

<context:annotation-config/>
<mvc:annotation-driven /> 

With your configuration Spring is not able to find jsp file, because you specified the wrong path. You have to be folder specific, in your case you have jsp files in /WEB-INF/jsp folder.

EDIT:

I configured your project in my workspace and it works. Try to remove this lines from web.xml:

<init-param>
     <param-name>contextConfigLocation</param-name>
     <param-value>/WEB-INF/LoginServlet-servlet.xml</param-value>
 </init-param>

And your Controller class should be like this:

@Controller
@RequestMapping("/")
public class LoginServlet{

    @RequestMapping(method = RequestMethod.GET)
    public ModelAndView forwardTo(ModelMap model) {
        return new ModelAndView("login", "login", "login");
    }
}

And pay attention on how you invoke the controller:

http://localhost:8080/Root/

This is the correct way to call the controller because you named your project Root and the controller is listening to "/" path. I used port 8080 because you tagged the question with tomcat, and this is the default tomcat port, if you use another one change it with the one you use.

1
votes

In LoginServlet-servlet.xml file try

<property name="prefix" value="/WEB-INF/jsp/"/>

instead of

<property name="prefix" value="/" />
1
votes

With your current project setup

LoginServlet-servlet.xml

<context:component-scan base-package="plan.route.server" />

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

<context:annotation-config/>
<mvc:annotation-driven />

LoginServlet.java

@Controller
@RequestMapping("/")
public class LoginServlet {

@RequestMapping(method = RequestMethod.GET)
public String forwardTo() {
    return "index";
}

@RequestMapping(value="/login", method = RequestMethod.GET)
public String forwardToLogin() {
    return "login";
}

}

This should work