Java 选择标记中的列表属性可以';在Struts 2中使用ModelDriven接口时无法解决此问题
我在Struts 2应用程序中使用接口Java 选择标记中的列表属性可以';在Struts 2中使用ModelDriven接口时无法解决此问题,java,jsp,collections,struts2,model-driven,Java,Jsp,Collections,Struts2,Model Driven,我在Struts 2应用程序中使用接口ModelDriven。我在呈现页面时遇到问题,因为我总是遇到错误: 19 nov. 2013 11:23:12 org.apache.catalina.core.StandardWrapperValve invoke GRAVE: "Servlet.service()" pour la servlet jsp a généré une exception tag 'select', field 'list': The requested
ModelDriven
。我在呈现页面时遇到问题,因为我总是遇到错误:
19 nov. 2013 11:23:12 org.apache.catalina.core.StandardWrapperValve invoke
GRAVE: "Servlet.service()" pour la servlet jsp a généré une exception
tag 'select', field 'list': The requested list key 'listeItems' could not be resolved as a collection/array/map/enumeration/iterator type. Example: people or people.{name} - [unknown location]
at org.apache.struts2.components.Component.fieldError(Component.java:240)
at org.apache.struts2.components.Component.findValue(Component.java:333)
at org.apache.struts2.components.ListUIBean.evaluateExtraParams(ListUIBean.java:80)
我不知道哪里出错了,所以我向社区求助
struts.xml
:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.devMode" value="false" />
<constant name="struts.action.extension" value="do" />
<constant name="struts.custom.i18n.resources" value="com.omb.i18n.StrutsResourceBundle" />
<constant name="struts.objectFactory" value="org.apache.struts2.spring.StrutsSpringObjectFactory" />
<constant name="struts.objectFactory.spring.autoWire" value="name" />
<constant name="struts.i18n.encoding" value="ISO-8859-1" />
<constant name="struts.i18n.reload" value="false" />
<constant name="struts.configuration.xml.reload" value="false" />
<constant name="struts.locale" value="fr" />
<constant name="struts.multipart.maxSize" value="100000000000" />
<constant name="struts.enable.SlashesInActionNames" value="true" />
<constant name="struts.mapper.alwaysSelectFullNamespace" value="false"/>
<constant name="struts.codebehind.classSuffix" value="Controller"/>
<constant name="struts.codebehind.action.checkImplementsAction" value="false"/>
<constant name="struts.codebehind.action.checkAnnotation" value="false"/>
<constant name="struts.codebehind.action.defaultMethodName" value="index"/>
<constant name="struts.configuration.classpath.defaultParentPackage" value="rest-default" />
<package name="default" extends="tiles-default" namespace="/">
<interceptors>
<interceptor name="params-filter"
class="com.opensymphony.xwork2.interceptor.ParameterFilterInterceptor" />
<interceptor-stack name="defaultStack">
<interceptor-ref name="exception" />
<interceptor-ref name="alias" />
<interceptor-ref name="servletConfig" />
<interceptor-ref name="i18n" />
<interceptor-ref name="chain" />
<interceptor-ref name="modelDriven" />
<interceptor-ref name="fileUpload">
<param name="maximumSize">11204928</param>
</interceptor-ref>
<interceptor-ref name="staticParams" />
<interceptor-ref name="conversionError" />
<interceptor-ref name="params" />
<interceptor-ref name="prepare" />
<interceptor-ref name="basicStack"/>
<interceptor-ref name="validation" />
<interceptor-ref name="workflow" />
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="defaultStack" />
<global-results>
<result name="technicalError" type="chain">
errorAction
</result>
<result name="sessionInvalidError" type="tiles">
sessionInvalid
</result>
<result name="blank" type="tiles">blank</result>
</global-results>
<global-exception-mappings>
<exception-mapping exception="java.lang.Exception"
result="technicalError" />
<exception-mapping
exception="com.omb.service.exception.UserSessionInvalidException"
result="sessionInvalidError" />
</global-exception-mappings>
</package>
<package name="omb" extends="default" namespace="/omb">
<action name="*Action" class="myAction" method="{1}">
<result name="success" type="redirectAction">
<param name="namespace">/omb</param>
<param name="actionName">displayResult</param>
</result>
<result name="error" type="redirectAction">
<param name="namespace">/error</param>
<param name="actionName">displayError</param>
</result>
</action>
</package>
</struts>
package com.omb.actions;
public class MyAction extends ActionSupport implements ModelDriven<MyModel>{
private MyModel myModel = new MyModel();
public MyModel getModel() {
return myModel;
}
public String execute() throws Exception {
myModel.add(new Item("A", "Item A"));
myModel.add(new Item("B", "Item B"));
return SUCCESS;
}
public String doAction() {
// do something
return "SUCCESS";
}
public MyModel getMyModel() {
return this.myModel;
}
public void setMyModel(MyModel myModel) {
this.myModel = myModel;
}
}
package com.omb.modele;
import java.util.ArrayList;
import java.util.List;
import com.omb.item.Item;
public class MyModel {
private String idItem;
private List<Item> listeItems = new ArrayList<Item>();
public String getIdItem() {
return this.idItem;
}
public void setIdItem(String idItem) {
this.idItem = idItem;
}
public List<Item> getListeItems() {
return this.listeItems;
}
public void setListeItems(List<Item> listeItems) {
this.listeItems = listeItems;
}
}
package com.omb.item;
import java.io.Serializable;
public class Item implements Serializable {
private static final long serialVersionUID = 1L;
private String id;
private String label;
public Item() {
super();
}
public Item(String id, String label) {
this.id = id;
this.label = label;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
}
}
<%@ taglib prefix="s" uri="/struts-tags"%>
<table width="100%">
<tr>
<td><label><s:property value="%{getText('label')}" /></label></td>
</tr>
<tr>
<td><s:select id="idSelectItem"
emptyOption="true" list="listeItems" value="idItem"
listKey="id" listValue="label" />
</td>
</tr>
</table>
MyModel.java
:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.devMode" value="false" />
<constant name="struts.action.extension" value="do" />
<constant name="struts.custom.i18n.resources" value="com.omb.i18n.StrutsResourceBundle" />
<constant name="struts.objectFactory" value="org.apache.struts2.spring.StrutsSpringObjectFactory" />
<constant name="struts.objectFactory.spring.autoWire" value="name" />
<constant name="struts.i18n.encoding" value="ISO-8859-1" />
<constant name="struts.i18n.reload" value="false" />
<constant name="struts.configuration.xml.reload" value="false" />
<constant name="struts.locale" value="fr" />
<constant name="struts.multipart.maxSize" value="100000000000" />
<constant name="struts.enable.SlashesInActionNames" value="true" />
<constant name="struts.mapper.alwaysSelectFullNamespace" value="false"/>
<constant name="struts.codebehind.classSuffix" value="Controller"/>
<constant name="struts.codebehind.action.checkImplementsAction" value="false"/>
<constant name="struts.codebehind.action.checkAnnotation" value="false"/>
<constant name="struts.codebehind.action.defaultMethodName" value="index"/>
<constant name="struts.configuration.classpath.defaultParentPackage" value="rest-default" />
<package name="default" extends="tiles-default" namespace="/">
<interceptors>
<interceptor name="params-filter"
class="com.opensymphony.xwork2.interceptor.ParameterFilterInterceptor" />
<interceptor-stack name="defaultStack">
<interceptor-ref name="exception" />
<interceptor-ref name="alias" />
<interceptor-ref name="servletConfig" />
<interceptor-ref name="i18n" />
<interceptor-ref name="chain" />
<interceptor-ref name="modelDriven" />
<interceptor-ref name="fileUpload">
<param name="maximumSize">11204928</param>
</interceptor-ref>
<interceptor-ref name="staticParams" />
<interceptor-ref name="conversionError" />
<interceptor-ref name="params" />
<interceptor-ref name="prepare" />
<interceptor-ref name="basicStack"/>
<interceptor-ref name="validation" />
<interceptor-ref name="workflow" />
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="defaultStack" />
<global-results>
<result name="technicalError" type="chain">
errorAction
</result>
<result name="sessionInvalidError" type="tiles">
sessionInvalid
</result>
<result name="blank" type="tiles">blank</result>
</global-results>
<global-exception-mappings>
<exception-mapping exception="java.lang.Exception"
result="technicalError" />
<exception-mapping
exception="com.omb.service.exception.UserSessionInvalidException"
result="sessionInvalidError" />
</global-exception-mappings>
</package>
<package name="omb" extends="default" namespace="/omb">
<action name="*Action" class="myAction" method="{1}">
<result name="success" type="redirectAction">
<param name="namespace">/omb</param>
<param name="actionName">displayResult</param>
</result>
<result name="error" type="redirectAction">
<param name="namespace">/error</param>
<param name="actionName">displayError</param>
</result>
</action>
</package>
</struts>
package com.omb.actions;
public class MyAction extends ActionSupport implements ModelDriven<MyModel>{
private MyModel myModel = new MyModel();
public MyModel getModel() {
return myModel;
}
public String execute() throws Exception {
myModel.add(new Item("A", "Item A"));
myModel.add(new Item("B", "Item B"));
return SUCCESS;
}
public String doAction() {
// do something
return "SUCCESS";
}
public MyModel getMyModel() {
return this.myModel;
}
public void setMyModel(MyModel myModel) {
this.myModel = myModel;
}
}
package com.omb.modele;
import java.util.ArrayList;
import java.util.List;
import com.omb.item.Item;
public class MyModel {
private String idItem;
private List<Item> listeItems = new ArrayList<Item>();
public String getIdItem() {
return this.idItem;
}
public void setIdItem(String idItem) {
this.idItem = idItem;
}
public List<Item> getListeItems() {
return this.listeItems;
}
public void setListeItems(List<Item> listeItems) {
this.listeItems = listeItems;
}
}
package com.omb.item;
import java.io.Serializable;
public class Item implements Serializable {
private static final long serialVersionUID = 1L;
private String id;
private String label;
public Item() {
super();
}
public Item(String id, String label) {
this.id = id;
this.label = label;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
}
}
<%@ taglib prefix="s" uri="/struts-tags"%>
<table width="100%">
<tr>
<td><label><s:property value="%{getText('label')}" /></label></td>
</tr>
<tr>
<td><s:select id="idSelectItem"
emptyOption="true" list="listeItems" value="idItem"
listKey="id" listValue="label" />
</td>
</tr>
</table>
JSP文件:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.devMode" value="false" />
<constant name="struts.action.extension" value="do" />
<constant name="struts.custom.i18n.resources" value="com.omb.i18n.StrutsResourceBundle" />
<constant name="struts.objectFactory" value="org.apache.struts2.spring.StrutsSpringObjectFactory" />
<constant name="struts.objectFactory.spring.autoWire" value="name" />
<constant name="struts.i18n.encoding" value="ISO-8859-1" />
<constant name="struts.i18n.reload" value="false" />
<constant name="struts.configuration.xml.reload" value="false" />
<constant name="struts.locale" value="fr" />
<constant name="struts.multipart.maxSize" value="100000000000" />
<constant name="struts.enable.SlashesInActionNames" value="true" />
<constant name="struts.mapper.alwaysSelectFullNamespace" value="false"/>
<constant name="struts.codebehind.classSuffix" value="Controller"/>
<constant name="struts.codebehind.action.checkImplementsAction" value="false"/>
<constant name="struts.codebehind.action.checkAnnotation" value="false"/>
<constant name="struts.codebehind.action.defaultMethodName" value="index"/>
<constant name="struts.configuration.classpath.defaultParentPackage" value="rest-default" />
<package name="default" extends="tiles-default" namespace="/">
<interceptors>
<interceptor name="params-filter"
class="com.opensymphony.xwork2.interceptor.ParameterFilterInterceptor" />
<interceptor-stack name="defaultStack">
<interceptor-ref name="exception" />
<interceptor-ref name="alias" />
<interceptor-ref name="servletConfig" />
<interceptor-ref name="i18n" />
<interceptor-ref name="chain" />
<interceptor-ref name="modelDriven" />
<interceptor-ref name="fileUpload">
<param name="maximumSize">11204928</param>
</interceptor-ref>
<interceptor-ref name="staticParams" />
<interceptor-ref name="conversionError" />
<interceptor-ref name="params" />
<interceptor-ref name="prepare" />
<interceptor-ref name="basicStack"/>
<interceptor-ref name="validation" />
<interceptor-ref name="workflow" />
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="defaultStack" />
<global-results>
<result name="technicalError" type="chain">
errorAction
</result>
<result name="sessionInvalidError" type="tiles">
sessionInvalid
</result>
<result name="blank" type="tiles">blank</result>
</global-results>
<global-exception-mappings>
<exception-mapping exception="java.lang.Exception"
result="technicalError" />
<exception-mapping
exception="com.omb.service.exception.UserSessionInvalidException"
result="sessionInvalidError" />
</global-exception-mappings>
</package>
<package name="omb" extends="default" namespace="/omb">
<action name="*Action" class="myAction" method="{1}">
<result name="success" type="redirectAction">
<param name="namespace">/omb</param>
<param name="actionName">displayResult</param>
</result>
<result name="error" type="redirectAction">
<param name="namespace">/error</param>
<param name="actionName">displayError</param>
</result>
</action>
</package>
</struts>
package com.omb.actions;
public class MyAction extends ActionSupport implements ModelDriven<MyModel>{
private MyModel myModel = new MyModel();
public MyModel getModel() {
return myModel;
}
public String execute() throws Exception {
myModel.add(new Item("A", "Item A"));
myModel.add(new Item("B", "Item B"));
return SUCCESS;
}
public String doAction() {
// do something
return "SUCCESS";
}
public MyModel getMyModel() {
return this.myModel;
}
public void setMyModel(MyModel myModel) {
this.myModel = myModel;
}
}
package com.omb.modele;
import java.util.ArrayList;
import java.util.List;
import com.omb.item.Item;
public class MyModel {
private String idItem;
private List<Item> listeItems = new ArrayList<Item>();
public String getIdItem() {
return this.idItem;
}
public void setIdItem(String idItem) {
this.idItem = idItem;
}
public List<Item> getListeItems() {
return this.listeItems;
}
public void setListeItems(List<Item> listeItems) {
this.listeItems = listeItems;
}
}
package com.omb.item;
import java.io.Serializable;
public class Item implements Serializable {
private static final long serialVersionUID = 1L;
private String id;
private String label;
public Item() {
super();
}
public Item(String id, String label) {
this.id = id;
this.label = label;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
}
}
<%@ taglib prefix="s" uri="/struts-tags"%>
<table width="100%">
<tr>
<td><label><s:property value="%{getText('label')}" /></label></td>
</tr>
<tr>
<td><s:select id="idSelectItem"
emptyOption="true" list="listeItems" value="idItem"
listKey="id" listValue="label" />
</td>
</tr>
</table>
删除method=“{1}”
,因为您不使用通配符映射,并且您的操作除了执行execute
之外没有其他方法,只有此方法初始化列表。如果列表未初始化,则会出现上述错误。如果有其他方法没有显示在那里,那么应该为操作实现,并将初始化列表的代码移到那里
public class MyAction extends ActionSupport implements ModelDriven<MyModel>, Preparable {
public void prepare() {
myModel = new MyModel();
myModel.add(new Item("A", "Item A"));
myModel.add(new Item("B", "Item B"));
}
...
}
公共类MyAction扩展了ActionSupport实现了模型驱动、可准备的{
公众假期准备(){
myModel=新的myModel();
添加(新项目(“A”、“A”项目);
myModel.add(新项目(“B”、“B项”);
}
...
}
您好,我的操作还有许多其他方法可以执行。只是在我的匿名代码中我也删除了。抱歉,我将更新我的示例。如果列表已初始化,它将不再引发此类异常prepare()
方法在该类执行任何操作之前执行。谢谢,我实现了这一点,但我有一个NullPointerException,因为我有一个由拦截器赋值的属性。所以我觉得prepare()方法是在拦截器之前调用的。是这样吗?我是Struts2技术的新手,我以前做过很多JSF,我继承了一个应用程序,人们在添加超级复杂的覆盖Struts2之前。。。。所以进化是非常重要的difficult@OusmaneMINTE您可以在prepare()
中初始化,我已经更新了代码。