lee
lee

Reputation: 246

How can I access the servlet application context from Struts 2 tags?

How can I access jsp tags in struts ? for eg:

<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.

Upvotes: 1

Views: 1229

Answers (2)

Umesh Awasthi
Umesh Awasthi

Reputation: 23587

Well the exception is clearly indicating the cause as the S2 tag will not allow this expression inside it. More over Tag require a List/ArrayList or any collection list as a source of Data and build in ONGL mechanism will do the rest of work for you.

You have a clean way to achieve this create a property in your action class with name countryList that should have data type as List/Map and provide a getter and setter for this property.Fill the List with the required data in your action class.

Action Class

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;
 }
}

Now in your JSP all you need to do following

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

So when OGNL will find this list="countryList" as a data source it will look for a method named getCountryList() in your action class and will use the data to fill the select tag.

Hope this will give u clear idea how this works. For details refer the official document

select tag

Upvotes: 2

tom
tom

Reputation: 2745

You do not need to use a java scriptlet for the list.

You have to use an OGNL expression. If your action has a getCountryList method, all you need to do is:

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

You should search some documentation on how to use OGNL in struts. It is really powerful.

Upvotes: 1

Related Questions