我是struts2的新手。我正在使用xml进行表单验证。但是我的每个领域都很少会在每次都失效。
失效的filds是: 警察局,父亲的姓名,电话号码和身份证号码。
我对电话号码的验证显示两条错误消息。我在xml和其他版本中提供的是Invalid field value for field "phone".
此外,对于客户端验证,如果我将validate=true"
添加到我的<s:form>
代码,则会在jsp中的表单后面出现以下错误
FreeMarker template error!
Method public java.util.List org.apache.struts2.components.Form.getValidators(java.lang.String) threw an exception when invoked on org.apache.struts2.components.Form@17bb1c6
The problematic instruction:
----------
==> list tag.getValidators("${tagName}") as aValidator [on line 66, column 9 in template/~~~xhtml/form-close-validate.ftl]
in include "/${parameters.templateDir}/${parameters.expandTheme}/form-close-validate.ftl" [on line 25, column 1 in template/xhtml/form-close.ftl]
----------
Java backtrace for programmers:
----------
freemarker.template.TemplateModelException: Method public java.util.List org.apache.struts2.components.Form.getValidators(java.lang.String) threw an exception when invoked on org.apache.struts2.components.Form@17bb1c6
at freemarker.ext.beans.SimpleMethodModel.exec(SimpleMethodModel.java:130)
at freemarker.core.MethodCall._getAsTemplateModel(MethodCall.java:93)
at freemarker.core.Expression.getAsTemplateModel(Expression.java:89)
at freemarker.core.IteratorBlock.accept(IteratorBlock.java:94)
at freemarker.core.Environment.visit(Environment.java:221)
at freemarker.core.IteratorBlock$Context.runLoop(IteratorBlock.java:167)
at freemarker.core.Environment.visit(Environment.java:428)
at freemarker.core.IteratorBlock.accept(IteratorBlock.java:102)
at freemarker.core.Environment.visit(Environment.java:221)
at freemarker.core.MixedContent.accept(MixedContent.java:92)
at freemarker.core.Environment.visit(Environment.java:221)
at freemarker.core.ConditionalBlock.accept(ConditionalBlock.java:79)
at freemarker.core.Environment.visit(Environment.java:221)
at freemarker.core.MixedContent.accept(MixedContent.java:92)
at freemarker.core.Environment.visit(Environment.java:221)
at freemarker.core.Environment.include(Environment.java:1508)
at freemarker.core.Include.accept(Include.java:169)
at freemarker.core.Environment.visit(Environment.java:221)
at freemarker.core.MixedContent.accept(MixedContent.java:92)
at freemarker.core.Environment.visit(Environment.java:221)
at freemarker.core.Environment.process(Environment.java:199)
at freemarker.template.Template.process(Template.java:259)
at org.apache.struts2.components.template.FreemarkerTemplateEngine.renderTemplate(FreemarkerTemplateEngine.java:158)
at org.apache.struts2.components.UIBean.mergeTemplate(UIBean.java:593)
at org.apache.struts2.components.UIBean.end(UIBean.java:547)
at org.apache.struts2.views.jsp.ComponentTagSupport.doEndTag(ComponentTagSupport.java:42)
at org.apache.jsp.register_002dcomplaint_jsp._jspx_meth_s_005fform_005f0(register_002dcomplaint_jsp.java:368)
at org.apache.jsp.register_002dcomplaint_jsp._jspx_meth_s_005fdiv_005f0(register_002dcomplaint_jsp.java:233)
at org.apache.jsp.register_002dcomplaint_jsp._jspService(register_002dcomplaint_jsp.java:166)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:403)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:347)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:301)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:96)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:503)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:136)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:74)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:516)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1015)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:652)
at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:222)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1575)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1533)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:744)
Caused by: java.lang.NullPointerException
at com.opensymphony.xwork2.validator.AnnotationActionValidatorManager.buildValidatorKey(AnnotationActionValidatorMana
并在我的控制台上显示以下错误
SEVERE [http-nio-8084-exec-129] org.apache.catalina.core.StandardWrapperValve.invoke Servlet.service() for servlet [jsp] in context with path [/FIR_System] threw exception [Method public java.util.List org.apache.struts2.components.Form.getValidators(java.lang.String) threw an exception when invoked on org.apache.struts2.components.Form@17bb1c6 - Class: freemarker.ext.beans.SimpleMethodModel
File: SimpleMethodModel.java
Method: exec
Line: 130 - freemarker/ext/beans/SimpleMethodModel.java:130:-1] with root cause
java.lang.NullPointerException
at com.opensymphony.xwork2.validator.AnnotationActionValidatorManager.buildValidatorKey(AnnotationActionValidatorManager.java:237)
at com.opensymphony.xwork2.validator.AnnotationActionValidatorManager.getValidators(AnnotationActionValidatorManager.java:94)
at com.opensymphony.xwork2.validator.AnnotationActionValidatorManager.getValidators(AnnotationActionValidatorManager.java:90)
at org.apache.struts2.components.Form.getValidators(Form.java:280)
at sun.reflect.GeneratedMethodAccessor321.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at freemarker.ext.beans.BeansWrapper.invokeMethod(BeansWrapper.java:866)
at freemarker.ext.beans.SimpleMethodModel.exec(SimpleMethodModel.java:106)
at freemarker.core.MethodCall._getAsTemplateModel(MethodCall.java:93)
at freemarker.core.Expression.getAsTemplateModel(Expression.java:89)
at freemarker.core.IteratorBlock.accept(IteratorBlock.java:94)
at freemarker.core.Environment.visit(Environment.java:221)
at freemarker.core.IteratorBlock$Context.runLoop(IteratorBlock.java:167)
at freemarker.core.Environment.visit(Environment.java:428)
at freemarker.core.IteratorBlock.accept(IteratorBlock.java:102)
at freemarker.core.Environment.visit(Environment.java:221)
at freemarker.core.MixedContent.accept(MixedContent.java:92)
at freemarker.core.Environment.visit(Environment.java:221)
at freemarker.core.ConditionalBlock.accept(ConditionalBlock.java:79)
at freemarker.core.Environment.visit(Environment.java:221)
at freemarker.core.MixedContent.accept(MixedContent.java:92)
at freemarker.core.Environment.visit(Environment.java:221)
at freemarker.core.Environment.include(Environment.java:1508)
at freemarker.core.Include.accept(Include.java:169)
at freemarker.core.Environment.visit(Environment.java:221)
at freemarker.core.MixedContent.accept(MixedContent.java:92)
at freemarker.core.Environment.visit(Environment.java:221)
at freemarker.core.Environment.process(Environment.java:199)
at freemarker.template.Template.process(Template.java:259)
at org.apache.struts2.components.template.FreemarkerTemplateEngine.renderTemplate(FreemarkerTemplateEngine.java:158)
at org.apache.struts2.components.UIBean.mergeTemplate(UIBean.java:593)
at org.apache.struts2.components.UIBean.end(UIBean.java:547)
at org.apache.struts2.views.jsp.ComponentTagSupport.doEndTag(ComponentTagSupport.java:42)
at org.apache.jsp.register_002dcomplaint_jsp._jspx_meth_s_005fform_005f0(register_002dcomplaint_jsp.java:368)
at org.apache.jsp.register_002dcomplaint_jsp._jspx_meth_s_005fdiv_005f0(register_002dcomplaint_jsp.java:233)
at org.apache.jsp.register_002dcomplaint_jsp._jspService(register_002dcomplaint_jsp.java:166)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:403)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:347)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:301)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:96)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:503)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:136)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:74)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:516)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1015)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:652)
at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:222)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1575)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1533)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:744)
请帮我解决这些问题。我使用 Struts 2.3.16 库。
更新当我将某些字段留空时,验证不正常工作。当struts表单标记中的validate="true"
为ON时,异常部分仍未解析。这是更新后的代码。
**更新了jsp表单**
<s:form name="register-complaint" action="registerComplaint validate=""true" method="post">
<s:textfield name="policeStation" label="Nearest Police Station"/>
<s:select name="district" label="District"
list="{'Central Delhi','East Delhi','New Delhi','North Delhi','North-East Delhi','North-West Delhi', 'South Delhi','Soth West Delhi', 'West Delhi'}" />
<s:textfield name="name" label="Your name"/>
<s:textfield name="fathersName" label="Father's Name"/>
<s:textarea name="address" label="Your Address"/>
<s:textfield name="phone" label="Phone Number"/>
<s:textfield name="email" label="Email address"/>
<s:select name="idProof" label="ID Proof"
list="{'Aadhar Card', 'Driving License','PAN Card', 'Passport', 'Electoral Card'}"/>
<s:textfield name="idNumber" label="ID Number"/>
<s:textfield name="distance" label="Distance from nearest police station"/>
<s:select name="direction" label="Direction from nearest Police Station"
list="{'East', 'North', 'North-East', 'North-west', 'South', 'South-east', 'South-west', 'West' }"/>
<sj:datepicker name="dateAndTimeOfOccurance" label="Date and Time of Offence" timepicker="true" timepickerAaPm="true" timepickerGridHour="1" timepickerGridMinute="5" timepickerStepMinute="5"/>
<s:textfield name="offence" label="Offence"/>
<s:textfield name="section" label="section"/>
<s:textarea name="particulars" label="Particulars of the offence"/>
<s:textarea name="descriptionOfAccused" label="Description of accused"/>
<s:textarea name="witness" label="Witnesses (with address)" />
<s:textarea name="compaint" label="Detailed complaint" />
<s:submit name="submit" label="Submit" align="center" />
</s:form>
**更新了验证文件**
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC
"-//Apache Struts//XWork Validator 1.0.3//EN"
"http://struts.apache.org/dtds/xwork-validator-1.0.3.dtd">
<validators>
<field name="policeStation">
<field-validator type="requiredstring">
<message>
Police Station Required.
</message>
</field-validator>
</field>
<field name="name">
<field-validator type="requiredstring">
<message>
Your name is required.
</message>
</field-validator>
</field>
<field name="fathersName">
<field-validator type="requiredstring">
<message>
Father's name required.
</message>
</field-validator>
</field>
<field name="address">
<field-validator type="requiredstring">
<message>
Your address required.
</message>
</field-validator>
</field>
<field name="phone">
<field-validator type="requiredInt">
<message>
Valid Phone number required.
</message>
</field-validator>
</field>
<field name="email">
<field-validator type="email">
<message>
Enter a valid email id.
</message>
</field-validator>
</field>
<field name="idNumber">
<field-validator type="requiredstring">
<message>
Your ID proof number is required.
</message>
</field-validator>
</field>
<field name="distance">
<field-validator type="double">
<message>
Please enter the distance from police station.
</message>
</field-validator>
</field>
<field name="dateAndTimeOfOccurance">
<field-validator type="date">
<message>
Please enter the date and time of incident.
</message>
</field-validator>
</field>
<field name="offence">
<field-validator type="requiredstring">
<message>
Enter the offence.
</message>
</field-validator>
</field>
<field name="particulars">
<field-validator type="requiredstring">
<message>
Provide the details lost/damaged life/property.
</message>
</field-validator>
</field>
<field name="complaint">
<field-validator type="requiredstring">
<message>
Describe your complaint in detail.
</message>
</field-validator>
</field>
</validators>
动作类
public class RegisterComplaintAction extends ActionSupport implements SessionAware, ServletContextAware {
private int id;
private int userId;
private String policeStation;
private String district;
private String name;
private String fathersName;
private String address;
private int phone;
private String email;
private String idProof;
private String idNumber;
private float distance;
private String directionFromPoliceStation;
private Date dateAndTimeOfOccurance;
private String offence;
private String section;
private String particulars;
private String descriptionOfAccused;
private String witness;
private String complaints;
private Map session;
private ServletContext context;
@Override
public void setSession(Map session) {
this.session = session;
}
@Override
public void setServletContext(ServletContext sc) {
this.context = sc;
}
/**
*
* @return @throws Exception
*/
@Override
public String execute() throws Exception {
Connection con = null;
PreparedStatement ps = null;
try {
System.out.println(getDateAndTimeOfOccurance());
Class.forName(context.getInitParameter("db-driver-class"));
con = DriverManager.getConnection(context.getInitParameter("db-url"), context.getInitParameter("db-user"), context.getInitParameter("db-password"));
ps = con.prepareStatement("INSERT INTO FIR.COMPLAINTS (USERNAME, POLICE_STATION, DISTRICT, NAME, FATHER_NAME, ADDRESS, PHONE, EMAIL, ID_PROOF, ID_NUMBER, DISTANCE_FROM_POLICE_STATION, DIRECTION_FROM_POLICE_STATION, DATE_AND_TIME_OF_OCCURANCE, OFFENCE, SEC, PARTICULARS, DESCRIPTION_OF_ACCUSED, WITNESS, COMPLAINTS) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
ps.setString(1, (String) session.get("username"));
ps.setString(2, getPoliceStation());
ps.setString(3, getDistrict());
ps.setString(4, getName());
ps.setString(5, getFathersName());
ps.setString(6, getAddress());
ps.setInt(7, getPhone());
ps.setString(8, getEmail());
ps.setString(9, getIdProof());
ps.setString(10, getIdNumber());
ps.setFloat(11, getDistance());
ps.setString(12, getDirectionFromPoliceStation());
ps.setTimestamp(13, new java.sql.Timestamp(getDateAndTimeOfOccurance().getTime()));
ps.setString(14, getOffence());
ps.setString(15, getSection());
ps.setString(16, getParticulars());
ps.setString(17, getDescriptionOfAccused());
ps.setString(18, getWitness());
ps.setString(19, getComplaints());
if (ps.executeUpdate() != 0) {
session.put("successMessage", "Your Complaint has been successfully registered");
}
} catch (Exception e) {
session.put("errorMessage", "Unable to register your complaint " + e + getDateAndTimeOfOccurance());
return ERROR;
} finally {
ps.close();
con.close();
}
return SUCCESS;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public String getPoliceStation() {
return policeStation;
}
public void setPoliceStation(String policeStation) {
this.policeStation = policeStation;
}
public String getDistrict() {
return district;
}
public void setDistrict(String district) {
this.district = district;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getFathersName() {
return fathersName;
}
public void setFathersName(String fathersName) {
this.fathersName = fathersName;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public int getPhone() {
return phone;
}
public void setPhone(int phone) {
this.phone = phone;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getIdProof() {
return idProof;
}
public void setIdProof(String idProof) {
this.idProof = idProof;
}
public String getIdNumber() {
return idNumber;
}
public void setIdNumber(String idNumber) {
this.idNumber = idNumber;
}
public float getDistance() {
return distance;
}
public void setDistance(float distance) {
this.distance = distance;
}
public String getDirectionFromPoliceStation() {
return directionFromPoliceStation;
}
public void setDirectionFromPoliceStation(String directionFromPoliceStation) {
this.directionFromPoliceStation = directionFromPoliceStation;
}
public Date getDateAndTimeOfOccurance() {
return dateAndTimeOfOccurance;
}
public void setDateAndTimeOfOccurance(Date dateAndTimeOfOccurance) {
System.out.println(dateAndTimeOfOccurance);
this.dateAndTimeOfOccurance = dateAndTimeOfOccurance;
}
public String getOffence() {
return offence;
}
public void setOffence(String offence) {
this.offence = offence;
}
public String getSection() {
return section;
}
public void setSection(String section) {
this.section = section;
}
public String getParticulars() {
return particulars;
}
public void setParticulars(String particulars) {
this.particulars = particulars;
}
public String getDescriptionOfAccused() {
return descriptionOfAccused;
}
public void setDescriptionOfAccused(String descriptionOfAccused) {
this.descriptionOfAccused = descriptionOfAccused;
}
public String getWitness() {
return witness;
}
public void setWitness(String witness) {
this.witness = witness;
}
public String getComplaints() {
return complaints;
}
public void setComplaints(String complaints) {
this.complaints = complaints;
}
}
PS:我因为最大字符限制而被迫从这篇文章中删除以前的代码:(
答案 0 :(得分:2)
电话号码显示两条错误消息,因为您的操作中可能已将电话号码设为int
或long
。
如果我们在您的操作中的数据类型为int
或long
的文本字段中未提供任何内容(甚至不是0),则从JSP表单中默认情况下将空字符串传递给你的行动。
因此,简单地在您的操作setPhone()
方法内部无法将字符串(即空字符串)设置为int
或long
字段&amp;它显示了行动
error i.e Invalid field value for field "phone".
如果您已将手机设为int
或long
,则应使用intRangeValidator
或long validator
代替required
验证码。
freemarker.template.TemplateModelException
- 当您尝试显示列表中的值但是该列表未从操作类传递或者您没有getters
&amp;时,通常会出现此异常。给定列表setters
。
希望这会对你有所帮助。
请出示行动课程以获得进一步的帮助。
答案 1 :(得分:0)
我在我的项目中遇到了同样的问题,然后我在此链接https://coderanch.com/t/555477/framework/FreeMarker-template-error
上找到了一个解决方案只需在jsp页面中更改您的操作,例如
s:form action="loginprocess.action" validate="true"
我希望这会对你有所帮助。