Rufi
Rufi

Reputation: 2645

Spring MVC and JSP. Basic steps

I am starting with Spring MVC, but something is going wrong because I am getting 404 error with the message "The requested resource is not available." Searching for few hours and trying different things didn't help, so I decided to ask help from you guys.

I have an index.jsp where I want to have a link which will invoke the method in my controller and will return hello.jsp.

index.jsp:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title></title>
</head>
<body>
    <a href="${"test"}">Click to press hello</a>
</body>
</html>

By clicking to this link I assume that I should come to my MessageController:

package main.java.service;

//imports...

@Controller
@RequestMapping(value = "/test")
public class MessageController {

    @RequestMapping(method = RequestMethod.GET)
    public String getThis(Map<String, String> map) {
        map.put("message", "Hello World!");
        return "hello";
    }
}

According to my understanding it should happen because I marked the package of my controller is including in servlet-config.xml

<?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:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/mvc             http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
       http://www.springframework.org/schema/beans          http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">

   <mvc:annotation-driven />
   <context:component-scan base-package="main.java.service"/>

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

And then finally there is a web.xml

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

    <display-name>SpringWebConfiguration</display-name>

    <servlet>
        <servlet-name>spring-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>spring-dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/servlet-config.xml</param-value>
    </context-param>
</web-app>

So, according to that I should get a hello.jsp file with the message Hello World! but instead I get a 404 error with the message "The requested resource is not available." Would be nice if someone could find an issue here.

Here is my hello.jsp

<%@ page language="java" contentType="text/html; charset=US-ASCII" pageEncoding="US-ASCII"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
  <title>Title</title>
</head>
<body>Here there is a message: ${message}</body>
</html>

This is the working repo: https://github.com/vladt89/fsboard

Upvotes: 0

Views: 163

Answers (4)

Saif
Saif

Reputation: 7042

change your web.xml as

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

<display-name>SpringWebConfiguration</display-name>

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

or you can just ignore <init-param> by:

Renaming your servlet-config.xml to spring-dispatcher-servlet.xml
when <init-param> is not present Name of the Spring configuration file should be same as the servlet-name which used in web.xml with a tailing -servlet.

And definitely you need to put your hello.jsp inside WEB-INF folder as you have defined in the view resolver as

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

so it will look for the jsp files inside the WEB-INF folder


Update : I have fork your code and tested in my environment.

The Code which causing this problem you haven't post in you question.... too bad.
Add @Component in both in your MessageService and MessageServiceImpl

@Component
public interface MessageService {

    void createMessage(String message);

    Collection<String> fetchAllMessages();
}

and

@Component
public class MessageServiceImpl implements MessageService {
    private Collection<String> messageLibrary = new LinkedList<>();

    @Override
    public void createMessage(String message) {
        messageLibrary.add(message);
    }

    @Override
    public Collection<String> fetchAllMessages() {
        return messageLibrary;
    }
}

Upvotes: 1

Bharath
Bharath

Reputation: 259

I am adding suggestions from Annamalai Thangaraj and Sotirios Delimanolis.

  1. You need to initialize the context in web.xml.
  2. You need to place the hello.jsp in WEB-INF folder. This is because the view resolver you have configured is looking for the hello.jsp in WEB-INF

Upvotes: 0

Bharath
Bharath

Reputation: 259

in index.jsp your link seems to be bad. you may want to change the anchor tag to:

<a href="test">Click to press hello</a>

Upvotes: 0

Annamalai Thangaraj
Annamalai Thangaraj

Reputation: 532

Check your url in href reaches your controller and Is hello.jsp available inside WEB-INF folder?

Upvotes: 1

Related Questions