大鱼

JSF中commandLink与dataTable搭配不能正常工作的解决办法

1.     环境

Myfaces1.4 + tomahawk-1.1.3 + tomcat5.x + JDK5

下载:http://myfaces.apache.org

2.     问题描述

假设场景,首先显示用户列表(用dataTable来实现列表显示),然后可以点击用户的名字导航到用户详细信息页面。

3.     尝试实现以及出现的问题

UserInfo.java

package com.wide.jsf.model;

 

/**

 * 用户信息

 *

 * @author cydooo

 *

 */

public class UserInfo {

    private Long userId;

    private String username;

    private String userdesc;

    public UserInfo() {

    }

    public UserInfo(Long userId, String username) {

        this.userId = userId;

        this.username = username;

    }

    public UserInfo(Long userId, String username, String userdesc) {

        this(userId, username);

        this.userdesc = userdesc;

    }

    //省略getter和setter

}
 


UserInfoAction.java

package com.wide.jsf.web;

 

import java.util.Arrays;

import java.util.List;

 

import javax.faces.context.FacesContext;

 

import com.wide.jsf.model.UserInfo;

 

public class UserInfoAction {

 

    /*

     * 用于显示详细信息

     */

    private UserInfo userInfo;

 

    /*

     * 用于显示用户列表

     */

    private List<UserInfo> userInfos;

 

    public UserInfo getUserInfo() {

       return userInfo;

    }

 

    public void setUserInfo(UserInfo userInfo) {

       this.userInfo = userInfo;

    }

 

    public List<UserInfo> getUserInfos() {

       return userInfos;

    }

 

    public void setUserInfos(List<UserInfo> userInfos) {

       this.userInfos = userInfos;

    }

 

    public String listUser() {

       userInfos = Arrays.asList(new UserInfo[] { new UserInfo(1L, "Tom"),new UserInfo(2L, "John"), new UserInfo(3L, "Lily") });

         return "listUser";

    }

 

    public String showDetail() {

       FacesContext context = FacesContext.getCurrentInstance();

       Long userId = Long.parseLong((String) context.getExternalContext()

              .getRequestParameterMap().get("userId"));

       System.out.println("==>" + userId);

       switch (userId.intValue()) {

       case 1:

           userInfo = new UserInfo(1L, "Tom", "I am Tom");

           break;

       case 2:

           userInfo = new UserInfo(2L, "John", "I am John");

           break;

       case 3:

           userInfo = new UserInfo(3L, "Lily", "I am Lily");

       }

       return "userdetail";

    }

}
 


Backingbean和navigation的配置

<managed-bean>

       <managed-bean-name>userInfoAction</managed-bean-name>

       <managed-bean-class>

           com.wide.jsf.web.UserInfoAction

       </managed-bean-class>

       <managed-bean-scope>request</managed-bean-scope>

    </managed-bean>

    <navigation-rule>

       <from-view-id>/listUser.jsp</from-view-id>

       <navigation-case>

           <from-outcome>userdetail</from-outcome>

           <to-view-id>/userdetail.jsp</to-view-id>

       </navigation-case>

    </navigation-rule>

    <navigation-rule>

       <from-view-id>/userdetail.jsp</from-view-id>

    </navigation-rule>
 


Jsp页面内容

A. listUser.jsp

<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>

<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>

<%@ page contentType="text/html; UTF-8" pageEncoding="UTF-8"%>

 

<f:view>

<html>

  <head>

    <title>userlist</title>

  </head>

 

  <body>

    <h:form>

       <h:dataTable var="user" value="#{userInfoAction.userInfos}">

           <h:column>

              <h:commandLink value="#{user.username}" action="#{userInfoAction.showDetail}">

                  <f:param value="#{user.userId}" name="userId"/>

              </h:commandLink>

           </h:column>

       </h:dataTable>

    </h:form>

  </body>

</html>

</f:view>
 


B. userdetail.jsp

<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>

