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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了无法从xhtml文件,Jsf managedBean访问方法相关的知识,希望对你有一定的参考价值。

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

我是java新手,对不起我现在的愚蠢错误

我的Webb fille

<?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";

        }

    }

谢谢你的帮助......

答案

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

注射的例子:

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

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

@ManagedBean(name="kullanicilar ")

有关从另一个托管bean访问一个托管bean的更多信息,请访问take a look here

以上是关于无法从xhtml文件,Jsf managedBean访问方法的主要内容,如果未能解决你的问题,请参考以下文章

JSF 过滤器跳过一些文件

Spring boot JSF可执行JAR找不到xhtml文件

JSF 无法使用 from-view-id '/index.xhtml' 找到匹配的导航案例以进行操作

JSF 和 PrettyFaces - 如何限制直接 xhtml 请求

将 JSF .xhtml 文件映射为无扩展名

如何避免用户访问 JSF 中的 .xhtml 页面?