0

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 Answers 3

2

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.

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

7 Comments

Cheers, i should have added this to the variations section however. I changed that prefix value when I attempted to move the .jsp files out of the web-inf folder. I should have re-added it, this does not resolve my issue however. Thanks.
Sorry for the late reply, but i managed to make it work on my workspace, see my edit.
Hi, thanks for your efforts. I've transplanted your changes into my workspace identically and I'm still receiving the same errors. So a warning stating the HTTP request with URI is not mapped. The servlet class is still correctly being invoked.
how are you invoking the controller? paste the link please.
localhost:8080/Root .but ive tried numerous combinations. I.E /Root/index etc
|
1

In LoginServlet-servlet.xml file try

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

instead of

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

1 Comment

Cheers, i should have added this to the variations section however. I changed that prefix value when I attempted to move the .jsp files out of the web-inf folder. I should have re-added it, this does not resolve my issue however. Thanks.
1

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

3 Comments

Thanks, I've made these changes to my current version, although its 100% been in that state before and its still behaving exactly the same, I'll update my question to include these changes.
I just edited the LoginServlet.java. The cause of your problem is that there is no request mapping for the login page which I have now included.
With the approach i used in my answer, you don't need that handleRequestInternal method. So please delete it.

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.