struts 使用POJO应注意的一点

对于提交数据比较多的表单,为了避免大量地在FormBean里面使用大量的变量及setter和getter方法,从Struts1.2和Struts1.3起,FormBean中可以使用POJO,提交数据后将直接把数据赋给POJO。

 

 

public class UserBeanForm extends ActionForm {
	private Person person = new Person();
	private String action;
	public Person getPerson() {
		return person;
	}
	public void setPerson(Person person) {
		this.person = person;
	}
	public String getAction() {
		return action;
	}
	public void setAction(String action) {
		this.action = action;
	}

 

 

提示:Struts不会自动创建POJO对象,这里需要用new实例化一个Person对象,否则为Person设置属性时,结合jsp页面会抛出以下错误:

 

 

严重: Servlet.service() for servlet [jsp] in context with path [/struts1] threw exception [javax.servlet.ServletException: javax.servlet.jsp.JspException: Invalid argument looking up property: "person.name" of bean: "org.apache.struts.taglib.html.BEAN"] with root cause
javax.servlet.jsp.JspException: Invalid argument looking up property: "person.name" of bean: "org.apache.struts.taglib.html.BEAN"
	at org.apache.struts.taglib.TagUtils.lookup(TagUtils.java:887)
	at org.apache.struts.taglib.html.BaseFieldTag.prepareValue(BaseFieldTag.java:123)
	at org.apache.struts.taglib.html.BaseFieldTag.renderInputElement(BaseFieldTag.java:102)
	at org.apache.struts.taglib.html.BaseFieldTag.doStartTag(BaseFieldTag.java:80)
	at org.apache.jsp.userInfo_jsp._jspx_meth_html_005ftext_005f0(userInfo_jsp.java:224)
	at org.apache.jsp.userInfo_jsp._jspx_meth_html_005fform_005f0(userInfo_jsp.java:148)
	at org.apache.jsp.userInfo_jsp._jspService(userInfo_jsp.java:116)
	at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
	at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:433)
	at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:389)
	at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:333)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
	at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
	at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:405)
	at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:964)
	at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:515)
	at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:1824)
	at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
	at java.lang.Thread.run(Thread.java:662)

 

看似是jsp的错误,但是实际上是没有创建POJO的错误。

你可能感兴趣的:(struts)