如何访问在设置参数值时创建的seam组件?

时间:2011-12-28 17:08:27

标签: java seam

我的网址有一个参数名称= SSA-COLUMB。 seam参数(在DevicesList.page.xml中设置)是

<param name="name" value="#{searchDeviceName.paramValue}"/>

当我加载页面时,实例化searchDeviceName组件并设置paramValue。我在setParamValue()上用print语句验证了它。这是SearchDeviceName.java的代码

@Name("searchDeviceName")
@Scope(ScopeType.CONVERSATION)
public class SearchDeviceName {

    public String paramValue;
    public Table table;
    public String sqlString;
    public ArrayList<JoinClause> joinList;

    public SearchDeviceName() {
        Table devTable = new Table("devices","d","dev_id");
        setTable(devTable); 
        setSqlString(" d.name like '%"+paramValue+"%'");
    }

    <getters and setters>

}

我有一个无状态会话bean,用于获取此组件的实例以用于构建sql语句。但是当我抓取组件实例时,它没有设置paramValue。显然它是组件的新实例。

SearchDeviceName searchObj = (SearchDeviceName) Component.getInstance("searchDeviceName", ScopeType.CONVERSATION);

这与设置参数值时实例化的searchDeviceName的实例不同。我知道这是因为paramValue为null而不是设置为“SSA-COLUMB”。

有没有办法在我的会话bean中使用在设置参数值时创建的相同组件intance?我怎么抓住它?

以下是抓取组件的代码

@Name("idListBuilder")
public class IdListBuilder {

    @In
    private Context conversationContext;

    @In(create = true)
    EntityManager entityManager;

    private String sqlQuery;
    private Table headTable;

    private ArrayList<String> restrictionValues = new ArrayList<String>();
    private ArrayList<Table> restrictionJoinTables  = new ArrayList<Table>();

    public IdListBuilder(Table table) {
        this.headTable = table;
        this.sqlQuery = "SELECT " + table.alias + "." + table.primaryKey + " FROM " +
                table.name + " " + table.alias;
        searchObjects.add("searchDeviceName");
        searchObjects.add("searchOfficeState");  
    }

    public List<Integer> getIdList(){
        evaluateSearchObjects();
        createQuery();
        /*
        Code for grabbing resultlist of query goes here.
        return resultList;
        */
        return null;
    }

    public void evaluateSearchObjects() {
            SearchDeviceName searchObj = (SearchDeviceName) Component.getInstance("searchDeviceName", ScopeType.CONVERSATION);
            if ( searchObj != null ) {
                restrictionValues.add(searchObj.sqlString);
                restrictionJoinTables.add(searchObj.table);
            }
    }

    void createQuery(){
        StringBuilder strBuilder = new StringBuilder(sqlQuery);
        strBuilder.append(" where ");
        for ( String str : restrictionValues ){
            strBuilder.append(str);
        }
        System.out.println("done");
    }

}

2 个答案:

答案 0 :(得分:0)

我不是百分百肯定,但也许上下文事件是可行的。

看看这个link

也许你可以听一个事件

   org.jboss.seam.preSetVariable.<name> // called when the context variable <name> is s et
   org.jboss.seam.postSetVariable.<name> // called when the context variable <name> is set


   @Observer("org.jboss.seam.postSetVariable.YOUR_COMPONENT")
   public void method() {
      //Maybe you need to retrieve the correct object as parameter also,look at the documentation
   }

答案 1 :(得分:0)

您是否确认在 evaluateSearchObjects()之前调用了setParamValue()?

什么是调用IdListBuilder.getIdList()?也许这种情况发生在JSF生命周期的早期,即setParamValue()。