Unable to Setup Spring + Maven Project - spring

I am new to working with Spring and decided to follow this tutorial:
http://www.programcreek.com/2014/02/spring-mvc-helloworld-using-maven-in-eclipse/
My files and file structure match the tutorial, and index.jsp is working. However, when I click to go to helloworld.jsp, I get the following 404 error:
The origin server did not find a current representation for the target
resource or is not willing to disclose that one exists.
Can anybody suggest places to dig? Is there something wrong with the tutorial that is not suited to Tomcat 8.5? Or is it more likely that there is something wrong with my setup?
EDIT:
I have the following installed:
Tomcat 8.5.14
Eclipse Neon with Spring IDE
Maven 3.5.0
If it helps, Maven has been working before I tried using it with a Web/Spring (ie mvn install downloads the correct libraries)
I have included an image
of my files and a below is the actual code of the files that I believe are relevant:
UserController.java
package com.ankurmgoyal.hellotest.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
#Controller
public class HelloWorldController {
String message = "Welcome to Spring MVC!";
#RequestMapping("/hello")
public ModelAndView showMessage(
#RequestParam(value = "name", required = false, defaultValue = "World") String name) {
System.out.println("in controller");
ModelAndView mv = new ModelAndView("helloworld");
mv.addObject("message", message);
mv.addObject("name", name);
return mv;
}
}
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>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>
<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>
index.jsp
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Spring 4 MVC - HelloWorld Index Page</title>
</head>
<body>
<center>
<h2>Hello World</h2>
<h3>
Click Here
</h3>
</center>
</body>
</html>
helloworld.jsp
<%# page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Spring 4 MVC -HelloWorld</title>
</head>
<body>
<center>
<h2>Hello World</h2>
</center>
</body>
</html>
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-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.ankurmgoyal.hellotest.controller" />
<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>
</beans>

index.jsp is served by DefaultServlet. The url-pattern for dispatcher is /. / pattern overrides the defaultServlet and all the calls are routed through this servlet. Change the url pattern to some non-empty string and try it out.
Refer to this SO Question for more details.

Related

How to Pass a view from controller?

I am new to Spring MVC. And I am trying to work with multiple views. When I use a single view and a single controller it works perfectly. But when I try to open a view from another view, I am getting HTTP 404 error (The requested resource is not found)
Project Directory Structure
As in the picture, I am having index.jsp as my home page view. From this page, I need to navigate another page(viewpage1.jsp) which is inside the WEB-INF/jsp directory. When I do so, I get HTTP 404 not found error.
HelloController.java
package com.springwebproject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
#Controller
public class HelloController
{
#RequestMapping("/")
public String home()
{
return "index";
}
#RequestMapping("/hello")
public String display()
{
return "viewpage1";
}
}
index.jsp
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<f:view>
<h1>Home Page</h1>
Go to Next View
</f:view>
</body>
</html>
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>SpringWebProject</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>SpringWebProject</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SpringWebProject</servlet-name>
<url-pattern>/viewpage1.jsp</url-pattern>
</servlet-mapping>
</web-app>
spring-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">
<!-- Provide support for component scanning -->
<context:component-scan base-package="com.springwebproject" />
<!--Provide support for conversion, formatting and validation -->
<mvc:annotation-driven/>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
</bean>
</beans>
viewpage1.jsp
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<f:view>
<h1>First View Page after Home page</h1>
</f:view>
</body>
</html>
Add this bean to your configuration:
#Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver viewResolver =
new InternalResourceViewResolver();
viewResolver.setPrefix("/views/"); // locate your directory with *.jsp files here
viewResolver.setSuffix(".jsp");
return viewResolver;
}
as you have given the mapping as "/hello" to view viewpage1, you have to change your href like this:
Go to Next View
Let me know, if this helps.

init #PostConstruct doesnt work

