JSPs not displaying objects from model in Spring - java

I have what should be an easy issue to solve, but I'm having no luck.
In my servlet-servlet.xml file, I have the following beans (in addition to others):
<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>
<context:component-scan base-package="com.servlet.web" />
My test controller looks like this:
package com.servlet.web;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
#Controller
public class TestController
{
protected final Log log = LogFactory.getLog(getClass());
#RequestMapping("/test")
public String methodName(Map<String, Object> map) {
map.put("someMessage", "some string here");
return "test";
}
}
My jsp view looks like this:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>servlet.com</title>
</head>
<body>
${someMessage}
</body>
</html>
So, when I view the jsp, I'd expect the value of someMessage (some string here), but I only get the following:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>servlet.com</title>
</head>
<body>
${someMessage}
</body>
</html>
When I crank up the logging, I see that my someMessage object is being place in the model:
22:21:17,425 DEBUG DispatcherServlet:852 - DispatcherServlet with name 'servlet' determining Last-Modified value for [/servlet/access/test]
22:21:17,426 DEBUG DefaultAnnotationHandlerMapping:183 - Mapping [/test] to handler 'com.servlet.web.TestController#762fef'
22:21:17,426 DEBUG DispatcherServlet:868 - Last-Modified value for [/servlet/access/test] is: -1
22:21:17,426 DEBUG DispatcherServlet:700 - DispatcherServlet with name 'servlet' processing GET request for [/servlet/access/test]
22:21:17,427 DEBUG HandlerMethodInvoker:158 - Invoking request handler method: public java.lang.String com.servlet.web.TestController.methodName(java.util.Map)
22:21:17,427 DEBUG DispatcherServlet:1070 - Rendering view [org.springframework.web.servlet.view.JstlView: name 'test'; URL [/WEB-INF/jsp/test.jsp]] in DispatcherServlet with name 'servlet'
22:21:17,427 DEBUG JstlView:328 - Added model object 'someMessage' of type [java.lang.String] to request in view with name 'test'
22:21:17,428 DEBUG JstlView:237 - Forwarding to resource [/WEB-INF/jsp/test.jsp] in InternalResourceView 'test'
22:21:17,429 DEBUG DispatcherServlet:666 - Successfully completed request
Obviously, my view is mapped correctly, but I can't seem to access model objects added to the request in the view. I've done this type of thing with Spring MVC many times in the past, but I must be missing something obvious here. Any ideas? Thanks.

