java.lang.ClassNotFoundException: jakarta.servlet.http.HttpServlet : Spring MVC and Eclipse - spring

I am creating a web application using Spring MVC and Eclipse IDE.
Spring Version- 6.0.3
To configure the project, I followed the following steps-
Added dependencies in pom.xml-
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- Spring MVC Dependency -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>6.0.3</version>
</dependency>
</dependencies>
Added Tomcat Server Runtime to the build path-
Added maven dependencies to deployment assembly-
web.xml- (in WEB-INF folder)
<web-app>
<display-name>Spring MVC Demo</display-name>
<!-- Configure dispatcher servlet -->
<servlet>
<servlet-name>dispatcherservlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherservlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- / means handle all the requests from all urls -->
</web-app>
dispatcherservlet-servlet.xml (in WEB-INF folder)
<?xml version="1.0" encoding="UTF-8"?>
<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
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"
xmlns:p="http://springframework.org/schema/p">
<!-- Enable annotations -->
<context:component-scan base-package="spring-mvc-demo.src.main.java.controller"></context:component-scan>
<!-- View Resolver bean -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
name="viewResolver">
<!-- Inject two properties -->
<!-- Location for pages is given to prefix -->
<property name="prefix" value="/WEB-INF/views/" />
<!-- ending of page is .jsp -->
<property name="suffix" value=".jsp" />
<!-- Example name /WEB-INF/views/hello.jsp (here the name hello will be
given by controller) -->
</bean>
</beans>
Placed index.jsp under views folder in WEB-INF-
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Home Page</title>
</head>
<body>
<h1>This is home page</h1>
<h1>Called by home controller</h1>
<h1>fired for /</h1>
</body>
</html>
Created HomeController.java class in src/main/java/controller
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
#Controller
#RequestMapping("/home")
public class HomeController {
#GetMapping("/current")
public String home() {
//return the name of the page
System.out.println("Hello this is home URL");
return "index";
}
}
I have created a controller with the url /home/current. On visiting this url I expect to see the desired index.jsp.
Problem-
When I "Run on Server" I get following error-
SEVERE: Allocate exception for servlet [dispatcherservlet]
java.lang.ClassNotFoundException: jakarta.servlet.http.HttpServlet
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1412)
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1220)
at java.base/java.lang.ClassLoader.defineClass1(Native Method)
at java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1012)
at java.base/java.security.SecureClassLoader.defineClass(SecureClassLoader.java:150)
Please help me to find the error in my configuration and the reason I get this error.
I saw several other posts, re-checked my steps but still getting the same error-
java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet

Based on the first screenshot in your question, you're using Tomcat 9. But for Spring 6 you need a minimum of Tomcat 10. Tomcat 10 is the first to use jakarta.* namespace whereas older versions used javax.* namespace.
Spring 6 (and Spring Boot 3) is the first version to use jakarta.* namespace whereas older versions used javax.* namespace.
So you have 2 options:
Upgrade Tomcat to a minimum of 10.
Or, downgrade Spring to a maximum of 5.
Clearly, option 1 is the recommended way to go in long term.
By the way, Spring 6 requires Java 17 not Java 1.7 as seen in the first screenshot in your question.
See also:
A Java 17 and Jakarta EE 9 baseline for Spring Framework 6
Apache Tomcat Versions
How to properly configure Jakarta EE libraries in Maven pom.xml for Tomcat?

Related

Tomcat 6 vs 7 broke default and jsp servlet requests (404)