I have created a web dynamic project using hibernate4 and spring4.
here are my files:
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" version="3.0">
<display-name>posts</display-name>
<welcome-file-list>
<welcome-file>users.jsp</welcome-file>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:application-context.xml</param-value>
</context-param>
</web-app>
here is the bean where I invoked init() method
package com.posts.beans;
import java.io.Serializable;
import java.util.List;
import javax.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import com.posts.models.Users;
import com.posts.services.UsersService;
#SuppressWarnings("serial")
#Component("Usersbean")
#Scope("session")
public class UsersBean implements Serializable{
#Autowired
private transient UsersService us;
private List<Users> lu;
public UsersBean() {
}
#PostConstruct
public void init(){
lu=us.getAllUsers();
System.out.println("hello");//doesn't shw up=init() doesnt work
}
public String getMyname(){
return "mohamed";
}
public List<Users> getLu() {
return lu;
}
public void setLu(List<Users> lu) {
this.lu = lu;
}
}
here is my jsp file ; I tried to show just one field:
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%#taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%#taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql" %>
<!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 list users</title>
</head>
<body>
<jsp:useBean id="users" class="com.posts.beans.UsersBean" />
<table border="1">
<tr><th>id</th><th>nom</th><th>prenom</th><th>login</th><th>password</th></tr>
</table>
<c:forEach var="user" items="${users.lu}">
<p><c:out value="${user.nom}" /></p>
</c:forEach>
</body>
</html>
so the console doesnt show the message "hello" in method init which means that id doesnt work.
maybe it is because that i didnt declare servlet element in web.xm, but i dont have any idea on how it works ..
NB: I tested Services With JUnit, worked fine
i have solved that problem with annotating UserBean class with #Service("userbean") and adding :
<bean id="usersBean" class="com.posts.beans.UsersBean" init-method="init" />
in application-context.xml file ..
when I run my application , i see hibernate generated code of SELECT and the message hello
Hello
Hibernate: select users0_.id as id1_2_, users0_.login as login2_2_, users0_.nom as nom3_2_, users0_.password as password4_2_, users0_.prenom as prenom5_2_ from public.users users0_
" in the console which means that init() method have been executed.
You can make it work in three different ways:
add to your application-context.xml bean that is responsible to work out #PostConstruct annotation:
<bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" />
add namespase that already has CommonAnnotationBeanPostProcessor inside:
<context:annotation-config />
or
<context:component-scan base-package="package.with.beans" /> <!-- scans components and adds <context:annotation-config /> -->
define init method in the tag bean of your class for argument "init-method"

Need help on Spring MVC hello world

I am trying to learn Spring MVC on NetBeans 8.0.2 using Hibernate and frankly I am stuck. Any help is appreciated. I am only trying to do a very simple "hello world" type site.
Someone would on the first page hit a submit button and the resulting page would have a list of values from the DB. Sounds pretty simple right?
I've included 5 very short files here that will hopefully help you to help me if you are so inclined.
"web.xml", "dispatcher-servlet.xml","index.jsp", "TeamController.java", "secondView.jsp"
The way I understand how Spring MVC should work, using my files, is as follows...
1) Running the project from NetBeans, the index.jsp file is brought up.
This happens because the Web.xml is consulted, which has the following line...
"<welcome-file>redirect.jsp</welcome-file>"
once the redirect.jsp is consulted, we see that it has the following...
"<% response.sendRedirect("index.htm"); %>"
so with that redirect we go back to the web.xml which has the following...
"<servlet-name>dispatcher</servlet-name>"
"<url-pattern>*.htm</url-pattern>"
so with that request coming in as index.htm it is to be handled by the dispatcher servlet.
In the dispatcher servlet's config file, the view resolver will add the following...
p:prefix="/WEB-INF/jsp/"
p:suffix=".jsp" />
so index.htm gets changed to /WEN-INF/jsp/index.jsp and that page is brought up.
2) At this point the index.jsp is brought up. The only thing in this file is a form with a submit button. The intent is just to have someone press the button and info from the DB is returned on screen. I have Hibernate as part of this web project and I have created a java class "Team.java" based on a DB table "Team". It is populated with a few records.
Currently what I am seeing is that once the index.jsp comes up and I hit submit, it is giving a 404.
This is the URL shown for the form
"host:port/HelloWebFour/index.htm "
if I do a view source it shows it as the index.jsp. When I hit submit it gives a 404 with this url
"host:port/HelloWebFour/team? "
btw, "HelloWebFour" is the name of my project in NetBeans.
I am not sure what is happening, if my understanding is right or if I need to add anything. any help is appreciated...
The very short code files are below...
"Web.xml"
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" 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">
<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>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>redirect.jsp</welcome-file>
</welcome-file-list>
</web-app>
"dispatcher-servlet.xml"
<?xml version='1.0' encoding='UTF-8' ?>
<!-- was: <?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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd" xmlns:context="http://www.springframework.org/schema/context">
<context:component-scan base-package="testnew1" />
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>
<!--
Most controllers will use the ControllerClassNameHandlerMapping above, but
for the index controller we are using ParameterizableViewController, so we must
define an explicit mapping for it.
-->
<bean id="urlMapping"
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="index.htm">indexController</prop>
</props>
</property>
</bean>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/"
p:suffix=".jsp" />
<!--
The index controller.
-->
<bean name="indexController"
class="org.springframework.web.servlet.mvc.ParameterizableViewController"
p:viewName="index" />
</beans>
"index.jsp"
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<%#taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<!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>index.jsp - the submit page</title>
</head>
<body>
<h2>Hit submit to get DB information</h2>
<form:form method="GET" action="/HelloWebFour/team">
<table>
<tr>
<td>
<input type="submit" value="Submit"/>
</td>
</tr>
</table>
</form:form>
</body>
</html>
"TeamController.java"
package testnew1;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
#Controller
public class TeamController {
#RequestMapping(value = "/team", method = RequestMethod.GET)
public String getTeam(#ModelAttribute("SpringWeb") Team team,
ModelMap model) {
model.addAttribute("city", team.getCity());
model.addAttribute("state", team.getState());
model.addAttribute("nickname", team.getNickname());
return "secondView";
}
}
"secondView.jsp"
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<%#taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>secondView.jsp - the results page</title>
</head>
<body>
<h2>Database Information</h2>
<table>
<tr>
<td>City</td>
<td>${city}</td>
</tr>
<tr>
<td>State</td>
<td>${state}</td>
</tr>
<tr>
<td>Nickname</td>
<td>${nickname}</td>
</tr>
</table>
</body>
</html>
As far I see you Submiting to "/HelloWebFour/team but your only request handler method is mapped to /team, try to submit to just /team. I don't use any prefix on Dispatcher mapping so can be a missed .htm on url combining with the unknown request url /HelloWebFour/team.
Then if one of above solutions work, your method may fail because you are expecting a modelAttribute named as "SpringWeb" of type Team, but your form actually dont post any data and it's modelAttribute is mapped to "command", the default value, so removing this can possibly remove a further error if occurs.

