初学struts2没多久,在用到级联菜单时遇到以下两个问题,相信很多人也会遇到。
一、FreeMarker问题
例如下面的代码:
<s:set name="foobar" value="#{'Java': {'Spring', 'Hibernate', 'Struts 2'}, '.Net': {'Linq', ' ASP.NET 2.0'}, 'Database': {'Oracle', 'SQL Server', 'DB2', 'MySQL'}}" /> <s:doubleselect list="#foobar.keySet()" doubleName="technology" doubleList="#foobar[top]" label="Technology"/>
这个代码没有在一个<s:form>标签里,执行之后,显示就会出现以下错误:
freemarker.core.InvalidReferenceException: Expression parameters.formName is undefined on line 98, column 43 in template/simple/doubleselect.ftl. at freemarker.core.TemplateObject.assertNonNull(TemplateObject.java:124) at freemarker.core.Expression.getStringValue(Expression.java:118) at freemarker.core.Expression.getStringValue(Expression.java:93) at freemarker.core.DollarVariable.accept(DollarVariable.java:76) at freemarker.core.Environment.visit(Environment.java:196) at freemarker.core.MixedContent.accept(MixedContent.java:92) at freemarker.core.Environment.visit(Environment.java:196) at freemarker.core.Environment.include(Environment.java:1375) at freemarker.core.Include.accept(Include.java:155) at freemarker.core.Environment.visit(Environment.java:196) at freemarker.core.MixedContent.accept(MixedContent.java:92) at freemarker.core.Environment.visit(Environment.java:196) at freemarker.core.Environment.process(Environment.java:176) at freemarker.template.Template.process(Template.java:232) at org.apache.struts2.components.template.FreemarkerTemplateEngine.renderTemplate(FreemarkerTemplateEngine.java:168) at org.apache.struts2.components.UIBean.mergeTemplate(UIBean.java:530) at org.apache.struts2.components.UIBean.end(UIBean.java:484) at org.apache.struts2.views.jsp.ComponentTagSupport.doEndTag(ComponentTagSupport.java:43) at org.apache.jsp.hello_jsp._jspx_meth_s_005fdoubleselect_005f1(hello_jsp.java:613) at org.apache.jsp.hello_jsp._jspx_meth_s_005fform_005f0(hello_jsp.java:185) at org.apache.jsp.hello_jsp._jspService(hello_jsp.java:104) at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) at javax.servlet.http.HttpServlet.service(HttpServlet.java:717) at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:388) at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313) at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260) at javax.servlet.http.HttpServlet.service(HttpServlet.java:717) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:413) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at com.yuqiaotech.simplejee.webapp.servlet.SampleFilter.doFilter(SampleFilter.java:46) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at com.yuqiaotech.simplejee.webapp.servlet.SimpleClickstreamFilter.doFilter(SimpleClickstreamFilter.java:51) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:291) at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:877) at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:594) at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1675) at java.lang.Thread.run(Thread.java:619)
<s:set name="foobar" value="#{'Java': {'Spring', 'Hibernate', 'Struts 2'}, '.Net': {'Linq', ' ASP.NET 2.0'}, 'Database': {'Oracle', 'SQL Server', 'DB2', 'MySQL'}}" /> <s:doubleselect list="#foobar.keySet()" doubleName="technology" doubleList="#foobar[top]" label="Technology" formName="aaa"/>
这样就不会出现上述错误,但紧接着有第二个问题。
二、二级菜单没有值,不显示
这个问题非常好解决,但我也在网上找了很久,不过我也小白了一把,相信很多人在真正用时,都会把这个<s:doubleselect>放在一个FORM里,不然它也就没有意义了。
所以做如下改动就可以了,并且第一个问题中提到要加formName属性也可以去掉了,如下:
<s:form action="Menu" name="aaa"> <s:set name="foobar" value="#{'Java': {'Spring', 'Hibernate', 'Struts 2'}, '.Net': {'Linq', ' ASP.NET 2.0'}, 'Database': {'Oracle', 'SQL Server', 'DB2', 'MySQL'}}" /> <s:doubleselect list="#foobar.keySet()" doubleName="technology" doubleList="#foobar[top]" label="Technology"/> </s:form>