我在课堂上使用基于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)
例外是告诉您确切在哪里查找错误:
org.apache.jasper.JasperException:表达式eKey在template / simple / fielderror.ftl的 第32行第23列中 未定义 。-类:freemarker.core.TemplateObject
罪魁祸首是fieldError无与伦比的fieldName。经过测试(在注释中),结果为full_name, 显然 在整个链中 未正确处理下划线 :
fieldError
fieldName
full_name
<s:textfield id="full_name" name="full_name" ... /> ... <s:fielderror fieldName="full_name" cssClass="alert-danger"/>
full_name然后应fullName使用变量及其Getter和Setter 更改为与之一样长,以便工作:
fullName
<s:fielderror fieldName="fullName" cssClass="alert-danger"/>
还要确保拥有所有最新且一致的struts库和插件,并带有正确版本的外部依赖项(Hibernate验证器等)
就是说,我想给您一些有关变量命名的建议:
始终对带有多个单词的变量使用camelCase:passwordis password(单个单词,虽然是复合单词),但usernameand useremail应该是userNameand userEmail。您已针对单个类的6个变量使用了3种不同的约定…
password
username
useremail
userName
userEmail
由于这些是UserClass 的变量,因此请避免user在变量名称中重复:userid = id,username = name,userPhoneNumber = phoneNumber等…如果您这样做是为了在JSP中提高可读性,则只需避免使用<s:push>标记可能会感到困惑。
User
user
<s:push>
如果您刚开始,更改数据库列名称应该很容易…否则,请在下一个项目中保留此建议。
PS:请注意,使用JPA和Hibernate,您可以在persistence.xml中使用此设置自动使用camelCase变量和带下划线的列名(phoneNumber在Entity中,phone_number在数据库中):
phoneNumber
phone_number
<property name="hibernate.ejb.naming_strategy" value="org.hibernate.cfg.ImprovedNamingStrategy" />