【问题标题】:h:commandLink in jsf2 not invoking bean method and not even through javascript calljsf2中的h:commandLink不调用bean方法,甚至不通过javascript调用
【发布时间】:2014-09-15 17:13:24
【问题描述】:

这是在将我的项目迁移到 tomcat7 上的 jsf2 之后发生的。早些时候在 jsf 1 的 tomcat5.5 上运行良好。我有一个 .xhtml 文件,我试图通过 h:commandLink 调用托管 bean 方法,但它没有被调用。我已经尝试添加 EL 2.2 jar,如其他有关同一主题的 stackoverflow 论坛中所建议的那样,并且还在 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>

【问题讨论】:

  • 您的页面上没有h:headh:body。 passwordHint 在您的 commandLink 操作中拼写不正确。这是您正在尝试的实际代码吗?

标签: javascript jsf-2


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2023-03-25
    • 2019-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-13
    • 1970-01-01
    • 2011-05-07
    相关资源
    最近更新 更多