gpt4 book ai didi

java - 如何从 Struts 2 标签访问 servlet 应用上下文?

转载 作者:行者123 更新时间:2023-11-29 06:02:50 24 4
gpt4 key购买 nike

如何访问 struts 中的 jsp 标签?例如:

<s:select name="country" list="<%=countryList%>"  headerKey="0" headerValue="Country"
label="Select your country" required="true"/>

异常(exception):

Messages: /jsp/index.jsp(35,2) According to TLD or attribute directive in tag file, attribute list does not accept any expressions. countryList is a ArrayList.

最佳答案

这个异常清楚地表明了原因,因为 S2 标记不允许在其中包含此表达式。此外,Tag 需要 List/ArrayList 或任何集合列表作为数据源,内置 ONGL 机制将为您完成其余工作。

你有一个干净的方法来实现这个在你的 Action 类中创建一个名为 countryList 的属性,它的数据类型应该是 List/Map 并为此属性提供一个 getter 和 setter。填写列表在您的操作类中包含所需的数据。

Action 类

public class MyAction extends ActionSupport{

private List<String> countryList;
// getter and setter for countryList

public String execute() throws Exception{
countryList=new ArrayList<String>();
// Add values to list
return SUCCESS;
}
}

现在在您的 JSP 中,您需要执行以下操作

<s:select name="country" list="countryList"  headerKey="0" headerValue="Country"
label="Select your country" required="true"/>

因此,当 OGNL 发现此 list="countryList" 作为数据源时,它将在您的操作类中查找名为 getCountryList() 的方法,并将使用该数据填充选择标记。

希望这能让您清楚地了解这是如何工作的。详情引用官方文档

select tag

关于java - 如何从 Struts 2 标签访问 servlet 应用上下文?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9512980/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com