FreeMarker 模板错误!在struts2中
Posted
技术标签:
【中文标题】FreeMarker 模板错误!在struts2中【英文标题】:FreeMarker template error! in struts2 【发布时间】:2015-02-27 06:45:27 【问题描述】:我在课堂上使用基于 sturts 2 注释的验证,同时显示操作错误,我收到了这个奇怪的“FreeMarker 模板错误!”这是我的实体类。
@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;
这是我的 Struts.xml 文件操作代码
<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>
这是我的jsp页面
<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>
提交表单后,即使我提供了有效的详细信息,它仍显示此错误
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】:异常告诉你在哪里寻找错误:
org.apache.jasper.JasperException:在 template/simple/fielderror.ftl 的第 32 行第 23 列未定义表达式 eKey。 - 类:freemarker.core.TemplateObject
罪魁祸首是 fieldError
与不匹配的 fieldName
。 经过测试(在 cmets 中)结果是 full_name
,显然 下划线不正确通过整个链条处理:
<s:textfield id="full_name" name="full_name" ... />
...
<s:fielderror fieldName="full_name" cssClass="alert-danger"/>
然后应将full_name
更改为fullName
,只要使用变量及其Getter 和Setter 才能工作:
<s:fielderror fieldName="fullName" cssClass="alert-danger"/>
还要确保拥有所有最新且一致的 struts 库和插件,以及正确版本的外部依赖项(Hibernate 验证器等)
也就是说,我想给你一些关于变量命名的建议:
-
1234563您对单个类的 6 个变量使用了 3 种不同的约定...
由于这些是 User
类的变量,请避免在变量名称中使用多余的 user
:userid = id、username = name、userPhoneNumber = phoneNumber 等...
如果您这样做是为了在 JSP 中提高可读性,那么请避免使用可能会混淆的 <s:push>
标记。
如果您刚开始,更改数据库列名应该不会很痛苦...否则,请为您的下一个项目保留此建议。
P.S:请注意,使用 JPA 和 Hibernate,您可以在persistence.xml 中自动使用camelCase 变量和带下划线的列名(实体中的phoneNumber
,数据库中的phone_number
):
<property name="hibernate.ejb.naming_strategy"
value="org.hibernate.cfg.ImprovedNamingStrategy" />
【讨论】:
尝试从 full_name 中删除一个,看看它是否是罪魁祸首。我的想法用完了,如果你有另一个 WAR 可以工作,请仔细检查配置、库、注释、jsp 代码中的每一个差异...... 感谢上帝终于解决了。罪魁祸首是全名。刚刚删除了对该变量的验证。我不知道为什么和下划线导致了一个大问题。非常感谢您的持续帮助和您的耐心听我的业余话。 感谢您的大力帮助。以上是关于FreeMarker 模板错误!在struts2中的主要内容,如果未能解决你的问题,请参考以下文章
使用 <@s.generator 和迭代器标签时出现“Freemarker 模板错误”