Could not load component class org.springframework.web.context.ContextLoaderListener error occurs in spring mvc

when i try to execute spring mvc application i am facing below issue.
my controller class:-
package com.prasad.spring;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
#Controller
public class HelloWorldController {
#RequestMapping("/hello")
public ModelAndView helloWorld(){
String message = "HELLO SPRING MVC HOW R U";
return new ModelAndView("hellopage", "message", message);
}
}
and below welcome jsp file under WebContent folder
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Spring MVC Example</title>
</head>
<body>
click Here
</body>
</html>
below web.xml file:-
<?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>SpringMVCFirst</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
<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>
</web-app>
below spring-servlet.xml from Webconte/WEB-INF folder
<?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: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.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.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>
below jsp file from /WEB-INF/jsp/
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
Message is: ${message}
</body>
</html>
when i run above code on JBOSS7 i am facing below issue
23:57:49,379 INFO [org.jboss.as.controller] (DeploymentScanner-threads - 2) JBAS014774: Service status report
JBAS014777: Services which failed to start: service jboss.deployment.unit."SpringMVCFirst.war".POST_MODULE: org.jboss.msc.service.StartException in service jboss.deployment.unit."SpringMVCFirst.war".POST_MODULE: Failed to process phase POST_MODULE of deployment "SpringMVCFirst.war"
23:57:49,383 ERROR [org.jboss.as.server.deployment.scanner] (DeploymentScanner-threads - 1) {"JBAS014653: Composite operation failed and was rolled back. Steps that failed:" => {"Operation step-2" => {"JBAS014671: Failed services" => {"jboss.deployment.unit.\"SpringMVCFirst.war\".POST_MODULE" => "org.jboss.msc.service.StartException in service jboss.deployment.unit.\"SpringMVCFirst.war\".POST_MODULE: Failed to process phase POST_MODULE of deployment \"SpringMVCFirst.war\""}}}}
00:06:49,803 INFO [org.jboss.as.server.deployment.scanner] (DeploymentScanner-threads - 1) JBAS015003: Found SpringMVCFirst.war in deployment directory. To trigger deployment create a file called SpringMVCFirst.war.dodeploy
00:06:49,814 INFO [org.jboss.as.server.deployment] (MSC service thread 1-8) JBAS015876: Starting deployment of "SpringMVCFirst.war"
00:06:49,842 ERROR [org.jboss.msc.service.fail] (MSC service thread 1-2) MSC00001: Failed to start service jboss.deployment.unit."SpringMVCFirst.war".POST_MODULE: org.jboss.msc.service.StartException in service jboss.deployment.unit."SpringMVCFirst.war".POST_MODULE: Failed to process phase POST_MODULE of deployment "SpringMVCFirst.war"
at org.jboss.as.server.deployment.DeploymentUnitPhaseService.start(DeploymentUnitPhaseService.java:119) [jboss-as-server-7.1.0.Final.jar:7.1.0.Final]
at org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1811) [jboss-msc-1.0.2.GA.jar:1.0.2.GA]
at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1746) [jboss-msc-1.0.2.GA.jar:1.0.2.GA]
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source) [rt.jar:1.6.0]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [rt.jar:1.6.0]
at java.lang.Thread.run(Unknown Source) [rt.jar:1.6.0]
Caused by: org.jboss.as.server.deployment.DeploymentUnitProcessingException: JBAS011093: Could not load component class org.springframework.web.context.ContextLoaderListener
at org.jboss.as.ee.component.deployers.InterceptorAnnotationProcessor.processComponentConfig(InterceptorAnnotationProcessor.java:113)
at org.jboss.as.ee.component.deployers.InterceptorAnnotationProcessor.deploy(InterceptorAnnotationProcessor.java:54)
at org.jboss.as.server.deployment.DeploymentUnitPhaseService.start(DeploymentUnitPhaseService.java:113) [jboss-as-server-7.1.0.Final.jar:7.1.0.Final]
... 5 more
Caused by: java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener from [Module "deployment.SpringMVCFirst.war:main" from Service Module Loader]
at org.jboss.modules.ModuleClassLoader.findClass(ModuleClassLoader.java:190)
at org.jboss.modules.ConcurrentClassLoader.performLoadClassUnchecked(ConcurrentClassLoader.java:468)
at org.jboss.modules.ConcurrentClassLoader.performLoadClassChecked(ConcurrentClassLoader.java:456)
at org.jboss.modules.ConcurrentClassLoader.performLoadClass(ConcurrentClassLoader.java:398)
at org.jboss.modules.ConcurrentClassLoader.loadClass(ConcurrentClassLoader.java:120)
at org.jboss.as.ee.component.deployers.InterceptorAnnotationProcessor.processComponentConfig(InterceptorAnnotationProcessor.java:111)
... 7 more
and also i have added all spring jar files. please any one help to me where my code getting error
thanks in advance
how about adding
<param-value>classpath:/WEB-INF/spring-servlet.xml</param-value>. Try above one by adding classpath to param-value. So ContextLoaderListener can find your spring-servlet file.

