spring adding external jar with controller not working - java

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.

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 - URL pattern not working

I have the following files:
web.xml:
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<display-name>To do List</display-name>
<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/tk-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/spring-mvc/*</url-pattern>
</servlet-mapping>
</web-app>
LoginController:
package de.yellowsub.tk.mvc;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
#Controller
public class LoginController {
#RequestMapping(value = "/login")
#ResponseBody
public String sayHello() {
return "Hello World!";
}
}
Now if I put in localhost:8080/login in the URL I can see "Hello World" in the Browser but not if I write localhost:8080/spring-mvc/login
Any ideas?
Also here is the tk-servlet.xml if it's any use:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/bean/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">
<context:component-scan base-package="de.yellowsub" /> //I also tried "de.yellowsub.*"
<mvc:annotation-driven />
</beans>
Based on your comment you have generated a Spring Boot application. That is quite different from a Spring MVC application, you do not need web.xml or tk-servlet.xml to configure it. You can delete both.
You can add server.contextPath=/spring-mvc to your application.properties (create it in src/main/resources) to set the context path.
Also please find a different tutorial, because pure Spring based MVC application is totally different from Spring Boot web application.
You NEED TO fix the root context for your web application (spring-mvc) to fix the URL
If you are using Maven, you can fix your war name by adding the below tag into the pom.xml:
<build>
<finalName>spring-mvc</finalName>
</build>
Then you should be able to access your application controller always with below url :
http://localhost:8080/spring-mvc/login
if you dont use maven follow this link How to set the context path of a web application in Tomcat 7.0

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 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>

Spring project in Eclipse - 404

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.

Categories