问题
I'm using sturts 2 annotation based validations in my class while dispalying action error i got this strange "FreeMarker template error!" This is my entity class.
@Entity
public class User implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long userid;
private String userPhoneNumber;
private String userName;
private String password;
private String full_name;
private String useremail;
@OneToOne
private userType usertypeid;
public Long getUserid() {
return userid;
}
public void setUserid(Long userid) {
this.userid = userid;
}
public String getUserPhoneNumber() {
return userPhoneNumber;
}
@RequiredStringValidator(message = "Enter Phone Number",type = ValidatorType.FIELD)
public void setUserPhoneNumber(String userPhoneNumber) {
this.userPhoneNumber = userPhoneNumber;
}
public String getUserName() {
return userName;
}
@RequiredStringValidator(message = "Enter User Name",type = ValidatorType.FIELD)
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
@RequiredStringValidator(message = "Enter Password",type = ValidatorType.FIELD)
public void setPassword(String password) {
this.password = password;
}
public String getFull_name() {
return full_name;
}
@RequiredStringValidator(message = "Enter Full Name",type = ValidatorType.FIELD)
public void setFull_name(String full_name) {
this.full_name = full_name;
}
public String getUseremail() {
return useremail;
}
@RequiredStringValidator(message = "Enter Email Id",type = ValidatorType.FIELD)
@EmailValidator(message = "Enter Valid Email Id",type = ValidatorType.FIELD)
public void setUseremail(String useremail) {
this.useremail = useremail;
}
public userType getUsertypeid() {
return usertypeid;
}
public void setUsertypeid(userType usertypeid) {
this.usertypeid = usertypeid;
}
}
This is my Struts.xml file action code
<action name="saveorupdate" class="com.elegant.purchasemodule.purchasemasters.user.UserAction"
method="saveOrUpdate">
<interceptor-ref name="defaultStackHibernateStrutsValidation"/>
<result name="input">/AddUser.jsp</result>
<result name="success" type="redirect">userlist</result>
</action>
This is my jsp page
<s:form action="saveorupdate" method="post" cssClass="form-horizontal" theme="simple" >
<s:push value="u">
<s:hidden name="userid" id="userid"></s:hidden>
<fieldset>
<legend>User</legend>
<div class="form-group">
<label class="col-md-3 control-label" for="textinput">User Name</label>
<div class="col-md-3">
<s:textfield id="userName" name="userName" cssClass="form-control input-md"></s:textfield>
<div id="userNameError" style="color: red"></div>
<s:fielderror fieldName="userName" cssClass="alert-danger"/>
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label" for="textinput">Full Name</label>
<div class="col-md-3">
<s:textfield id="full_name" name="full_name" cssClass="form-control input-md" ></s:textfield>
<div id="full_nameError" style="color: red"></div>
<s:fielderror fieldName="full_name" cssClass="alert-danger"/>
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label" for="textinput">Contact No</label>
<div class="col-md-3">
<s:textfield id="userPhoneNumber" name="userPhoneNumber" cssClass="form-control input-md"></s:textfield>
<div id="contactNoError" style="color: red"></div>
<s:fielderror fieldName="userPhoneNumber" cssClass="alert-danger"/>
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label" for="textinput">Email Id</label>
<div class="col-md-3">
<s:textfield id="useremail" name="useremail" cssClass="form-control input-md" ></s:textfield>
<div id="emailIdError" style="color: red"></div>
<s:fielderror fieldName="useremail" cssClass="alert-danger"/>
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label" for="textinput">Password</label>
<div class="col-md-3">
<s:password id="password" name="password" cssClass="form-control"/>
<div id="passwordError" style="color: red"></div>
<s:fielderror fieldName="password" cssClass="alert-danger"/>
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label" for="selectbasic">User Type</label>
<div class="col-md-3">
<s:select id="UserType"
label="UserType"
headerKey="-1" headerValue="Select Status"
list="usertylist"
listValue="usertypename"
listKey="usertypeid"
cssClass='form-control input-md'
name="usertypeid.usertypeid"/>
</div>
<div id="UserTypeError" style="color: red"></div>
</div>
<div class="form-group">
<label class="col-md-4 control-label" for="singlebutton"> </label>
<div class="col-md-4">
<button id="save" type="submit" name="save" class="btn btn-primary">Save</button>
<button type="reset" value="reset" class="btn btn-primary">Reset</button>
<button type="button" onclick="window.location.href = 'userlist'" class="btn btn-primary ">Cancel</button>
</div>
</div>
</fieldset>
</s:push>
</s:form>
After submitting the form it showing this error eventhough i'm giving valid details
org.apache.jasper.JasperException: Expression eKey is undefined on line 32, column 23 in template/simple/fielderror.ftl. - Class: freemarker.core.TemplateObject
File: TemplateObject.java
Method: assertNonNull
Line: 125 - freemarker/core/TemplateObject.java:125:-1
at org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:570)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:457)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:391)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:749)
at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:487)
at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:412)
at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:339)
at org.apache.struts2.dispatcher.ServletDispatcherResult.doExecute(ServletDispatcherResult.java:164)
at org.apache.struts2.dispatcher.StrutsResultSupport.execute(StrutsResultSupport.java:186)
at com.opensymphony.xwork2.DefaultActionInvocation.executeResult(DefaultActionInvocation.java:371)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:275)
at com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:265)
at org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept (AnnotationValidationInterceptor.java:68)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
at com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:138)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:249)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:249)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
at com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:191)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
at org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:73)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
at org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:91)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
at org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:252)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
at com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:100)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
at com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept (ScopedModelDrivenInterceptor.java:141)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
at com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:145)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
at com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:171)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
at com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:139)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
at org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
at com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:193)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
at com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:189)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
at com.googlecode.s2hibernate.struts2.plugin.interceptors.SessionTransactionInjectorInterceptor.intercept (SessionTransactionInjectorInterceptor.java:157)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
at org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:54)
at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:562)
at org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter (StrutsPrepareAndExecuteFilter.java:99)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.displaytag.filter.ResponseOverrideFilter.doFilter(ResponseOverrideFilter.java:125)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)
回答1:
The exception is telling you exactly where to look for the error:
org.apache.jasper.JasperException: Expression eKey is undefined on line 32, column 23 in template/simple/fielderror.ftl. - Class: freemarker.core.TemplateObject
The culprit is a fieldError
with an unmatching fieldName
. After the tests (in the comments) it turned out to be full_name
, appearently the underscore is not correctly handled through the whole chain:
<s:textfield id="full_name" name="full_name" ... />
...
<s:fielderror fieldName="full_name" cssClass="alert-danger"/>
full_name
should be then changed to fullName
as long with the variable and its Getter and Setter in order to work:
<s:fielderror fieldName="fullName" cssClass="alert-danger"/>
Also ensure to have all the latest and aligned struts libraries and plugins, with the correct version of external dependencies (Hibernate validator, etc)
That said, I want to give you a couple of advices about variables naming:
Always use camelCase for variables with multiple words:
password
ispassword
(a single word, though composite), butusername
anduseremail
should beuserName
anduserEmail
. You've used 3 different conventions for 6 variables of a single Class...Since those are the variables of the
User
Class , avoid the redundantuser
in the variables names: userid = id, username = name, userPhoneNumber = phoneNumber, etc... If you did that for more readability in the JSP, then just avoid using the<s:push>
tag that might confound.
If you are starting, altering the database column names should not be a pain... otherwise, keep this advices for your next project.
P.S: note that with JPA and Hibernate you can use camelCase variables and underscored column names (phoneNumber
in Entity, phone_number
in database) automatically with this setting in persistence.xml:
<property name="hibernate.ejb.naming_strategy"
value="org.hibernate.cfg.ImprovedNamingStrategy" />
来源:https://stackoverflow.com/questions/27716690/freemarker-template-error-in-struts2