Archive for the ‘struts’ Category.

Expression eKey is undefined on line 71, column 63 in template/simple/fielderror.ftl

This is one nasty exception occured due to carelessness.  I have added one field error in my action.

super.addFieldError("userName", "User name is null");

Instead of the field name “userName”, I have given the variable itself :)

super.addFieldError(userName, "User name is null");

Replacing this removed this error.

10:40:24,859        ERROR runtime:96 -
Expression eKey is undefined on line 71, column 63 in template/simple/fielderror.ftl.
The problematic instruction:
----------
==> assignment: eValue=fieldErrors[eKey] [on line 71, column 33 in template/simple/fielderror.ftl]
----------
Java backtrace for programmers:
----------
freemarker.core.InvalidReferenceException: Expression eKey is undefined on line 71, column 63 in template/simple/fielderror.ftl.
 at freemarker.core.TemplateObject.assertNonNull(TemplateObject.java:124)
 at freemarker.core.DynamicKeyName._getAsTemplateModel(DynamicKeyName.java:85)
 at freemarker.core.Expression.getAsTemplateModel(Expression.java:89)
 at freemarker.core.Assignment.accept(Assignment.java:90)
 at freemarker.core.Environment.visit(Environment.java:208)
 at freemarker.core.MixedContent.accept(MixedContent.java:92)
 at freemarker.core.Environment.visit(Environment.java:208)
 at freemarker.core.IteratorBlock$Context.runLoop(IteratorBlock.java:167)
 at freemarker.core.Environment.visit(Environment.java:415)
 at freemarker.core.IteratorBlock.accept(IteratorBlock.java:102)
 at freemarker.core.Environment.visit(Environment.java:208)
 at freemarker.core.MixedContent.accept(MixedContent.java:92)
 at freemarker.core.Environment.visit(Environment.java:208)
 at freemarker.core.ConditionalBlock.accept(ConditionalBlock.java:79)
 at freemarker.core.Environment.visit(Environment.java:208)
 at freemarker.core.IfBlock.accept(IfBlock.java:82)
 at freemarker.core.Environment.visit(Environment.java:208)
 at freemarker.core.MixedContent.accept(MixedContent.java:92)
 at freemarker.core.Environment.visit(Environment.java:208)
 at freemarker.core.ConditionalBlock.accept(ConditionalBlock.java:79)
 at freemarker.core.Environment.visit(Environment.java:208)
 at freemarker.core.MixedContent.accept(MixedContent.java:92)
 at freemarker.core.Environment.visit(Environment.java:208)
 at freemarker.core.Environment.process(Environment.java:188)
 at freemarker.template.Template.process(Template.java:237)
 at org.apache.struts2.components.template.FreemarkerTemplateEngine.renderTemplate(FreemarkerTemplateEngine.java:182)
 at org.apache.struts2.components.UIBean.mergeTemplate(UIBean.java:559)
 at org.apache.struts2.components.UIBean.end(UIBean.java:513)
 at org.apache.struts2.views.jsp.ComponentTagSupport.doEndTag(ComponentTagSupport.java:42)
 at org.apache.jsp.index_jsp._jspx_meth_s_005ffielderror_005f0(index_jsp.java:193)
 at org.apache.jsp.index_jsp._jspx_meth_s_005fform_005f0(index_jsp.java:132)
 at org.apache.jsp.index_jsp._jspService(index_jsp.java:78)
 at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
 at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
 at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:377)
 at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
 at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
 at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
 at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:646)
 at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:436)
 at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:374)
 at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:302)
 at org.apache.struts2.dispatcher.ServletDispatcherResult.doExecute(ServletDispatcherResult.java:154)
 at org.apache.struts2.dispatcher.StrutsResultSupport.execute(StrutsResultSupport.java:186)
 at com.opensymphony.xwork2.DefaultActionInvocation.executeResult(DefaultActionInvocation.java:361)
 at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:265)
 at com.eginnovations.common.interceptor.AuthorizationInterceptor.intercept(AuthorizationInterceptor.java:42)
 at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
 at org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:52)
 at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:468)
 at org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
 at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:76)
 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
 at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
 at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
 at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
 at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
 at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
 at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
 at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:852)
 at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
 at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
 at java.lang.Thread.run(Unknown Source)