Are you sure that evaluation of EL is enabled in your JSP? I sometimes had the problem, that it got turned of somehow. Try evaluating a simple expression like ${'test'} and see if 'test' appears.
You can also try enabling it with page directives or something else if EL should be disabled.
<%# page isScriptingEnabled="true" isELIgnored="false" %> //of course it has to be FALSE
(Sorry, I can't remember if this 100% correct. It might be 'isELEnabled')

I ran into the same problem, and after comparing 2 similar apps (one with EL working fine and the other not), noticed that the problem on my tomcat 7 depended on the webapp version specified in the web.xml of the application.
The same jsp using Web App 2.3 displays ${someMessage}. (BTW, this is what you get using maven archetype:generate with archetypeArtifactId=maven-archetype-webapp).
<!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>
...
The same jsp using Web App 2.4 displays the model object properly:
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="webapp-id" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
...
Hope that it helps!

I haven't used quite as much annotation configuration as you have with Spring MVC, so I'm not sure of all the things that are being automatically done with your setup. My only thought is this: should the method parameter be a ModelMap object? The examples I've seen before have all used ModelMap as the parameter type. Section 13.11.3 of this page is one of them: http://static.springsource.org/spring/docs/2.5.6/reference/mvc.html.
Like I said, I haven't used this type of auto-configuration before -- I do it slightly more manually, and extend my controllers from something like an AbstractController or a SimpleFormController.

Related

CSS file is not getting mapped/loaded in SPRING MVC program in eclipse

I'm very new to Spring mvc,for this question i've found many answers here but nothing is working for me and please help me to find out what mistake i'm doing here.
I have created a css file under WebContent>WEB-INF>resources>css>style.css and mapping is done under spring-servelet.xml and the code is
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
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">
<!-- DispatcherServlet Context: defines this servlet's request-processing
infrastructure -->
<!-- Enables the Spring MVC #Controller programming model -->
<annotation-driven />
<context:component-scan base-package="com.journaldev.spring" />
<!-- Resolves views selected for rendering by #Controllers to .jsp resources
in the /WEB-INF/views directory -->
<beans:bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<!-- Handles HTTP GET requests by efficiently serving up static resources
in the corresponding directory -->
<mvc:annotation-driven />
<mvc:default-servlet-handler />
<mvc:resources mapping="resources/**" location="resources/" cache-period="31556926"/>
</beans:beans>
and i've called the css file from jsp and the jsp file is
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%# taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>welcome</title>
<!-- Custom Theme files -->
<link href="<spring:url value='resources/css/style.css' />" rel="stylesheet" type="text/css" media="all" />
</head>
<body>
<h1>Hello world!</h1>
<P>The time on the server is ${serverTime}.</p>
</body>
</html>
Now network is aborted error is displaying at console .As i told i'm newbie to this framework so please help me out other than giving a link to an answer because i've tried almost all.Thank you
Put your CSS folder inside webcontent and add below code servlet xml file
<mvc:resources mapping="/css/**" location="/css/" />
Now you will be able to take the CSS from JSP inside views folder using below code -
<link href="/css/yourCSS.css" rel="stylesheet" type="text/css" media="all" />
This should work otherwise let usknow!
So, you will need to do what vipin cp is suggesting. You need this:
<link href="<spring:url value='/css/style.css' />" rel="stylesheet" type="text/css" media="all" />
This is because the you are using the mapping mapping="/css/**" , so your value needs to begin with the same thing as your mapping "/css/...", without the beginning slash, it doesn't match your mapping so spring doesn't know where to map it too, you could also change your mapping to mapping="css/**". Which ever you choose, they just need to match each other.
Once they match Spring can then resolve your urls to what they need to be.
Also, just a suggestion for readability. Specify all your spring:url elements at the top with a var argument and then you can reference that with standard SpEl.
And example is:
<spring:url value='/css/style.css' var='styleCss'/>
So, that tells Spring that the styleCss variable references the resolved url from /css/style.css. You can then reference that variable like:
<link href="${styleCss}" rel="stylesheet" type="text/css" media="all" />
Obviously, up to you, but I have always found it more readable.
For what it is worth, this can also be done with your JS imports if you have any.

unrelated struts actions calls with struts2.X

Working on one of the big web application with struts 2.x ( currently using struts-core 2.3.4.1 )
In this app when I call an action through ajax , after returning from an action, an unrelated some other action getting called. I didn't find the solution even after spending many days.
Later I thought to create simple sample app , which has just two actions, no web service calls, just one jsp, and only one action mapped ( I tried mapping other action too in struts.xml) again the same problem
Please find the code below
Struts Action Class
package lpaction;
public class PlanAction {
public String updatePlan() {
return "PLAN_ACTION_SUCCESS";
}
public String getPlans() {
return "PLAN1_ACTION_SUCCESS";
}
}
JSP Code
<%#page import="org.json.JSONArray"%>
<%#page import="org.json.JSONObject"%>
<%# page contentType="text/html; charset=utf-8" language="java"
import="java.sql.*" errorPage=""%>
<%# taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Client</title>
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon" />
<link rel="icon" href="../images/favicon.png" type="image/png" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script type="text/javascript" src="<%=request.getContextPath()%>/js/Libs/utility.js"></script>
<script type="text/javascript" src="<%=request.getContextPath()%>/js/Libs/jquery1.10.js"> </script>
<script type="text/javascript" src="<%=request.getContextPath()%>/js/Libs/ui/jquery-ui.js"></script>
<script type="text/javascript" src="<%=request.getContextPath()%>/js/Libs/jquery.ba-throttle-debounce.min.js"></script>
<script>
function callme(){
var formdata = "";
console.log("formData = "+formdata);
$.ajax({
type: 'POST',
url: 'plan',
contentType: "application/x-www-form-urlencoded",
async: false,
data :formdata,
cache: false,
processData:false,
datatype: "json",
success: function(response) {
alert("Success"+response);
},
error: function(e) {
alert("Fail");
}
});
}
</script>
</head>
<body>
<div id="wrap">
<input type="button" onclick="callme()"> press me </>
</div>
</body>
</html>
Struts.xml file
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.devMode" value="true" />
<package name="lessonPlan" extends="json-default">
<action name="plan" class="lpaction.PlanAction"
method="updatePlan">
<result name="PLAN_ACTION_SUCCESS" type="json" />
<result name="PLAN_ACTION_FAIL" type="json" />
</action>
<!-- <action name="plan1" class="lpaction.PlanAction"
method="getPlans">
<result name="PLAN1_ACTION_SUCCESS" type="json" />
<result name="PLAN1_ACTION_FAIL" type="json" />
</action> -->
</package>
</struts>
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_2_5.xsd"
version="2.5">
<filter>
<filter-name>testClient</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>testClient</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
</web-app>
If I put breakpoint in both action methods, the first action method will be called after that second action method will call.
Why second action method is getting called ?
Infact second action is not at all mapped ( Note I tried to uncomment that action mapping in struts.xml, still same problem )
I am using the following JAR files
Reason your getPlans method is called when you call plan action is that the method name starts with get. Since you have not specified any Customized Serialization and Deserialization for your JSON Result everything that starts with get will be Serialized, that means at the time of generating response getPlans method will be called because of Serialization.
You can change the action method name to overcome this issue, also refer this JSON Plugin Documentation for controlling your json object sent in response. If you don't control json object in response it can lead to unintentional data exposure which intern cause security risks and .
Update: As per documents
The serialization process is recursive, meaning that the whole object
graph, starting on the action class (base class not included) will be
serialized (root object can be customized using the "root" attribute)
This means every thing in action will be serialized(so that means every method with prefix get will be called), And this is done with recursion that is going in depth of object excluding base class.

There is no Action mapped for namespace / and action name tutorial

I am unable to execute my struts2 application. I am using eclipse indigo IDE, tomcat 7 and jdk 1.7.
The jar files I included are:
commons-logging-1.0.4.jar,
freemarker-2.3.8.jar,
ognl- 2.6.11.jar,
struts2-core-2.0.11.jar,
xwork-2.0.4.jar
I placed the struts.xml in classes folder in WEB-INF and I also tried it placing in
src folder but I could not able to make it. I am getting the below error on console
There is no Action mapped for namespace / and action name tutorial. - [unknown location]
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>Insert title here</title>
</head>
<body>
<form action="./tutorial.action">
Username: <input type="text" />
<input type="submit" value="Submit" />
</form>
</body>
</html>
struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration
2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="default" extends="struts-default">
<action name="tutorial" class="com.test.TutorialAction">
<result name="success">/success.jsp</result>
<result name="failure">/failure.jsp</result>
</action>
</package>
</struts>
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>Struts2Starter</display-name>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
TutorialAction.java
package com.test;
public class TutorialAction {
public String execute() {
System.out.println("Hello from execute");
return "success";
}
}
As others have pointed out, you do not have a getTutorial action in your mapping, only a "tutorial". However, I'm going to skip over that and suggest that you learn how to sanity check a Struts2 app. Anytime you are setting up a new technology, and even when you are working with a familiar technology, it is very useful to understand how to do a basic sanity check -- in this case, you need to verify that your struts xml has been successfully parsed and the framework knows about your actions.
I HIGHLY RECOMMEND you add the Struts 2 Config Browser plugin to your struts 2 apps. To add the plugin, you just get the jar ( all struts 2 plugins are jars ) and put in your webapp's lib directory. That's it. When you start your applicaiton, hit this URL:
http://localhost:8080/starter/config-browser/index.action
And it shows you all of the actions, as well as other configurations, that the framework knows about. Essentialy struts 2 diagnostic tool, and too easy to use to not use it.
Try to add namespace attribute into you package element of struts.xml file.
Like this:
<struts>
<package name="default" extends="struts-default" namespace="/">
<action name="tutorial" class="com.test.TutorialAction">
<result name="success">/success.jsp</result>
<result name="failure">/failure.jsp</result>
</action>
</package>
</struts>
I had the same problem and I saw multiple posts on this one....here is the possible solution
I was pretty sure that I had mapped all my actions accurately, but it was showing the same error above....so I just cleaned the project and then ran it again..it worked perfectly fine...give it a try !
I encountered this so many times...so to avoid such kind of things, I just added "../eclipse.exe -clean" to the shortcut icon property....this works and u can forget about getting such kind of errors which is actually not an error....!
The error comes because the server is not able to find correct path for struts.xml .
Its better to put the struts.xml in parallel to src folder or in WEB-INF/classes.
Try this once don't append .action
<form action="tutorial">
place your struts.xml file in src outside the package or in WEB-INF classes folder.

How do I figure out why my Struts 2 action hasn't been mapped? [duplicate]

I am unable to execute my struts2 application. I am using eclipse indigo IDE, tomcat 7 and jdk 1.7.
The jar files I included are:
commons-logging-1.0.4.jar,
freemarker-2.3.8.jar,
ognl- 2.6.11.jar,
struts2-core-2.0.11.jar,
xwork-2.0.4.jar
I placed the struts.xml in classes folder in WEB-INF and I also tried it placing in
src folder but I could not able to make it. I am getting the below error on console
There is no Action mapped for namespace / and action name tutorial. - [unknown location]
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>Insert title here</title>
</head>
<body>
<form action="./tutorial.action">
Username: <input type="text" />
<input type="submit" value="Submit" />
</form>
</body>
</html>
struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration
2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="default" extends="struts-default">
<action name="tutorial" class="com.test.TutorialAction">
<result name="success">/success.jsp</result>
<result name="failure">/failure.jsp</result>
</action>
</package>
</struts>
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>Struts2Starter</display-name>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
TutorialAction.java
package com.test;
public class TutorialAction {
public String execute() {
System.out.println("Hello from execute");
return "success";
}
}
As others have pointed out, you do not have a getTutorial action in your mapping, only a "tutorial". However, I'm going to skip over that and suggest that you learn how to sanity check a Struts2 app. Anytime you are setting up a new technology, and even when you are working with a familiar technology, it is very useful to understand how to do a basic sanity check -- in this case, you need to verify that your struts xml has been successfully parsed and the framework knows about your actions.
I HIGHLY RECOMMEND you add the Struts 2 Config Browser plugin to your struts 2 apps. To add the plugin, you just get the jar ( all struts 2 plugins are jars ) and put in your webapp's lib directory. That's it. When you start your applicaiton, hit this URL:
http://localhost:8080/starter/config-browser/index.action
And it shows you all of the actions, as well as other configurations, that the framework knows about. Essentialy struts 2 diagnostic tool, and too easy to use to not use it.
Try to add namespace attribute into you package element of struts.xml file.
Like this:
<struts>
<package name="default" extends="struts-default" namespace="/">
<action name="tutorial" class="com.test.TutorialAction">
<result name="success">/success.jsp</result>
<result name="failure">/failure.jsp</result>
</action>
</package>
</struts>
I had the same problem and I saw multiple posts on this one....here is the possible solution
I was pretty sure that I had mapped all my actions accurately, but it was showing the same error above....so I just cleaned the project and then ran it again..it worked perfectly fine...give it a try !
I encountered this so many times...so to avoid such kind of things, I just added "../eclipse.exe -clean" to the shortcut icon property....this works and u can forget about getting such kind of errors which is actually not an error....!
The error comes because the server is not able to find correct path for struts.xml .
Its better to put the struts.xml in parallel to src folder or in WEB-INF/classes.
Try this once don't append .action
<form action="tutorial">
place your struts.xml file in src outside the package or in WEB-INF classes folder.

Exception-" java.lang.NullPointerException: Module 'null' not found " in Java-Struts 1.3

While deplyoing Struts 1.3 Login application on localhost 8080 (Apache Tomcat 6.0.16 Server). I am getting following error.
HTTP Status 500 -
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: An exception occurred processing JSP page /Login.jsp at line 13
10: </head>
11: <body>
12:
13: <html:form action="/Login.do">
14: Username : <html:text name="LoginForm" property="userName"/><br/>
15: Password : <html:password name="LoginForm" property="password"/><br/>
16: <html:submit value="Login"/>
Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:505)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:416)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:337)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
root cause
java.lang.NullPointerException: Module 'null' not found.
org.apache.struts.taglib.TagUtils.getModuleConfig(TagUtils.java:755)
org.apache.struts.taglib.TagUtils.getModuleConfig(TagUtils.java:735)
org.apache.struts.taglib.html.FormTag.lookup(FormTag.java:818)
org.apache.struts.taglib.html.FormTag.doStartTag(FormTag.java:488)
org.apache.jsp.Login_jsp._jspx_meth_html_005fform_005f0(Login_jsp.java:105)
org.apache.jsp.Login_jsp._jspService(Login_jsp.java:78)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:337)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
note The full stack trace of the root cause is available in the Apache Tomcat/6.0.16 logs.
Apache Tomcat/6.0.16
Login.jsp
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%# taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<!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>Login Form</title>
</head>
<body>
<html:form action="/Login">
Username : <html:text name="LoginForm" property="userName"/><br>
Password : <html:password name="LoginForm" property="password"/><br>
<html:submit value="Login"/>
</html:form>
</body>
</html>
Struts-Config.xml
<?xml version="1.0" encoding="UTF-8"?>
<struts-config>
<!-- ========== Form Bean Definitions ================================== -->
<form-beans>
<form-bean name="loginForm" type="org.suraj.form.LoginForm"/>
</form-beans>
<!-- ========== Action Mapping Definitions ============================= -->
<action-mappings>
<action name="loginForm" path="/Login" type="org.suraj.action.LoginAction" scope="request" input="/Login.jsp" validate="true">
<forward name="failure" path="/Failure.jsp" redirect="true"/>
<forward name="success" path="/Success.jsp" redirect="true"/>
</action>
</action-mappings>
</struts-config>
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_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>Login</display-name>
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>
org.apache.struts.action.ActionServlet
</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>Login.jsp</welcome-file>
</welcome-file-list>
</web-app>
LoginForm.java
package org.suraj.form;
import org.apache.struts.action.ActionForm;
public class LoginForm extends ActionForm {
private static final long serialVersionUID = 1029546343415365160L;
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;
}
}
LoginAction.java
package org.suraj.action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.suraj.form.LoginForm;
public class LoginAction extends Action{
private static final long serialVersionUID = -8847579600418060362L;
private final static String SUCCESS = "success";
private final static String FAILURE = "failure";
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
LoginForm loginForm = (LoginForm) form;
if (loginForm.getUserName().equals(loginForm.getPassword())) {
return mapping.findForward(SUCCESS);
} else {
return mapping.findForward(FAILURE);
}
}
}
It could be for multiple reasons. Check here
Extract from the link,
This error occurs when you try to display a JSP before the Struts
ActionServlet has been initialized and is active. The causes for this
error are usually either:
You failed to specify 2 for the Struts ActionServlet in your web.xml file or
You did specify the above, but the Struts ActionServlet didn't initialize properly because of an error. Check the log file entries
for the time period when the Server first starts up to see if it
initialized properly or
You accessed a JSP page directly without going through an action
My issue appeared on the struts application having in pom.xml maven-compiler-plugin with setting source/target to 1.7 -> reset it to 1.5/1.6 it fixed my issue (building was done with Java 7). This application was deployed on Tomcat 7 but using java 6. Maybe it could help somebody...
Give load on startup as 1 to ActionServlet in web.xml.
this myt fix problem.
Verify if the TomCat Server must be run under JDK 1.7. Don't forget restart the server after the change.
This may be due to internet problem or systemconfig. problem
Solution:
1) Download http://struts.apache.org/dtds/struts-config_1_3.dtd dtd file and copy it into web-inf folder.
2) Change the struts-config file Doctype tag to !DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration1.3//EN" "**struts-config_1_3.dtd**">
I had the same problem but the solution that I found was different. After I checked the catalina.out file I realized that I had a problem with version of a class extended from ValidatorForm. And finally I solved it updating the jdk version. It happened because I was using a different version for develop than the installed version in tomcat.
Other than seeing this error on the login screen, I could see the error message which tells that my struts-config.xml file is not present under WEB-INF folder.
I had faced the same issue, but in my case I had a maven project
and I had my struts-config.xml under
src/main/resources/struts-config.xml
And it was not placed under src/main/webapp/WEB-INF folder.
As soon as I placed my struts-config.xml under src/main/webapp/WEB-INF folder, my error was gone and I was able to use the application.

Categories