I try to run the app on Tomcat but keep getting this error "The requested resource (/testapp/) is not available." - what might be wrong? I guess my XML setup is incorrect but not sure how to fix it.
dispatcher-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:mvc="http://www.springframework.org/schema/mvc"
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/mvc
http://www.springframework.org/schema/mvc/spring-mvc-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="controller" />
<mvc:view-controller path="/" view-name="HelloWorldPage" />
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
web.xml
<?xml version="1.0" encoding="utf-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<display-name>Spring Web MVC Application</display-name>
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
HelloWorldController.java - using Spring annotations.
package controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
#Controller
#RequestMapping("/welcome")
public class HelloWorldController {
#RequestMapping(method = RequestMethod.GET)
public ModelAndView helloWorld() {
ModelAndView model = new ModelAndView("HelloWorldPage");
model.addObject("msg", "hello world");
return model;
}
}
in your web.xml you map spring to *.htm but your controller is mapped to
#RequestMapping("/welcome")
you should map to
#RequestMapping("/welcome.htm")
In case you are not getting any exception on eclipse console, I think you are probably not hitting the right contextRoot of your project, mostly it's the exact name of your application, but sometimes it's different...
If you are not typing the app name wrongly try this.
Click on the project right mouse button click on properties.
Then search for Context.
you are going to find a section called ContextRoot
Use the string in that box to mount your Contextroot like this
localhost:8080/stringFound/
and hit enter in the browser.
Related
I'm starting to learn Spring MVC, but when I run my program, I am getting the following error:
HTTP Status 404 -
type Status report
message
description The requested resource is not available.
Apache Tomcat/8.0.41
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app 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"
version="3.1">
<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>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/springmvc-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
springmvc-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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="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.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache.xsd">
<context:component-scan base-package="com"/>
<mvc:annotation-driven/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
SpringTest.java:
package com;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
#Controller
#RequestMapping("/home")
public class SpringTest {
#RequestMapping(value = "/home",method =RequestMethod.GET)
public String home(){
return "home";
}
}
My project structure is as follows:
I use IntelliJ IDEA to run this procedure. Will the IntelliJ IDEA be the reason for the error I'm getting?
Please to change the order between <context:component-scan base-package="com"/> and <mvc:annotation-driven/> make <mvc:annotation-driven/> the first
You are trying to access ${ContextPath}/home/home, please try to replace your controller by below code:
#Controller
public class SpringTest {
#RequestMapping(value = "/home",method =RequestMethod.GET)
public String home(){
return "home";
}
}
I have been trying to debug it for like hours and even tried to create the same project over and again. I don't know whether it's the issue due to IntelliJ IDEA or something else. I tried to google but couldn't find any solution. I am making a simple spring mvc demo application in intelliJ. A default structure was provided by intelliJ and a view "index.jsp" in web directory.
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<display-name>spring-mvc-demo</display-name>
<!-- Spring MVC Configs -->
<!-- Step 1: Configure Spring MVC Dispatcher Servlet -->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-mvc-demo-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Step 2: Set up URL mapping for Spring MVC Dispatcher Servlet -->
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
spring-mvc-demo-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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
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.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- Step 3: Add support for component scanning -->
<context:component-scan base-package="com.luv2code.springdemo" />
<!-- Step 4: Add support for conversion, formatting and validation support -->
<mvc:annotation-driven/>
<!-- Step 5: Define Spring MVC view resolver -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
DebugController.java
package com.luv2code.springdemo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
/**
* Created by gaurav.ahirwar on 29/05/17.
*/
#Controller
public class DebugController {
#RequestMapping(value="/", method = RequestMethod.GET)
public String debug() {
return "hello";
}
}
The problem is that whenever I run the project and hit localhost:8080/ then index.jsp is executed but according to my configuration and mapping hello.jsp should be loaded. It may be possible that I am doing some silly mistake but I have tried as much as I can and now really frustrated. Please help me out. Thanks.
localhost:8080
Try this mapping:
#RequestMapping(value = {"", "/", "index.jsp"}, method = RequestMethod.GET)
I guess localhost:8080 equals "" path, but not /
I follow the instruction from the book "Spring in action" in SprinvMVC chapter
When i run the tomcat server and try the demo it return error like:
HTTP Status 404 – Not Found
Type Status Report
Description The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.
enter image description here
I using macOS , tomcat9 , Intellij 2017.1 , jdk 1.9
and here is my code.
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app 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"
version="3.1">
<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>spitter</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spitter</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
spitter.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"
xsi:schemaLocation="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.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--static resources getting-->
<mvc:resources mapping="/resources/**" location="/WEB-INF/resources"/>
<!--use annotations to create the mapping between-->
<!-- url and class deal with request(Controller) -->
<mvc:annotation-driven/>
<!--scan the component and auto regist as bean-->
<context:component-scan base-package="com.springmvc"/>
<!--Use this bean to map the jsp file according to the name return by Controller-->
<!--It will automatically add the prefix and suffix to the name string-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
controller:
package com.springmvc.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* Created by xwh on 29/3/2017.
*/
#Controller
public class HomeController {
// public static final int DEAFAULT_SPITTLES_PER_PAGE = 25;
public HomeController() {
System.out.println("-------HomeController init-------");
}
#RequestMapping("/")
public String showHomePage() {
System.out.println("-------showHomePage Method show-------");
return "home";
}
}
And here is my directory pic.
enter image description here
your contextConfigLocation file is spitter-servlet.xml?
In your web.xml file, you defined the context config file is applicationContext.xml. It means your configs in spitter.xml is not used.
try change the contextConfigLocation to /WEB-INF/spitter-servlet.xml
I have big problem with configuring Spring MVC application. No matter what I write in configuration xmls I get 404 while reaching localhost/appname/ or localhost/appname/register . Why that happens? Read tutorials for MVC in Maven and it looks like I'm doing everything as they says.
web.xml
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Wymysl to!</display-name>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
dispatcher-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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<context:component-scan base-package="wymysl.Controllers" />
<context:annotation-config />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/jsp/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<bean
class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
</beans>
RegisterController.java
package wymysl.Controllers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import wymysl.database.Person;
import wymysl.database.PersonsRepository;
#Controller
public class RegisterController {
#Autowired
PersonsRepository repo;
#RequestMapping(value = "/register", method = RequestMethod.GET)
public String register() {
return "register";
}
#RequestMapping(value = "/register", method = RequestMethod.POST)
public String register(#ModelAttribute("Person") Person person) {
System.out.println(""+person.getName());
return "index";
}
}
Things you can change to make it more basic:-
Remove
contextConfigLocation
/WEB-INF/dispatcher-servlet.xml
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
put your web.xml and dispatcher-servlet.xml at same path.
No need of
<context:annotation-config /> as it is already taken care by component-scan. See here:- Difference between <context:annotation-config> vs <context:component-scan> and <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
Make sure register.jsp is present in /WEB-INF/jsp/register.jsp.
If issue still does not get solved you need to share your server logs.
I am making a dummy hello world applicatiion for Spring MVC, i have done exactly same as was mentioned in example, but when i run the URL, its giving me the 404 error. Please help me if you can, following are my files:-
web.xml
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Spring MVC Application</display-name>
<mvc:default-servlet-handler default-servlet-name="moqahServices"/>
<servlet>
<servlet-name>moqahServices</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>moqahServices</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<error-page>
<error-code>404</error-code>
<location>/WEB-INF/jsp/error.jsp</location>
</error-page>
</web-app>
moqahServices.xml
<beans xmlns="http://www.springframework.org/schema/beans"
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-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="com.moqah" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
HelloController.jsp
package com.moqah;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.ui.ModelMap;
#Controller
#RequestMapping("/hello")
public class HelloController{
#RequestMapping(method = RequestMethod.GET)
public String printHello(ModelMap model) {
model.addAttribute("message", "Hello Spring MVC Framework!");
return "hello";
}
}
Please let me know if you have any suggestions
i guess the mvc:annotation-driven element should be used in your moqahServices, check it out
Looking into your code it seems,
1) you configured mvc:default-servlet-handler in the wrong place. It should be the part of your spring configurations.
2) In web.xml considering the servlet-name as moqahServices, spring configuration name should be suffixed with -servlet.xml followed by servlet-name ie. your spring configuration name is expected as moqahServices-servlet.xml
Please Try doing above changes.