0

Dear Altruist,

I am trying to run Spring Web MVC HelloWeb project but it always shows "The requested resource is not available" error. Would you please help me regarding this problem?

Project Structure:

enter image description here

web.xml file:

enter image description here

HelloWeb-servlet.xml file:

enter image description here

HelloController.java file:

enter image description here

Amit
  • 1,540
  • 1
  • 15
  • 28
  • Are you getting tomcat/other homepage at `localhost:8080`? Sometimes eclipse wont give you tomcat webapps. – Ramanujan R Feb 14 '17 at 09:19
  • Yes, "localhost:8080" is showing Tomcat homepage properly... – Amit Feb 14 '17 at 09:24
  • "Run Project" means "Run on Server" option? Also what URL is displayed in browser? What happens, after starting tomcat, when you go to `http://localhost:8080/HelloWeb/hello'. ? – Ramanujan R Feb 14 '17 at 09:31
  • Are you using dynamic web project? Is all the libraries present in the class path? I think you should use maven or gradle to build the project. – mirmdasif Feb 14 '17 at 09:32
  • I am trying to run with "localhost:8080/HelloWeb/hello" and it shows that error. And I am creating this project from eclipse >> "Dynamic Web Project". And All Spring libraries are present in /WEB_INF/lib folder. – Amit Feb 14 '17 at 09:45
  • 1
    Add `` in `helloweb-servlet` it will work fine – Prasanna Kumar H A Feb 14 '17 at 11:29
  • Showing compiled error: The prefix "mvc" for element "mvc:annotation-config" is not bound. @PrasannaKumar – Amit Feb 14 '17 at 11:56
  • You need to define the`xmlns:mvc` in xml header with mvc schema uri – Prasanna Kumar H A Feb 14 '17 at 12:31

2 Answers2

0

Based on the error message it seems you do not have your context defined in Tomcat.

Include a file named context.xml, into the META-INF folder, with the content:

<?xml version="1.0" encoding="UTF-8"?>

<Context path="/HelloWeb"/>

From the tomcat documentation:

Tomcat Context Container Documentation

Individual Context elements may be explicitly defined:

In an individual file at /META-INF/context.xml inside the application files. Optionally (based on the Host's copyXML attribute) this may be copied to $CATALINA_BASE/conf/[enginename]/[hostname]/ and renamed to application's base file name plus a ".xml" extension.

Also, return a ModelView object instead of a String to get redirected to your home.jsp page.

@RequestMapping(method = RequestMethod.GET)
protected ModelAndView printHello(HttpServletRequest request,
    HttpServletResponse response) throws Exception {

    ModelAndView model = new ModelAndView("hello");

    return model;
}
alfcope
  • 2,327
  • 2
  • 13
  • 21
  • I have included the context.xml file into META-INF folder but still same error occurred. – Amit Feb 14 '17 at 10:38
  • The implementation of `printHello` is correct. No need to change it into `ModelAndView` pattern. The string, `"hello"` it returns, is not just a string. It's a view name. Here it points to `hello.jsp`. But I don't know what causes the issue. – Ramanujan R Feb 14 '17 at 11:25
  • I have implemented ModelAndView pattern but still same issue. @alfcope – Amit Feb 14 '17 at 11:35
0

Replace your beans tag like this.

 <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="
            http://www.springframework.org/schema/beans     
            http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/mvc 
            http://www.springframework.org/schema/mvc/spring-mvc.xsd
            http://www.springframework.org/schema/context 
            http://www.springframework.org/schema/context/spring-context.xsd">
jagga
  • 163
  • 3
  • 5
  • 18
  • Applied, but still same error... `@Controller public class HelloController{ @RequestMapping(value = "/hello", method = RequestMethod.GET) public ModelAndView printHello(HttpServletRequest request, HttpServletResponse response) throws Exception { ModelAndView model = new ModelAndView("hello"); return model; } }` – Amit Feb 14 '17 at 11:47
  • try adding in helloweb-servlet – jagga Feb 14 '17 at 11:52
  • Showing compiled error: The prefix "mvc" for element "mvc:annotation-config" is not bound. @jagga – Amit Feb 14 '17 at 11:53
  • see the edited answer. replace your code in helloweb-servlet – jagga Feb 14 '17 at 12:05
  • I have replaced my helloweb-servet.xml with your tag and after that it showing another compilation error in eclipse. **"cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'mvc:annotation-config'."** – Amit Feb 14 '17 at 12:09
  • [this question can help you](http://stackoverflow.com/questions/15406231/no-declaration-can-be-found-for-element-mvcannotation-driven) – jagga Feb 14 '17 at 12:17
  • @Amit got the answer?? – jagga Feb 15 '17 at 08:34
  • After replacing beans tag in "helloweb-servet.xml" Now compilation error is gone but still showing "The requested resource is not available" error after running... – Amit Feb 16 '17 at 06:17