No WebApplicationContext found: no ContextLoaderListener registered?

I'm trying to create a simple Spring 3 application and have the following files. Please tell me the reason for this error
Below is my 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"
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_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>Spring2</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
Below is my index.jsp
<%# page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%# taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%# taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
Index Page<br/>
<form:form commandName="loginBean" method="POST" action="login">
<form:input path="userName" id="userName"/><br/>
<form:input path="password" id="password"/><br/>
<input type="submit" value="submit"/>
</form:form>
Go to Registration Page
</body>
</html>
Below is my 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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/" />
<property name="suffix" value=".jsp" />
</bean>
<bean name="/login" class="com.infy.controller.LoginController"/>
</beans>
This is the LoginController.java
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 org.springframework.web.servlet.ModelAndView;
#Controller
public class LoginController {
#RequestMapping(value="/login", method=RequestMethod.POST)
public ModelAndView loginAction(#ModelAttribute("loginBean")LoginBean bean){
return new ModelAndView("success", "success", "Successful Login");
}
}
And finally my LoginBean
public class LoginBean {
private String userName;
private String password;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
You'll have to have a ContextLoaderListener in your web.xml - It loads your configuration files.
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
You need to understand the difference between Web application context and root application context .
In the web MVC framework, each DispatcherServlet has its own WebApplicationContext, which
inherits all the beans already defined in the root WebApplicationContext. These inherited
beans defined can be overridden in the servlet-specific scope, and new scope-specific
beans can be defined local to a given servlet instance.
The dispatcher servlet's application context is a web application context which is only applicable for the Web classes . You cannot use these for your middle tier layers . These need a global app context using ContextLoaderListener .
Read the spring reference here for spring mvc .
And if you would like to use an existing context, rather than a new context which would be loaded from xml configuration by org.springframework.web.context.ContextLoaderListener,
then see -> https://stackoverflow.com/a/40694787/3004747

Resources