在jsf中获取空表单会话bean

时间:2014-08-24 16:30:08

标签: session jsf-2 primefaces

我在登录后设置了会话bean,并在会话bean中打印它在xhtml文件中打印的值但是当我转到下一页或bean会话bean返回空时它会自动重置该值。

我正在使用primeface 4.0,jsf 2.0和ejb 3.0进行Web应用程序。

Web.xml中

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    version="3.0">

    <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.xhtml</url-pattern>
    </servlet-mapping>
    <welcome-file-list>
        <welcome-file>index.xhtml</welcome-file>
    </welcome-file-list>
    <context-param>
        <param-name>javax.faces.PROJECT_STAGE</param-name>
        <param-value>Development</param-value>
    </context-param>
    <!-- <context-param> <param-name>javax.faces.STATE_SAVING_METHOD</param-name> 
        <param-value>server</param-value> </context-param> -->
    <session-config>
        <session-timeout>30</session-timeout>
    </session-config>

    <context-param>
        <param-name>primefaces.THEME</param-name>
        <param-value>ui-lightness</param-value>
    </context-param>


    <context-param>
        <param-name>org.apache.myfaces.CHECK_ID_PRODUCTION_MODE</param-name>
        <param-value>false</param-value>
    </context-param>
    <context-param>
        <param-name>org.apache.myfaces.VIEW_UNIQUE_IDS_CACHE_ENABLED</param-name>
        <param-value>true</param-value>
    </context-param>
    <mime-mapping>
        <extension>xlsx</extension>
        <mime-type>text/xlsx</mime-type>
    </mime-mapping>
</web-app>

//Setting user from loginMB having viewscope bean.

UserMB

@SessionScoped
@ManagedBean(name = "userMB")
public class UserMB extends AbstractMB implements Serializable {
    public static final String INJECTION_NAME = "#{userMB}";
    private static final long serialVersionUID = 1L;

    private User user;
    @EJB
    private UserEJB userEJB;

    public String logOut() {
        getRequest().getSession().invalidate();
        return "/index.xhtml?faces-redirect=true";
    }

    private HttpServletRequest getRequest() {
        return (HttpServletRequest) FacesContext.getCurrentInstance()
                .getExternalContext().getRequest();
    }

    public User getUser() {
        if (user == null) {
            user = new User();
        }
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }


    public List<User> getUserList() {
        if (userList == null) {
            userList = userEJB.findAll();
        }
        return userList;
    }

    public void setUserList(List<User> userList) {
        this.userList = userList;
    }

}

CampMB

@ManagedBean
@ViewScoped
public class CampMB extends AbstractMB implements Serializable {

    private static final long serialVersionUID = 1L;

    @ManagedProperty("#{userMB}")
    private UserMB userMB;

    public UserMB getUserMB() {
        return userMB;
    }

    public void setUserMB(UserMB userMB) {
        this.userMB = userMB;
    }
    private User user;

    private Camp camp;
    private List<CampDTO> listCamp;

    @EJB
    private CampEJB campEJB;
    @EJB
    private MemberEJB memberEJB;
    @EJB
    private CodeValueEJB codeEJB;

    TimeZone timeZone = TimeZone.getDefault();

    public TimeZone getTimeZone() {
        return timeZone;
    }

    public void setTimeZone(TimeZone timeZone) {
        this.timeZone = timeZone;
    }


    public List<CampDTO> getListCamp() {

        Map<String, Object> sessionMap =         FacesContext.getCurrentInstance().getExternalContext().getSessionMap();
        UserMB managedBean = (UserMB) sessionMap.get("userMB");

        System.out.println("test use==" + managedBean.getUser().getId());
        System.err.println("Session"+getUserMB().getUser().getId());
        if (listCamp == null) {
            listCamp = new ArrayList<CampDTO>();
        }
        listCamp = ConvertUtils.convertCampToDTO(campEJB.findAll());
        return listCamp;
    }

    public void setListCamp(List<CampDTO> listCamp) {
        this.listCamp = listCamp;
    }

    public User getUser() {
        if (user == null) {
            user = new User();
        }
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }
}

我正在尝试从搜索获得不同的技术,但没有人适合我? 是否有任何配置或如何正确设置/获取会话bean。

0 个答案:

没有答案