Can't load my webpages using tomcat, servlets and spring - java

So I've been trying to load my webpages from a spring/servlet based java application that I've created and I keep getting HTTP Status 404 – Not Found error each time however Im positive I have hooked up all of xml servlet mappings and Java code correctly. There are no backend errors popping up other than the front end web page loading the 404 and saying The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.There are no errors and the paths that I have setup for the xml seem to be fine so Im not sure why my code is having trouble mapping the paths to the actual jsp files itself. Here is my project structure and code so far
web.xml
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>spring-mvc-demo</display-name>
<!-- 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>
</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: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-3.0.xsd
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">
<!-- Step 3: Add support for component scanning -->
<context:annotation-config/>
<context:component-scan base-package="com.christien" />
<!-- Step 4: Add support for conversion, formatting and validation support -->
<mvc:annotation-driven/>
<!-- Step 5: Define Spring MVC view resolver -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
HelloWorldController.java
package com.christien;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
#Controller
public class HelloWorldController {
#RequestMapping("/")
public String index(){
return "index";
}
//controller method to show the initial form
#RequestMapping("/showForm")
public String showForm(){
return "helloworld-form";
}
//controller method to process the form
#RequestMapping("/processForm")
public String processForm(){
return "helloworld";
}
}
If anyone could help me as to what I'm doing wrong or possibly even redirect me to another useful stack post that would be great, thanks!

By adding element tags to the beginning and end of the XML file may solve your issue. Like below
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<element>
<web-app>
<display-name>spring-mvc-demo</display-name>
<!-- 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>
</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>
</element>

Related

Spring MVC webapp not loading on browser

I am new to spring mvc and tomcat.
I have developed a demo spring mvc project and trying to deploy it on tomcat 9 through eclipse.
Server starts successfully but when i try to access the url from browser i get 404 with below error message on screen. :
Message The requested resource [/spring-mvc-demo/] is not available
Description The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.
Below are my code details :
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app 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"
id="WebApp_ID" version="3.1">
<display-name>spring-mvc-demo</display-name>
<absolute-ordering />
<!-- 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>/spring-mvc-demo</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="main.webapp" />
<!-- 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>
HomeController.java
package main.webapp.springdemo.controller;
import javax.annotation.PostConstruct;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
#Controller
public class HomeController {
#PostConstruct
public void init() {
System.out.println("HomeController bean getting intiated");
}
#RequestMapping("/")
public String getMainMenu() {
return "main-menu";
}
}
I tried running this app on tomcat 9 through eclipse as well as manually but in both the scenarios i got same error.
Assuming the application deploys without errors (check your logs), the URI path you are using to access it is almost certainly wrong. In a servlet environment the URI path decomposes as:
<context-path><servlet-path><path-info>
where:
<context-path> is the prefix to your application. In the Eclipse server configuration page this is called just "path" and defaults to /<project-name>,
<servlet-path> is configured through the <servlet-mapping> element in your web.xml deployment descriptor,
<path-info> is the part usually used by Spring to perform its internal routing (unless alwaysUseFullPath is set on the HandlerMapping).
Therefore (theoretically) you should try accessing:
http://localhost:8080/projectName/spring-mvc-demo/
There is however another problem: your servlet mapping is an exact mapping, that does not match anything else beyond /projectName/spring-mvc-demo. You should replace it with a prefix mapping (see this question for an overview of servlet mappings):
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/spring-mvc-demo/*</url-pattern>
</servlet-mapping>
If you want to shorten your URL, the DispatcherServlet is usually mapped as default servlet /. Be aware not to use the catchall prefix mapping /*, which would override the mapping of the JSP servlet.
Remark: There are some cases when Spring does not use the part after <servlet-path> for its routing, e.g. when you use an exact or extension mapping, the whole path after <context-path> is used.
Therefore if you use:
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/spring-mvc-demo</url-pattern>
</servlet-mapping>
you should specify:
#RequestMapping("/spring-mvc-demo")
public String getMainMenu() {
...
}
and use the URL http://localhost:8080/appName/spring-mvc-demo.

Spring MVC, Why am I getting a 404 error?

I cannot figure out why I am getting a 404 error despite literally hours of looking.
HTTP Status 404 – Not Found
Type Status Report
Message /
Description The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.
Apache Tomcat/9.0.24
I have a very simple spring MVC applications but I seriously can't see what the error is. Please help I'm losing my mind
Structure:
SpringMVCDemo
src
main
java
com.springmvc.demo
HomeController.java
WEB-INF
view
main-menu.jsp
spring-mvc-demo-servlet.xml
web.xml
My Controller:
HomeController.java
package com.springmvc.demo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
#Controller
public class HomeController {
#RequestMapping("/")
public String showMyPage(){
return "main-menu"; // view name
}
My View:
main-menu.jsp
<!DOCTYPE html>
<html>
<body>
<h2> Spring MVC Demo - main-menu</h2>
</body>
</html>
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>
<absolute-ordering />
<!-- 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.springmvc.demo" />
<!-- Step 4: Add support for conversion, formatting and validation support -->
<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>
In this case my issue was solved by re-creating my artifact SpringMVCdemo:war exploded from modules.
I do not fully understand what was wrong to begin with but hopefully that helps someone in future

Spring MVC - Invalid view being executed (violating specified request mapping)

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 /

spring adding external jar with controller not working

I have made a Java project having spring controller that handle url
Here is the controller code
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
#EnableWebMvc
#Controller
public class SmartContentValidator {
#RequestMapping(value = "/validate")
public String validate() {
System.out.println("Yo");
return "YO";
}
}
Then I exported above java project as jar file.
Then created a new web project and added the above jar in build path. Here is the web.xml and dispatcher-servlet.xml
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>SmartContentValidatorTest</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>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
dispatcher-servlet.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-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="com" />
<context:annotation-config/>
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/views/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
Now when I try to hit http://localhost:8080/project_name/validate
it shows 404-not found on browser and
WARNING: No mapping found for HTTP request with URI [/SmartContentValidatorTest/validate/] in DispatcherServlet with name 'dispatcher'
in my eclipse console.
What is the issue? Am I missing something?
Edit
Project Structure
I am not sure if it helps with your case but I think that in standalone application there would be an issue as #EnableWebMvc should be used with Java config - class annotated with #Configuration, not with #Controller bean.
As you use xml config you can try put <mvc:annotation-driven />, see https://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-config-enable for more details.
You would also need to provide your application context as dispatcher servlet param like in example http://docs.spring.io/spring-flex/docs/1.0.x/reference/html/ch02s02.html:
<servlet>
<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/config/web-application-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
Normally this is caused by Spring not correctly finding your controller. If you enable DEBUG logging Spring will log what beans are registered when you start the application.
If it is correctly loading your controller then check that your root path is correct (I think this is your war name by default) which again should be displayed in the logging during startup.

Spring MVC controllers not loaded

I have a problem with Spring not picking up the controller. I've used maven webapp archetype to create a project in Eclipse and it worked fine - I could access the index.jsp page. Problems started when I added Spring to the project. (context, controller, etc.)
I'm using Eclipse Mars, Tomcat 8, maven 3.3.3 and Spring 4.2.2
Here is what I see in the log (probably only third line matters):
Looking for matching resources in directory tree [C:\Users\dima\spring_workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp1\wtpwebapps\cte\WEB-INF\classes\com\company\dept\demo\cte\controller]
Searching directory [C:\Users\dima\spring_workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp1\wtpwebapps\cte\WEB-INF\classes\com\company\dept\demo\cte\controller] for files matching pattern [C:/Users/dima/spring_workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp1/wtpwebapps/cte/WEB-INF/classes/com/company/dept/demo/cte/controller/**/*.class]
Resolved location pattern [classpath*:com/company/dept/demo/cte/controller/**/*.class] to resources []
Instead of something like
INFO: Mapped URL path ...
As a result I get 404 when trying to open the web page. These are the files:
dispatcher-servlet.xml
<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"
xmlns:ldap="http://www.springframework.org/schema/ldap"
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
http://www.springframework.org/schema/ldap
http://www.springframework.org/schema/ldap/spring-ldap.xsd">
<context:component-scan base-package="com.company.dept.demo.cte.controller" />
<mvc:annotation-driven/>
<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>
web.xml
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</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>
</web-app>
And here is my controller:
package com.company.dept.demo.cte.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
#Controller
#RequestMapping("/pid")
public class PidController {
#RequestMapping(value="/hello")
public ModelAndView helloWorld() {
ModelAndView mav = new ModelAndView("pidHello");
mav.addObject("message", "hello world");
return mav;
}
}
When trying to access localhost:8080/cte/pid/hello 404 returned and the log says:
DispatcherServlet with name 'dispatcher' processing GET request for [/cte/pid/hello]
Looking up handler method for path /pid/hello
Did not find handler method for [/pid/hello]
No mapping found for HTTP request with URI [/cte/pid/hello] in DispatcherServlet with name 'dispatcher'
Thanks.
Update:
WAR file is correct but for some reason when it is deployed to Tomcat controller class isn't copied.
Is it your full web.xml , Because you have to add your dispatcher servlet reference to web.xml , Like below -
<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/dispatcher-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

Categories