We're migrating around 20 web contexts from Tomcat 6.0.48 to Tomcat 7.0.76 but we're facing an issue with the requests handled by the servlets configured in the parent web.xml (tomcat7\conf\web.xml). The problem is that requests for /some_file.html, /some_file.jsp, /images/some-image.jpg, /index.xhtml return a 404 NOT FOUND, while other custom servlets declared in the web.xml of the context work fine. All 20 contexts are working fine in Tomcat 6 for years, but only 10 are failing on Tomcat 7. We have compared the ones that work with the ones that don't, but they are very different (unsurprisingly) and we haven't found the problem.
The tests
Projects are deployed using Eclipse under Windows, some test were executed deploying the WAR file directly on /webapps with the same results. All projects are Java 1.8 and they are mavenized. The tests consist in deploying one working context and one that does not work and navigate to: /some_file.html, /some_file.jsp, /images/some-image.jpg and see if the response is 200 or 404.
We've tried to match the version of javax.servlet.servlet-api (2.5 to 3.0.1) and javax.servlet.jsp.jsp-api (2.0 to 2.2) with the ones provided by Tomcat 7 in the dependencies (Parent POM). No change.
<dependency>
<groupId>javax.servlet</groupId>
<!-- <artifactId>servlet-api</artifactId>
<version>2.5</version> -->
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<!-- <version>2.0</version> -->
<version>2.2</version>
<scope>provided</scope>
</dependency>
We've copied the default and jsp servlet mapping configuration from the web.xml file of Tomcat on the web.xml of the application. No change.
<!-- The mapping for the default servlet -->
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- The mappings for the JSP servlet -->
<servlet-mapping>
<servlet-name>jsp</servlet-name>
<url-pattern>*.jsp</url-pattern>
<url-pattern>*.jspx</url-pattern>
</servlet-mapping>
We've changed the url-pattern / to /* in the tomcat's web.xml file for the default servlet and things started to work with static resources but not for *.jsp and *.xhtml files (Faces). But as read in the servlet specification (JSR-315), it should work with /. Either way, this change broke *.jsp requests for all contexts (¿?).
<servlet-name>default</servlet-name>
<url-pattern>/</url-pattern>
<servlet-name>default</servlet-name>
<url-pattern>/*</url-pattern>
We've deleted all servlet-mapping and filter-mapping from the web.xml file of the context to see if there was any kind of conflict. No change.
We suspect that...
either the tomcat's web.xml is not read, it's overwritten, or the url-pattern for / and *.jsp is broken by some contexts's descriptor.
Any clues?
Thanks in advance.

the message is not printed correctly on the view page in spring mvc [duplicate]

This question already has answers here:
EL expressions not evaluated in JSP
(5 answers)
Closed 7 years ago.
JSTL variable values are not shown in EL. For example this code:
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%# taglib prefix="s" uri="http://www.springframework.org/tags" %>
<html>
<body>
<c:forEach var="i" begin="1" end="5" >
<c:out value="${i}" />
</c:forEach>
</body>
</html>
browser renders like: ${i} ${i} ${i} ${i} ${i}
Or this one:
<c:set var="someVar" value="Hello"/>
<c:out value="${someVar}"/>
browser displays: ${someVar}
I'm using Spring-MVC 3 and Maven to build the sample project, deploying it to Tomcat 7.
In Spring's context I have view resolver configurated as follows:
<bean class=
"org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="
org.springframework.web.servlet.view.JstlView"></property>
<property name="prefix" value="/WEB-INF/"></property>
<property name="suffix" value=".jsp" />
</bean>
Model variables passed form Spring's controller not shown as well.
Mavens pom.xml has following jstl related dependencies:
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
<scope>provided</scope>
</dependency>
So, any suggestions how to resolve this?
So, the EL (those ${} things) doesn't get executed? That can happen when your servletcontainer runs at Servlet 2.3 / JSP 1.2 modus or lower, while you're using JSTL 1.1 or newer. During the change from JSTL 1.0 to 1.1, EL was moved from JSTL into JSP. That was JSP 2.0, which is part of Servlet 2.4. JSP 1.2 and older doesn't have EL bundled. JSTL 1.1 and newer doesn't have EL bundled.
You need to make sure that your web.xml root declaration conforms at least Servlet 2.4. As you're using JSP 2.1, which is part of Servlet 2.5, you're apparently targeting a Servlet 2.5 compatible container. So, make sure that your web.xml root declaration conforms Servlet 2.5:
<?xml version="1.0" encoding="UTF-8"?>
<web-app
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_5.xsd"
version="2.5">
<!-- Config here. -->
</web-app>
Tomcat 7 is however a Servlet 3.0 compatible container. I'd consider changing maven pom to declare Servlet 3.0 / JSP 2.2 so that you can benefit all the new Servlet 3.0 features.
See also:
Our JSTL wiki page
History of EL

Spring MVC not initial request mapping

Does the web deploy mechanism affect Spring MVC work?
I create a web project(SpringMVCTest) contains web.xml and a pjsp pages then I create a Java project(SpringMVCTestSrc) and use the link source function to link test_src and SpringMVCTestSrc.
My Eclipse workspace
My problem is if I deploy the project in war file then it works fine
, EX: if I open
localhost:8080/SpringMVCTest/message/showMessage.pages
it will forward to
main/showMessage.jsp
but if I deploy the project with separate web and jar file it will no request mapping found like below.
[DEBUG]-[2015/07/30 15:03:39,o.s.w.s.DispatcherServlet(init):139]: Servlet 'dispatcher' configured successfully
[DEBUG]-[2015/07/30 15:03:40,o.s.w.s.DispatcherServlet(doService):861]: DispatcherServlet with name 'dispatcher' processing GET request for [/SpringMVCTest/message/showMessage.pages]
[DEBUG]-[2015/07/30 15:03:40,o.s.w.s.m.m.a.RequestMappingHandlerMapping(getHandlerInternal):294]: Looking up handler method for path /message/showMessage.pages
[DEBUG]-[2015/07/30 15:03:40,o.s.w.s.m.m.a.RequestMappingHandlerMapping(getHandlerInternal):302]: Did not find handler method for [/message/showMessage.pages]
[WARN]-[2015/07/30 15:03:40,o.s.w.s.PageNotFound(noHandlerFound):1136]: No mapping found for HTTP request with URI [/SpringMVCTest/message/showMessage.pages] in DispatcherServlet with name 'dispatcher'
The log shows the different.
If I deploy in war file. The log shows found 18 bean definitions and request mapping works fine.
Export war file from Eclipse and put it to apache-tomcat-8.0.21\webapps
Startup Tomcat and open localhost:8080/SpringMVCTest/message/showMessage.pages
The page will show Message : Hello world
o.s.c.a.ClassPathBeanDefinitionScanner(registerDefaultFilters):244]: JSR-250 'javax.annotation.ManagedBean' found and supported for component scanning
o.s.c.i.s.PathMatchingResourcePatternResolver(doFindMatchingFileSystemResources):631]: Looking for matching resources in directory tree [D:\Temp\apache-tomcat-8.0.21\webapps\SpringMVCTest\WEB-INF\classes\in\hotkey\mvc]
o.s.c.i.s.PathMatchingResourcePatternResolver(doRetrieveMatchingFiles):693]: Searching directory [D:\Temp\apache-tomcat-8.0.21\webapps\SpringMVCTest\WEB-INF\classes\in\hotkey\mvc] for files matching pattern [D:/Temp/apache-tomcat-8.0.21/webapps/SpringMVCTest/WEB-INF/classes/in/hotkey/mvc/**/*.class]
o.s.c.i.s.PathMatchingResourcePatternResolver(findPathMatchingResources):424]: Resolved location pattern [classpath*:in/hotkey/mvc/**/*.class] to resources [file [D:\Temp\apache-tomcat-8.0.21\webapps\SpringMVCTest\WEB-INF\classes\in\hotkey\mvc\MainForm.class]]
o.s.c.a.ClassPathBeanDefinitionScanner(findCandidateComponents):286]: Identified candidate component class: file [D:\Temp\apache-tomcat-8.0.21\webapps\SpringMVCTest\WEB-INF\classes\in\hotkey\mvc\MainForm.class]
o.s.b.f.x.XmlBeanDefinitionReader(loadBeanDefinitions):224]: Loaded 18 bean definitions from location pattern [classpath:test-dispatcher-context.xml]
If I deploy with separate web and jar file. The log shows found 17 bean definitions and request mapping does not work.
Build a directory in apache-tomcat-8.0.21\webapps, EX: apache-tomcat-8.0.21\webapps\SpringMVCTest
Copy WEB-INF, META-INF from Eclipse workspace to apache-tomcat-8.0.21\webapps\SpringMVCTest
Export jar file from SpringMVCTestSrc and put it to apache-tomcat-8.0.21\webapps\SpringMVCTest\WEB-INF\lib
Startup Tomcat and open localhost:8080/SpringMVCTest/message/showMessage.pages
The page shows HTTP 404
o.s.c.a.ClassPathBeanDefinitionScanner(registerDefaultFilters):244]: JSR-250 'javax.annotation.ManagedBean' found and supported for component scanning
o.s.c.i.s.PathMatchingResourcePatternResolver(findPathMatchingResources):424]: Resolved location pattern [classpath*:in/hotkey/mvc/**/*.class] to resources []
o.s.b.f.x.XmlBeanDefinitionReader(loadBeanDefinitions):224]: Loaded 17 bean definitions from location pattern [classpath:test-dispatcher-context.xml]
Does someone know why the same code but different deploy mechanism has different result?
My code as blow
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>SpringMVCTest</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:test-service-context.xml</param-value>
</context-param>
<filter>
<filter-name>SetCharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>SetCharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:test-dispatcher-context.xml</param-value>
</init-param>
<load-on-startup>3</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.pages</url-pattern>
</servlet-mapping>
</web-app>
Spring Configuration(test-service-context.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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">
<bean id="testMessageService" class="in.hotkey.service.TestMessageService" >
</bean>
</beans>
Spring MVC Config(test-dispatcher-context.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-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<mvc:annotation-driven />
<context:component-scan base-package="in.hotkey.mvc"/>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" />
</beans>
Main Controller
package in.hotkey.mvc;
import in.hotkey.service.TestMessageService;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
#Controller
public class MainForm {
#RequestMapping(value="/message/showMessage.pages")
public ModelAndView handleShowMessage() throws Exception {
ModelAndView mv = new ModelAndView("main/showMessage");
mv.addObject("message", "Hello world!");
return mv;
}
}
JSP Content
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Test</title>
</head>
<body>
<h4>Message : ${message}</h4>
</body>
</html>
I found what the problem is....
Root cause founded from Spring MVC document.
It means annotation scan need directory entries!!
The problem is if I export whole project into war file. It contains the complete directory entries in WEB-INF/classes directory but jar file has no directory entry information.
Resolve this problem just check the "Add directory entries" option in Eclipse export function.

No mapping found for HTTP request with URI.... in DispatcherServlet with name [duplicate]

This question already has answers here:
Why does Spring MVC respond with a 404 and report "No mapping found for HTTP request with URI [...] in DispatcherServlet"?
(13 answers)
Closed 6 years ago.
I checked out nearly every relevant article on stackoverflow already, but I just cant fix my problem.
Here is the code:
web.xml:
<display-name>Spring3MVC</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-servlet.xml</param-value>
</context-param>
<listener>
<listener-class> org.springframework.web.context.ContextLoaderListener </listener-class>
</listener>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.html</url-pattern>
<url-pattern>/</url-pattern>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
</web-app>
spring-servlet.xml:
<context:component-scan base-package="com.mycompany.elso" />
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>
<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>
myController:
public class myController {
#RequestMapping("/hello")
public ModelAndView helloWorld() {
String message = "Hello World, Spring 3.0!";
return new ModelAndView("hello", "message", message);
}
}
Web Pages/index.jsp:
<html>
<head>
<title>Spring 3.0 MVC Series</title>
</head>
<body>
Say Hello
</body>
</html>
Web Pages/WEB-INF/jsp/hello.jsp:
<html>
<head>
<title>Spring 3.0 MVC Series: Hello World - ViralPatel.net</title>
</head>
<body>
${message}
</body>
</html>
So when i launch the appication the index.jsp is loaded correctly but when i click on the href to navigate to hello.jsp i got a 404 error and the server log says:
No mapping found for HTTP request with URI [/Elso/hello.html] in DispatcherServlet with name 'spring'
I've checked out dozens of articles like that, but I just can't find the mistake, anybody has any idea what could it be?
Add
<mvc:default-servlet-handler/>
to spring-servlet.xml
You could try and add an #Controller annotation on top of your myController Class and
try the following url /<webappname>/my/hello.html.
This is because org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping prepends /my to each RequestMapping in the myController class.
If you are using
<mvc:annotation-driven/>
make sure your spring-servlet.xml has correct
<context:component-scan base-package="com.....controller" /> tag.
Basically, you need to include all the packages where you have used the annotation in your java code.
Also, please ensure you do not have duplication of component-scan (for a discovery of beans). If your config XML already contains the element, then any of your Controller classes that are annotated with #ComponentScan(basePackages=... needs to be stripped of the said annotation.
I solved my issue with :
Java Build Path -> JRE system library - > Edit -> Alternate JRE -> -> Finish
As it was configured to JDK folder so it was giving Exception
Make sure
<mvc:annotation-driven/>
<context:component-scan base-package="com.hireartists.web.controllers"/>
points to proper package that contains controllers.
Please check your [PROJECT_NAME]\target\classes directory to see whether myController.class is generated or not.
If not, please check all your java source code whether there are any compilation errors.
If you are using Java code based on Spring MVC configuration then enable the DefaultServletHandlerConfigurer in the WebMvcConfigurerAdapter object.
#Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
Try:
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
Worked for me!
Check ur Bean xmlns..
I also had similar problem, but I resolved it by adding mvc xmlns.
<?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: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.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.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>
addition of <mvc:annotation-driven/> worked for me.
Add it before line <context:component-scan ............/>
If you want to serve .html files, you must add this <mvc:default-servlet-handler /> in your spring config file. .html files are static.
Hope that this can help someone.
It is not finding the controllers, this is basic issues.
it can be due to following reasons.
A. inside WEB-INF folder you have file web.xml that refers to dispatcherServlet. Here it this case is mvc-config.xml
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/mvc-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
B. This mvc-config.xml file have namespaces and it has to scan the controllers.
<context:component-scan base-package="org.vimal.spring.controllers" />
<mvc:annotation-driven />
C. Check for the correctness of the package name where you have the controllers. It should work.
All Controllers must be Annotated with #Controller.
Had the exact same error and it took me a long time trying to understand it. It is most likely down to compilation errors, the java classes did not get published in your servlet. Please check this by going in the server that you are using \tmp1\wtpwebapps[PROJECT_NAME]\WEB-INF\classes\ and try to find you controller classes to see whether or not they have been published. If not you need to get to the bottom of any compilation errors.
If you are using maven as build tool for project , build your project properly,your changes in the code and xml files are not reflecting after compilations.
If you depend on Spring Social, check that you have configured a Web Controller bean:
import org.springframework.context.annotation.Bean;
import org.springframework.social.connect.web.ConnectController;
import org.springframework.social.connect.ConnectionFactoryLocator;
import org.springframework.social.connect.ConnectionRepository;
...
#Bean
public ConnectController connectController(ConnectionFactoryLocator connectionFactoryLocator, ConnectionRepository connectionRepository) {
return new ConnectController(connectionFactoryLocator, connectionRepository);
}
if you are using maven then do run maven install command before you run your web app on a server as it will generate a class file for your controller and in my experience that is what your application has been missing.
I had the same issue and after lots of reserach I found the classes were not getting published in my target folder. So I had run the below two commands from cmd
mvn clean install
mvn package
Surprisingly I was able to access the page and error was gone. Same can be verified from target folder where you will be able to find the complied classes which were missing earlier.
Add #Controller to your controller or where ever you have the #RequestMapping for you json end point.
This worked for me while deploying a similar application.
If you are using Maven ,
Add these to your pom.xml
<dependency>
<groupid>javax.servlet</groupid>
<artifactId>jstl</artifactId>
<version>1.2</version>
<scope>runtime</scope>
<dependency>
<groupid>taglibs</groupid>
<artifactId>standard</artifactId>
<version>1.1.2</version>
<scope>runtime</scope>
I also faced the same issue, but after putting the namespace below, it works fine:
xmlns:mvc="http://www.springframework.org/schema/mvc"
Removing the Tomcat Server and adding new tomcat configuration in Eclipse resolved issue for me.
In pom.xml make sure packaging is set to war like <packaging>war</packaging> ,not to jar or any thing else.
What is /Elso?
You try:
#RequestMapping("/Elso")
public class myController {
#RequestMapping("/hello")
public ModelAndView helloWorld() {
String message = "Hello World, Spring 3.0!";
return new ModelAndView("hello", "message", message);
}
}

java.lang.VerifyError org.apache.cxf.transport.servlet.ServletTransportFactory overrides final method register.()V?

i'm trying to develop a new SOAP web service into my project using CXF and spring 2.5.6, but when i deploy the war file into tomcat 6 i got this exception :
java.lang.VerifyError org.apache.cxf.transport.servlet.ServletTransportFactory overrides final method register.()V
i'have done all the configurations into pom.xml, web.xml and application context files. :
this is the web.xml add lines for the WS :
<display-name>CXF Example Webservice</display-name>
<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
and i add these line into the applicationscontext file :
<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
<!-- Spring manage ServiceBean -->
<bean id="savServ" class="com.otn.sav.SavServiceImpl" />
<!-- JAX-WS Service Endpoint -->
<jaxws:endpoint id="savService" implementor="#savServ" address="/savService" />
i think tha ther is a problem of "versionig" this why java can't load the class :
org.apache.cxf.transport.servlet.ServletTransportFactory
i try it serval times to solve this exception but unfortunately i can't resolve it
if any one can help please answer me ???????
ps : sorry for my bad english :)
Edit: look if you don't have some older jar on your classpath - this was my case (not sure how it happened but deleting them helped). Still, it produced another problem.

Resources