May 12, 2010 10:40:24 AM org.apache.catalina.core.ApplicationDispatcher invoke
SEVERE: Servlet.service() for servlet jsp threw exception
Expression eKey is undefined on line 71, column 63 in template/simple/fielderror.ftl.
The problematic instruction:
----------
==> assignment: eValue=fieldErrors[eKey] [on line 71, column 33 in template/simple/fielderror.ftl]
----------
Java backtrace for programmers:
----------
freemarker.core.InvalidReferenceException: Expression eKey is undefined on line 71, column 63 in template/simple/fielderror.ftl.
 at freemarker.core.TemplateObject.assertNonNull(TemplateObject.java:124)
 at freemarker.core.DynamicKeyName._getAsTemplateModel(DynamicKeyName.java:85)
 at freemarker.core.Expression.getAsTemplateModel(Expression.java:89)
 at freemarker.core.Assignment.accept(Assignment.java:90)
 at freemarker.core.Environment.visit(Environment.java:208)
 at freemarker.core.MixedContent.accept(MixedContent.java:92)
 at freemarker.core.Environment.visit(Environment.java:208)
 at freemarker.core.IteratorBlock$Context.runLoop(IteratorBlock.java:167)
 at freemarker.core.Environment.visit(Environment.java:415)
 at freemarker.core.IteratorBlock.accept(IteratorBlock.java:102)
 at freemarker.core.Environment.visit(Environment.java:208)
 at freemarker.core.MixedContent.accept(MixedContent.java:92)
 at freemarker.core.Environment.visit(Environment.java:208)
 at freemarker.core.ConditionalBlock.accept(ConditionalBlock.java:79)
 at freemarker.core.Environment.visit(Environment.java:208)
 at freemarker.core.IfBlock.accept(IfBlock.java:82)
 at freemarker.core.Environment.visit(Environment.java:208)
 at freemarker.core.MixedContent.accept(MixedContent.java:92)
 at freemarker.core.Environment.visit(Environment.java:208)
 at freemarker.core.ConditionalBlock.accept(ConditionalBlock.java:79)
 at freemarker.core.Environment.visit(Environment.java:208)
 at freemarker.core.MixedContent.accept(MixedContent.java:92)
 at freemarker.core.Environment.visit(Environment.java:208)
 at freemarker.core.Environment.process(Environment.java:188)
 at freemarker.template.Template.process(Template.java:237)
 at org.apache.struts2.components.template.FreemarkerTemplateEngine.renderTemplate(FreemarkerTemplateEngine.java:182)
 at org.apache.struts2.components.UIBean.mergeTemplate(UIBean.java:559)
 at org.apache.struts2.components.UIBean.end(UIBean.java:513)
 at org.apache.struts2.views.jsp.ComponentTagSupport.doEndTag(ComponentTagSupport.java:42)
 at org.apache.jsp.index_jsp._jspx_meth_s_005ffielderror_005f0(index_jsp.java:193)
 at org.apache.jsp.index_jsp._jspx_meth_s_005fform_005f0(index_jsp.java:132)
 at org.apache.jsp.index_jsp._jspService(index_jsp.java:78)
 at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
 at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
 at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:377)
 at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
 at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
 at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
 at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:646)
 at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:436)
 at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:374)
 at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:302)
 at org.apache.struts2.dispatcher.ServletDispatcherResult.doExecute(ServletDispatcherResult.java:154)
 at org.apache.struts2.dispatcher.StrutsResultSupport.execute(StrutsResultSupport.java:186)
 at com.opensymphony.xwork2.DefaultActionInvocation.executeResult(DefaultActionInvocation.java:361)
 at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:265)
 at com.eginnovations.common.interceptor.AuthorizationInterceptor.intercept(AuthorizationInterceptor.java:42)
 at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
 at org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:52)
 at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:468)
 at org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
 at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:76)
 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
 at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
 at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
 at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
 at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
 at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
 at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
 at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:852)
 at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
 at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
 at java.lang.Thread.run(Unknown Source)

problem with multiple tiles configuration files

This is an interesting problem for this newbie. I have one common tiles configuration file tiles.xml. I decided to split it into pieces based on modules. The syntax allows me to do it. the web.xml entry changed from

<context-param>
 <param-name>org.apache.tiles.impl.BasicTilesContainer.DEFINITIONS_CONFIG</param-name>
 <param-value>/WEB-INF/tiles.xml</param-value>
 </context-param>

to

<context-param>
 <param-name>org.apache.tiles.impl.BasicTilesContainer.DEFINITIONS_CONFIG</param-name>
 <param-value>/WEB-INF/tiles-license.xml,/WEB-INF/tiles-common.xml</param-value>
 </context-param>

Fortunately/Unfortunately, I declared all my basic templates in tiles-common.xml which is reused by tiles-license.xml. Pathetic. I ended up with annoying exception

org.apache.tiles.definition.NoSuchDefinitionException: licenseExpiryList
    org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:578)
    org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:246)
    org.apache.struts2.views.tiles.TilesResult.doExecute(TilesResult.java:105)
    org.apache.struts2.dispatcher.StrutsResultSupport.execute(StrutsResultSupport.java:186)

We need to specify the tiles config files in ‘correct’ order. base definitions at first, children at the last.

Ref: http://old.nabble.com/How-can-I-separate-tiles.xml-in-several-files—td13216254.html

