代码之家  ›  专栏  ›  技术社区  ›  Arthur Ronald

如何使用Seam框架将表单字段作为操作参数传递?

  •  2
  • Arthur Ronald  · 技术社区  · 15 年前

    我不知道是否有可能,但我想要类似的东西

    <f:view>
    <h:form>      
        <div>
            <label for="accountId">Type your account id</label>
            <input type="text" name="accountId"/>
        </div>
        <div>
            <label for="amount">Type amount</label>
            <input type="text" name="amount"/>
        </div>
        <div>
            <!--NOTICE IT IS #{accountService.deposit} -->
            <!--BUT I WANT TO USE #{accountService.deposit(accountId, amount)} -->
            <h:commandButton action="#{accountService.deposit}" value="Deposit amount"/>
        </div>
    </h:form>
    </f:view>
    

    我的服务

    @Stateless
    @Name("accountService")
    public class AccountServiceImpl implements AccountService {
    
        @RequestParemeter
        protected Integer accountId;
    
        @RequestParemeter
        protected double amount;
    
        @PersistenceContext
        private EntityManager manager;
    
        public void deposit() {
            Account account = manager.find(Account.class, accountId);
    
            account.deposit(amount);
        }
    
    }
    

    碰巧我想用这个代替上面的显示

    @Stateless
    @Name("accountService")
    public class AccountServiceImpl implements AccountService {
    
        @PersistenceContext
        private EntityManager manager;
    
        public void deposit(Integer accountId, double amount) {
            Account account = manager.find(Account.class, accountId);
    
            account.deposit(amount);
        }
    
    }
    

    有可能吗?如果是这样的话,我应该用什么——事件或者其他什么——来实现我的目标?

    当做,

    1 回复  |  直到 14 年前
        1
  •  2
  •   Arthur Ronald    15 年前

    这是可能的,

    为了达到我的目标,我需要使用以下内容

    <f:view>
    <h:form>      
        <div>
            <label for="accountId">Type your account id</label>
            <input type="text" name="accountId"/>
        </div>
        <div>
            <label for="amount">Type amount</label>
            <input type="text" name="amount"/>
        </div>
        <div>
            <h:commandButton action="#{accountService.deposit(param.accountId, param.amount)}" value="Deposit amount"/>
        </div>
    </h:form>
    </f:view>
    

    param.accountID和param.amount 在调用action方法时计算 . 尽管Seam允许在el表达式中使用内置的事件上下文组件,如下所示

    <h:commandButton action="#{accountService.deposit(eventContext.accountId, eventContext.amount)}" value="Deposit amount"/>
    

    它不能按预期工作。可能是因为它只在使用组件时起作用,而不是在使用请求参数时起作用。如果要传递请求参数,请使用 帕拉姆

    当做,