无法从xhtml文件,Jsf managedBean访问方法

时间:2017-05-11 07:36:10

标签: java spring primefaces jsf-2

我遇到了一个我无法找到的问题,我使用了spring web和jsf,我创建了一个jsf托管bean并从xhtml登录页面调用但是我无法访问托管bean的方法,通常是#{managedBean.METHODNAME} autocompolete works ,但是我的xhtml文件无效!

我是java新手,对不起我现在已经犯下了愚蠢的错误

我的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">
  <display-name>sinav</display-name>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <listener>
    <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
  </listener>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
       WEB-INF/applicationContext.xml

    </param-value>
  </context-param>

  <context-param>

    <param-name>primefaces.THEME</param-name>

    <param-value>omega</param-value>
</context-param>


  <context-param>
    <param-name>javax.faces.PROJECT_STAGE</param-name>
    <param-value>Development</param-value>
  </context-param>
  <welcome-file-list>
    <welcome-file>faces/login.xhtml</welcome-file>
  </welcome-file-list>
  <servlet>
    <servlet-name>jersey-serlvet</servlet-name>
    <servlet-class>
                     com.sun.jersey.spi.container.servlet.ServletContainer
                </servlet-class>
    <init-param>
      <param-name>com.sun.jersey.config.property.packages</param-name>
      <param-value>kpss.rest</param-value>
    </init-param>
    <init-param>
      <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
      <param-value>true</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>jersey-serlvet</servlet-name>
    <url-pattern>/rest/*</url-pattern>
  </servlet-mapping>
  <context-param>
    <description>State saving method: 'client' or 'server' (=default). See JSF Specification 2.5.2</description>
    <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
    <param-value>client</param-value>
  </context-param>
  <context-param>
    <param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
    <param-value>resources.application</param-value>
  </context-param>
  <listener>
    <listener-class>com.sun.faces.config.ConfigureListener</listener-class>
  </listener>
  <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>/faces/*</url-pattern>
  </servlet-mapping>
</web-app>

xhtml登录文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:p="http://primefaces.org/ui">
<h:head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

<script type="text/javascript">
    function handleLoginRequest(xhr, status, args) {
        if(args.validationFailed || !args.loggedIn) {
            PF('dlg').jq.effect("shake", {times:5}, 100);
        }
        else {
            PF('dlg').hide();
            $('login.xhtml').fadeOut();
        }
    }
</script>


<title>Login</title>
</h:head>
<h:body>

<h:form>            
    <h:outputLink value="javascript:void(0)" onclick="PF('dlg').show();" title="login">
        <p:graphicImage name="/images/login.png" />
    </h:outputLink>

    <p:growl id="growl" sticky="true" showDetail="true" life="3000" />

    <p:dialog header="Giriş" widgetVar="dlg" resizable="false">
        <h:panelGrid columns="2" cellpadding="5">
            <h:outputLabel for="username" value="Kullanıcı Adı:" />
            <p:inputText id="username" value="#{kullaniciservice.kullanici.kullaniciad}" required="true" label="username" />

            <h:outputLabel for="password" value="Şifre:" />
            <p:password id="password" value="#{kullaniciservice.kullanici.sifre}" required="true" label="password" />

            <f:facet name="footer">
                <p:commandButton value="Giriş" update="growl" actionListener="#{kullaniciservice.GetKullaniciInfo()}"
                                 oncomplete="handleLoginRequest(xhr, status, args)" />
            </f:facet>  
        </h:panelGrid>
    </p:dialog>
</h:form>



</h:body>
</html>

和我的豆子

 import java.io.Serializable;
    import java.util.List;

    import javax.annotation.Resource;
    import javax.faces.bean.ManagedBean;
    import javax.faces.bean.SessionScoped;

    import org.hibernate.Query;
    import org.hibernate.SessionFactory;

    import org.springframework.transaction.annotation.Transactional;
    import org.springframework.web.context.support.SpringBeanAutowiringSupport;
    import kpss.entity.Kullanicilar;

    @ManagedBean (name="kullaniciservice")
    @SessionScoped
    public class KullaniciService extends SpringBeanAutowiringSupport implements Serializable {




        /**
         * 
         */
        private static final long serialVersionUID = 1L;


        @Resource(name = "sessionFactory")
        private SessionFactory sessionFactory;


        //
        private Kullanicilar kullanici;



        public KullaniciService() {
            kullanici = new Kullanicilar();
        }

        public Kullanicilar getKullanici() {
            return kullanici;
        }



        public void setKullanici(Kullanicilar kullanici) {
            this.kullanici = kullanici;
        }



        @Transactional ("maintrans")
        private String GetKullaniciInfo ()
        {
             Query query = sessionFactory.getCurrentSession().createQuery(
                        "SELECT d FROM Kullanicilar d WHERE d.kullaniciad=:k and d.sifre=:s");
                query.setParameter("k", kullanici.getKullaniciad());
                query.setParameter("s", kullanici.getSifre());
                List results = query.list();
                if (results.size()>0)
                    return "index";
                else 
                    return "login";

        }

    }

感谢您的帮助...

1 个答案:

答案 0 :(得分:0)

您需要在托管bean中注入bean Kullanicilar,以便访问kullaniciadsifre

注射的例子:

@ManagedProperty("#{kullanicilar }")
    private Kullanicilar kullanici; // +setter 

您还需要通过添加此anotation将Kullanicilar设置为托管bean:

@ManagedBean(name="kullanicilar ")

take a look here 有关从另一个

访问一个托管bean的更多信息