h: jsf2中的commandLink没有调用bean方法,甚至没有通过javascript调用

h:commandLink in jsf2 not invoking bean method and not even through javascript call

本文关键字:调用 javascript 方法 jsf2 commandLink bean 中的      更新时间:2023-09-26

这是在tomcat7上将我的项目迁移到jsf2之后发生的。早些时候,jsf1的tomcat5.5运行良好。我有一个.xhtml文件,试图通过h:commandLink调用托管bean方法,但没有调用它。我已经尝试在其他与同一主题相关的stackoverflow论坛中添加EL 2.2 jar,还添加了web.xml中的条目:

<context-param>
    <param-name>org.apache.myfaces.EXPRESSION_FACTORY</param-name>
    <param-value>com.sun.el.ExpressionFactoryImpl</param-value>
  </context-param>
  <context-param>
    <param-name>com.sun.faces.expressionFactory</param-name>
    <param-value>com.sun.el.ExpressionFactoryImpl</param-value>
  </context-param>

但这个问题并没有得到解决。请帮忙。

.xhtml文件:

<html xmlns="http://www.w3.org/1999/xhtml" xmlns:c="http://java.sun.com/jsp/jstl/core" 
      xmlns:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html" 
      xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:t="http://myfaces.apache.org/tomahawk">
    <f:view>
    <f:loadBundle var="text" basename="#{basePage.bundleName}"/>
    <title>#{text['user.passwordHint']}</title>
    <p>Looking up password hint for ${param.username}...</p>
    <h:form id="passwordForm">
        <h:inputHidden id="username" value="#{passwordHint.username}"/>
        <h:commandLink action="#{passworHint.execute}" id="execute">
           <f:param name="username" value="${param.username}"></f:param>
        </h:commandLink>
    </h:form>
    <script type="text/javascript">
        var f = document.forms['passwordForm'];
         f.elements['passwordForm:_link_hidden_'].value='passwordForm:execute';
    f.elements['username'].value='${param.username}';        
f.submit();
    </script>
    </f:view>
    </html>

托管bean:

public class PasswordHint extends BasePage {
    @ManagedProperty(value="#{param.username}")
    private String username;
 /*   private String execute;
    public String getExecute() {
        return execute;
    }
    public void setExecute(String execute) {
        this.execute = execute;
    }*/
    public String getUsername() {
        System.out.println("get username of passwordhint-------"+username);
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }

    public String execute() {
    /*  FacesContext context = FacesContext.getCurrentInstance();
        Map<String,String> params = context.getExternalContext().getRequestParameterMap();
        System.out.println(params.get("username"));
        System.out.println("Inside password hint execute-------------");
        */
        // ensure that the username has been sent
        if (username == null || "".equals(username)) {
            log.warn("Username not specified, notifying user that it's a required field.");
            addError("errors.required", getText("user.username"));
            return null;
        }
        if (log.isDebugEnabled()) {
            log.debug("Processing Password Hint...");
        }
        // look up the user's information
        try {
            User user = userManager.getUserByUsername(username);
            System.out.println("username retrieved---------"+username);
            StringBuffer msg = new StringBuffer();
            msg.append("Your password hint is: " + user.getPasswordHint());
            msg.append("'n'nLogin at: " + RequestUtil.getAppURL(getRequest()));
            message.setTo(user.getEmail());
            String subject = '[' + getText("webapp.name") + "] " + getText("user.passwordHint");
            message.setSubject(subject);
            message.setText(msg.toString());
            mailEngine.send(message);
            addMessage("login.passwordHint.sent", 
                       new Object[] { username, user.getEmail() });
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("In exception----------------");
            // If exception is expected do not rethrow
            //addError("login.passwordHint.error", username);
            addMessage("login.passwordHint.sent", username);
        }
        return "success";
    }
faces-config.xml:

 <navigation-rule>
            <from-view-id>/passwordHint.xhtml</from-view-id>
            <navigation-case>
                <from-outcome>success</from-outcome>
                <to-view-id>/login.jsp</to-view-id>
                <redirect/>
            </navigation-case>
        </navigation-rule>
      <managed-bean>
            <managed-bean-name>passwordHint</managed-bean-name>
            <managed-bean-class>com.webapp.action.PasswordHint</managed-bean-class>
            <managed-bean-scope>request</managed-bean-scope>
            <managed-property>
              <property-name>username</property-name>
              <value>#{param.username}</value>
            </managed-property>
            <managed-property>
                <property-name>userManager</property-name>
                <value>#{userManager}</value>
            </managed-property>
            <managed-property>
                <property-name>mailEngine</property-name>
                <value>#{mailEngine}</value>
            </managed-property>
            <managed-property>
                <property-name>message</property-name>
                <value>#{mailMessage}</value>
            </managed-property>
            <managed-property>
                <property-name>templateName</property-name>
                <value>accountCreated.vm</value>
            </managed-property> 
        </managed-bean>

是的,这是我正在尝试的实际代码,在复制粘贴实际代码时出现了拼写错误。此外,javascript错误显示:f.elements['passwordForm:_link_hidden_'] is null or not an object.