Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 347
Subho Avatar asked Dec 23 '13 11:12

Subho


2 Answers

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.

like image 73
Rohan Avatar answered Sep 28 '22 02:09

Rohan


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

like image 44
Ashish Jagtap Avatar answered Sep 28 '22 01:09

Ashish Jagtap