java – FreeMarker模板错误!在struts2中

我在课堂上使用了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">&nbsp;</label>
                <div class="col-md-4">
                    <button id="save" type="submit"  name="save" class="btn btn-primary">Save</button>&nbsp;
                    <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>&nbsp;

                </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)

解决方法:

例外是告诉您确切地查找错误的位置:

org.apache.jasper.JasperException: Expression eKey is undefined on line 32, column 23 in template/simple/fielderror.ftl. – Class: freemarker.core.TemplateObject

罪魁祸首是具有不匹配的fieldName的fieldError.在测试之后(在评论中)它结果是full_name,显然下划线没有通过整个链正确处理:

<s:textfield id="full_name" name="full_name" ... />
...
<s:fielderror fieldName="full_name" cssClass="alert-danger"/>

然后应该使用变量及其Getter和Setter将full_name更改为fullName以便工作:

<s:fielderror fieldName="fullName" cssClass="alert-danger"/>

还要确保拥有所有最新且对齐的struts库和插件,以及正确版本的外部依赖项(Hibernate验证器等)

那就是说,我想给你一些关于变量命名的建议:

>始终将camelCase用于具有多个单词的变量:password是password(单个单词,虽然是复合词),但username和useremail应该是userName和userEmail.您已经为单个类的6个变量使用了3种不同的约定…
>由于这些是用户类的变量,请避免变量名称中的冗余用户: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" />
上一篇:使用freemarker导出word文档


下一篇:springboot集成freemarker 配置freemarker详解