org.apache.tiles.definition.NoSuchDefinitionException: licenseExpiryList
	org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:578)
	org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:246)
	org.apache.struts2.views.tiles.TilesResult.doExecute(TilesResult.java:105)
	org.apache.struts2.dispatcher.StrutsResultSupport.execute(StrutsResultSupport.java:186)

Passing Struts values to Javascript

yureka

This another day to get into struts. See, I have an input type as image and onclick function.

<input type=”image” name=”" src=”/teller/images/close.gif” onclick=”del(200);”>

The 200 given as parameter should be passed from struts form bean. Different type of combinations lead to error. finally defining a variable helped as follows.

<bean:define id=”myID” name=”resultI” property=”id”></bean:define>

<html:image src=”/teller/images/close.gif”  onclick=”<%=”del(“+myID+”);” %>”></html:image>

Way to Struts 1.2 DataSource

hurray

Today I had downloaded struts 1.2.7 and tried to write an application. (I know I am selecting a bit old.) I configured the datasource in struts. Alas. When I tried to start my server, tomcat 6.0, I got the error saying "java.lang.ClassNotFoundException: org.apache.commons.dbcp.BasicDataSource". With Googling I found struts-legacy.jar need to downloaded and copied to lib folder of Tomcat. (I couldnt get the recent version of struts-legacy. I found something in archive). Happily restarted the server. I saw java.lang.NoClassDefFoundError: org/apache/commons/pool/impl/GenericObjectPool.. oof.. again I need to download commons-dbcp-1.2.2.jar and commons-pool.jar.

My search come to an end :) It is working fine now.

validator by night


I was trying to look at the validator plugin for struts. Struts counted the number of its beginners by one, ya. that’s me!!

Previously I have a simple collection of jsps and classes. something like get employee number, and display the employee details. employee number is in the form of xxx-xx-xxxx, where an x is a number. I had my form bean that has a validate method, that checks whether the number has been entered and the number entered is in the specified format. see below!

// Validate format of social security number.
private static boolean isValidSsNum(String ssNum) {
if (ssNum.length() < 11) {
return false;
}

for (int i = 0; i < 11; i++) {
if (i == 3 || i == 6) {
if (ssNum.charAt(i) != ‘-’) {
return false;
}
} else if (“0123456789″.indexOf(ssNum.charAt(i)) == -1) {
return false;
}
}

return true;
}

if it returns false, then new ActionError will be added errors and returned from validate() method. then comes the validator chapter :) actually, they tries to keep validation routines in a centralised location and call whenever there is a need.

Though it took time to keep track of the control flow, i feel it is a good practice, which i want to follow. They define the validation rules in an xml (validator-rules.xml) and map these rules to actions with another xml (validation.xml). Necessary changes have been done to FormBeans/DynaBean declarations.

Because of the validator plugin, both client-side and server-side validations becomes easier now.

Forexample, validator-rules.xml has an validator name ‘required’, which check the existance of data in form fields. the javascript code needed to perform this has been written in the xml file itself. So, it is directly embedded in the jsp whereever needed and executed.

Each definition is declared with validator tag. it is used to assign logical name to the routine with name attribute, and class and method for the routine.

Changes to Form Beans
If you have a Form Bean that extends ActionForm, make it to extend ValidatorForm. remove the reset() and validate() methods since they are implicitly given by the Validator framework.

import org.apache.struts.validator.ValidatorForm; public class MyForm extends ValidatorForm { }

The Form bean configuration in struts-config.xml is not changed.



type=”org.grassfield.MyForm”/>

The logical name ‘myForm’ is the one we will use for making validations. keep a note of that!

Then lets go for validation.xml, where we will map the forms with validations.




property=”username” depends=”required”>

If you have used Dynamic Form Beans, then your entries may go like this.


type=”org.apache.struts.validator.DynaValidatorForm”>

yes, you are right. you will be using DynaValidatorForm instead of DynaActionForm.

now coming back to my page… I need to do

1. Change the form to extend ValidatorForm (done!)
2. add a validator-rules.xml file. (yes, it is there )
3. create a validation.xml file with the following entry
I need to validate whether my ssNum is in the form of xxx-xx-xxxx format. so new entry has been added to validation.xml


property=”ssNum” depends=”mask”> mask ^\d{3}-\d{2}-\d{4}$
4. Add the validator plugin to struts-config.xml file

value=”/WEB-INF/validator-rules.xml, /WEB-INF/validation.xml”/>

has been added.

5. Add validation error message to Application Resources. (done)
6. recomplile, repackage and run the webapp!! (done)
it is working fine :)

yes. got a basic idea of validator module. I need to go through how to embed javascript for client side validation and how to internationalise the error messages. then validator may be complete!

See You!
————————-

By default, struts comes with many validations preconfigured. please see http://struts.apache.org/1.2.4/userGuide/dev_validator.html for more details!