<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>

<%@ page contentType="text/html; UTF-8" pageEncoding="UTF-8"%>

 

<f:view>

<html>

  <head>

    <title>userDetail</title>

  </head>

 

  <body>

    <h:form>

    <h:outputText value="#{userInfoAction.userInfo.userdesc}"/>

    </h:form>

  </body>

</html>

</f:view>
 


编码结束,启动tomcat测试,进入userlist页面之后,我们可以看到用户列表,但点击用户查看详细信息的时候,发现并没有调用showDetail方法。

4.     解决办法

A.                        将<managed-bean-scope>request</managed-bean-scope>改为<managed-bean-scope>session</managed-bean-scope>。这是最简单的做法,改了之后重起Tomcat之后马上就看到效果了。但是很明显的使用session scope不好,比如这个例子,用户列表在当前seesion有效的情况下将一直保留,多窗口浏览器中也容易发生意想不到的问题。没有必要不要用session。

B.                        将用户列表绑定到dataTable控件。

首先修改UserInfo类,让他实现Serializable接口,其他代码完全一样,实现Serializable接口是必须的,否则会抛出java.io.NotSerializableException异常。

public class UserInfo implements Serializable{…}
 


UserInfoAction.java的修改

package com.wide.jsf.web;

 

import java.util.Arrays;

import java.util.List;

 

import javax.faces.component.html.HtmlDataTable;

import javax.faces.context.FacesContext;

 

import com.wide.jsf.model.UserInfo;

 

public class UserInfoAction {

 

    /*

     * 用于显示详细信息

     */

    private UserInfo userInfo;

 

    /*

     * 绑定用户列表

     */

    private HtmlDataTable dataTable = new HtmlDataTable();

   

    public UserInfo getUserInfo() {

       return userInfo;

    }

 

    public void setUserInfo(UserInfo userInfo) {

       this.userInfo = userInfo;

    }

 

    public String listUser() {

       List<UserInfo> userInfos = Arrays.asList(new UserInfo[] { new UserInfo(1L, "Tom"),

              new UserInfo(2L, "John"), new UserInfo(3L, "Lily") });

       dataTable.setValue(userInfos);

       return "listUser";

    }

 

    public String showDetail() {

       FacesContext context = FacesContext.getCurrentInstance();

       Long userId = Long.parseLong((String) context.getExternalContext()

              .getRequestParameterMap().get("userId"));

       System.out.println("==>" + userId);

       switch (userId.intValue()) {

       case 1:

           userInfo = new UserInfo(1L, "Tom", "I am Tom");

           break;

       case 2:

           userInfo = new UserInfo(2L, "John", "I am John");

           break;

       case 3:

           userInfo = new UserInfo(3L, "Lily", "I am Lily");

       }

       return "userdetail";

    }

 

    public HtmlDataTable getDataTable() {

       return dataTable;

    }

 

    public void setDataTable(HtmlDataTable dataTable) {

       this.dataTable = dataTable;

    }

}
 


listUser.jsp的修改

<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>

<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>

<%@ page contentType="text/html; UTF-8" pageEncoding="UTF-8"%>

 

<f:view>

<html>

  <head>

    <title>userlist</title>

  </head>

 

  <body>

    <h:form>

       <h:dataTable var="user" binding="#{userInfoAction.dataTable}">

           <h:column>

              <h:commandLink value="#{user.username}" action="#{userInfoAction.showDetail}">

                  <f:param value="#{user.userId}" name="userId"/>

              </h:commandLink>

           </h:column>

       </h:dataTable>

    </h:form>

  </body>

</html>

</f:view>
 


重起Tomcat一切OK,不需要非得在session scope下了。

 

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/pengrg166/archive/2007/01/22/1490174.aspx

posted on 2009-10-15 14:28 大鱼 阅读(593) 评论(0)  编辑  收藏 所属分类: JSF


只有注册用户登录后才能发表评论。


网站导航: