macj7
macj7

Reputation: 21

Why won't my spring mvc views load?

I am new to spring mvc. I am currently using netbeans 8.0.2. along with a spring mvc plugin. From the jar files I see under my 'libraries' section of the project it seems I am using spring mvc 4.0.1.

I am able to load the index.jsp page from tomcat server or even glassfish, but if i should create another .jsp file under the directory structure WEB-INFO>jsp>response.jsp as an example. I get an error 404 from the server. This is probably a ridiculously simple fix, but no success so far.

I suspect it has something to do with my controller and the [springapp]-servlet.xml file. Most of the examples I've seen on the web use spring mvc 3.x and avoid using the annotation method in controller setup which at first glance the latter seems easier to me. These examples then proceed to add a few lines to the aforementioned xml file. However, I thought that by using the annotation controller setup that spring mvc would automatically detect my controller(s). Is using the annotation method not recommended?

My configuration files are as follows:

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>springapp</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>2</load-on-startup>

    </servlet>
    <servlet-mapping>
        <servlet-name>springapp</servlet-name>
        <url-pattern>*.htm</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>redirect.jsp</welcome-file>
    </welcome-file-list>
</web-app>

springapp-servlet.xml:

<?xml version='1.0' encoding='UTF-8' ?>
<!-- was: <?xml version="1.0" encoding="UTF-8"?> -->
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="
                            http://www.springframework.org/schema/mvc 
                            http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
                            http://www.springframework.org/schema/context 
                            http://www.springframework.org/schema/context/spring-context-40.xsd
                            http://www.springframework.org/schema/beans
                            http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
                            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
                            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">


    <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>


    <!--
    Most controllers will use the ControllerClassNameHandlerMapping above, but
    for the index controller we are using ParameterizableViewController, so we must
    define an explicit mapping for it.
    -->
    <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <props>
                <prop key="index.htm">indexController</prop>
            </props>
        </property>
    </bean>


    <bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver"
          p:prefix="/WEB-INF/jsp/"
          p:suffix=".jsp" />

    <!--
    The index controller.
    -->
    <bean name="indexController"
          class="org.springframework.web.servlet.mvc.ParameterizableViewController"
          p:viewName="index" />



</beans>

Other relevant files:

controller:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
//import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
//import org.springframework.web.bind.annotation.RequestParam;

/**
 *
 * @author macj7
 */

@Controller
public class HelloController {


    @RequestMapping( value = "/response", method = RequestMethod.GET)
    public String index( Model model) {
        String name = "World";
        model.addAttribute("name", name );
        return "response";
    }

}

response.jsp:

<%-- 
    Document   : response
    Created on : Jul 31, 2015, 1:25:19 PM
    Author     : macj7
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Spring Web MVC </title>
    </head>
    <body>
        <h1> th:text= "Hello, ${name}!" </h1>
    </body>
</html>

redirect.jsp:

<%--
Views should be stored under the WEB-INF folder so that
they are not accessible except through controller process.

This JSP is here to provide a redirect to the dispatcher
servlet but should be the only JSP outside of WEB-INF.
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<% response.sendRedirect("index.htm"); %>

If there is a solution to this issue or more information is needed please let me know. Also I would like an explanation of any answers given as I am trying to learn and not just copy and paste a solution. This is my first attempt at creating a web application with java, and I must say the configuration alone seems rather daunting.

Thanks in advance for your time and patience.

Upvotes: 1

Views: 1027

Answers (3)

macj7
macj7

Reputation: 21

Okay, so the solution I found was to define the controller that would be handling the request in springapp-servlet.xml as a bean, and also redefining the method in the controller itself so that it returns a ModelAndView (import org.springframework.web.servlet.ModelAndView;). My pages now render.

I should note that I have rewritten the model without using the annotation form for writing controllers, but I will soon test it in that form also. For now I have simply implemented the Controller Interface.

Thanks to all of those who responded. Your input appreciated.

Upvotes: 1

Braj
Braj

Reputation: 46881

Root cause:

  1. As per web.xml the DispatcherServlet looks for url that ends with *.htm that what it's not accepting request for response.jsp

Note:

Load DispatcherServlet on start up as priority 1

<load-on-startup>1</load-on-startup>

Solution:

Change the mapping for DispatcherServlet that will inspect for each request.

<servlet-mapping>
    <servlet-name>springapp</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

To access the response.jsp you need to type in url www.domain/context-root/response that maps as per RequestMapping defined in controller.

Read more about Web MVC framework - The DispatcherServlet

Upvotes: 0

Sachin Tanna
Sachin Tanna

Reputation: 46

I think in welcome file list of your web.xml file, you should provide your Spring Controller Request Mapping instead of jsp file name as shown below.

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

Upvotes: 1

Related Questions