Subho
Subho

Reputation: 923

Requested resource is not available in spring MVC

I am new to spring MVC,i am trying to deploy a hello world application in it.But I am always getting a requested resource not available error on the jsp page.I am using tomcat 7. Here I am pasting my code anyone please help..

web.xml

<?xml version="1.0" encoding="UTF-8"?>
 <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"
id="WebApp_ID" version="3.0">

<display-name>HelloWorldSpring</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>
    <load-on-startup>1</load-on-startup>
</servlet>

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

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


<context:component-scan
    base-package="net.viralpatel.spring3.controller" />

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

Controller of the application

package net.viralpatel.spring3.controller;

import javax.servlet.http.HttpServletRequest;

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

@Controller
public class HelloWorldController {

    @RequestMapping("/hello")
    public ModelAndView helloWorld() {
        String message = "Hello World, Spring 3.0!";
        System.out.println(message);
        return new ModelAndView("hello", "message", message);
    }

}

hello.jsp

<html>
<head>
<title>Spring 3.0 MVC </title>
</head>
<body>
${message}
</body>
</html>

index.jsp

<html>
<head>

</head>
<body>
<a href="hello">Say Hello</a>
</body>
</html>

this is my application and I am also adding screenshot of my project structure..enter image description here

Upvotes: 14

Views: 52081

Answers (7)

pushpendra yadav
pushpendra yadav

Reputation: 939

I felt the same issue. I added the jstl 1.2.jar and it worked for me.

Upvotes: 0

Ajay
Ajay

Reputation: 458

Well i too faced this problem while following this tutorial and code above seems to be from this tutorial also,so Adding spring-webmvc-x-y-z.RELEASE.jar, and removing "asm" and "web.servlet" jars worked for me. Hope this would help someone learning spring.

Upvotes: 0

Lakshmi Murali
Lakshmi Murali

Reputation: 1

Change the following in the web.xml

<servlet>

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

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

Upvotes: 0

Witold Kaczurba
Witold Kaczurba

Reputation: 10485

I had a similar problem and I believe it was Maven-related. What I did is I changed jstl dependency from jstl:jstl:1.2 to javax.servlet:jstl:1.2 . I do not believe that this was the reason but after Maven updated its dependencies - it started working. Then it made no difference which one I used. I think it had to do with libs being not in the right place...

enter image description here

Upvotes: 0

Fenil
Fenil

Reputation: 1

Once check your folder structure.. all spring jars should be copied in lib folder that is in Web-INF and import from there. That should solve your issue

Upvotes: 0

Rohan
Rohan

Reputation: 3078

The main problem was with <url-pattern>*.html</url-pattern>.
I did following changes in your code and i am able to run the same code on my machine :

1) changed <url-pattern>*.html</url-pattern> to <url-pattern>/</url-pattern>
2) copied jstl-1.2.jar in lib folder.

Upvotes: 11

Ashish Jagtap
Ashish Jagtap

Reputation: 2819

Just change your controller as follows

@Controller
    @RequestMapping("/hello")
    public class HelloWorldController {

    @RequestMapping(method = RequestMethod.GET)
    public String helloWorld(ModelMap model, HttpServletRequest request) {
        String message = "Hello World, Spring 3.0!";
        System.out.println(message);
        model.addAttribute("message", message);
        return "hello";
    }
    }

hope this will solve your problem

Upvotes: 2

Related Questions