求教JSF裡面的變量定義

daocha 2010-06-21 02:24:44

<f:view encoding="utf-8" locale="#{staff.language}">
<h:outputText value="#{staff.errMsg}" style="color:red"/><p/>

想請教下這個staff是在哪裡定義的?
...全文
205 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
stevech 2010-06-23
  • 打赏
  • 举报
回复
在JSF2.0下用了annotation还真不好去找。
在1.2时代,我们这样写一个bean:

public class StaffBean {
// some code here
}

然后在faces-config.xml中描述它:
<managed-bean>
<managed-bean-name>staff</managed-bean-name>
<managed-bean-class>Staff</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>

我们要去找staff的时候,去faces-config.xml中去看就知道了。可是在JSF2.0中,如果我们用annotation,bean就是这样写了:

@ManagedBean(name="staff")
@SessionScoped
public class StaffBean {
// some code here
}

这种情况下要去找staff在那里定义的,只好一个一个地去查看java源文件了。不过呢,JSF2在使用annotation时有一个规则很有用:如果在用@ManagedBean没有指定name属性时,直接用类名来作为name(将类名的首字母自动小写)。如:

@ManagedBean
@SessionScoped
public class Staff {
// some code here
}

这种情况,一个名为staff的变量就自动定义在session里了。建议在JSF2中使用annotation的时候用这种方式,这时候要去找staff的话只需要去找叫Staff的java文件。
daocha 2010-06-23
  • 打赏
  • 举报
回复
自己頂下
wula0010 2010-06-22
  • 打赏
  • 举报
回复
呵呵,jsf1.2的是在faces-config.xml里找,2.0的还没研究过...........
daocha 2010-06-22
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 stevech 的回复:]

楼上的这个说法有道理。
很有可能是用annotation定义的staff。
比如:
Java code

@ManagedBean(name="staff")
@SessionScoped
public class StaffBean {
// some code...
}


这种情况,在faces-config.xml中是找不到staff的定义的,因此只能在代码中去找了。(……
[/Quote]
想請問下 是什麼意思。。。註解是什麼東西 去哪裡找??
比如說這個頁面 用到了staff這個類的

<ui:composition 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.prime.com.tr/ui"
xmlns:c="http://java.sun.com/jsp/jstl/core" template="template.xhtml">
<ui:define name="head">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</ui:define>
<ui:define name="content">
<f:view encoding="utf-8" locale="#{staff.language}">
<table width="700px" align="center">
<c:if test="${staff.language=='th_TH'}">
<tr>
<td><h:outputText value="Welcome" /><h:outputText
value=", ${staff.userName}" /></td>
</tr>
<tr></tr>
<tr>
<td><h:outputText value="#{uic.staffInfo_description} เกรด 4 และสูงกว่าทั้งหมดควรตอบคำถาม" /></td>
</tr>
<tr>
<td><h:outputText value="#{uic.staffInfo_description2} ประกอบด้วยคำตอบให้เลือก 10 คำถาม กรุณาอ่านคำถามอย่างรอบคอบและเลือกคำตอบที่ถูกต้อง" /></td>
</tr>
<tr>
<td><h:outputText value="#{uic.staffInfo_passingRate} ผ่านระดับ: 8" /></td>
</tr>
<tr>
<td height="20px"></td>
</tr>
<tr>
<td><h:outputText value="#{uic.staffInfo_fill} กรุณากรอกข้อมูลของคุณ" /></td>
</tr>
</c:if>
<c:if test="${staff.language=='zh_CN' || staff.language=='zh_HK' || staff.language=='en'}">
<tr>
<td><h:outputText value="Welcome" /><h:outputText
value=", ${staff.userName}" /></td>
</tr>
<tr></tr>
<tr>
<td><h:outputText value="#{uic.staffInfo_description}" /></td>
</tr>
<tr>
<td><h:outputText value="#{uic.staffInfo_description2} " /></td>
</tr>
<tr>
<td><h:outputText value="#{uic.staffInfo_passingRate} " /></td>
</tr>
<tr>
<td height="20px"></td>
</tr>
<tr>
<td><h:outputText value="#{uic.staffInfo_fill}" /></td>
</tr>
</c:if>
</table>
<h:form method="post" id="staffForm" prependId="false">
<table width="700px" align="center">
<tr>
<td>
<table width="80%" align="center">
<tr>
<td width="30%"><h:outputText
value="#{uic.staffInfo_region}" /></td>
<td><h:selectOneMenu id="region"
value="#{staff.staffRegion}">
<f:selectItem itemLabel="HK" itemValue="HK" />
<f:selectItem itemLabel="China" itemValue="CN" />
<f:selectItem itemLabel="Thailand" itemValue="TH" />
<f:selectItem itemLabel="Singapore" itemValue="SG" />
<f:selectItem itemLabel="Malaysia" itemValue="MY" />
<f:selectItem itemLabel="Taiwan" itemValue="TW" />
</h:selectOneMenu></td>
</tr>
<tr>
<td><h:outputText value="#{uic.staffInfo_name}" /></td>
<td><h:inputText name="staffName" id="staffName"
value="#{staff.staffName}" required="true"
requiredMessage="Full Name is required" /></td>
</tr>
<tr>
<td><h:outputText value="#{uic.staffInfo_staffID}" /></td>
<td><h:inputText name="staffID" id="staffID"
value="#{staff.staffNo}" required="true"
requiredMessage="Staff Number is required" /></td>
</tr>
<tr>
<td><h:outputText value="#{uic.staffInfo_division}" /></td>
<td><h:selectOneMenu id="division"
value="#{staff.staffDivision}">
<f:selectItem itemLabel="Management" itemValue="Management" />
<f:selectItem itemLabel="Store Planning"
itemValue="Store Planning" />
<f:selectItem itemLabel="Administration"
itemValue="Administration" />
<f:selectItem itemLabel="Customer Service/ Call Centre"
itemValue="Customer Service/ Call Centre" />
<f:selectItem itemLabel="Marketing" itemValue="Marketing" />
<f:selectItem itemLabel="Logistic and Inventory Control"
itemValue="Logistic and Inventory Control" />
<f:selectItem itemLabel="Sales and Merchandising"
itemValue="Sales and Merchandising" />
<f:selectItem itemLabel="Quality Assurance"
itemValue="Quality Assurance" />
<f:selectItem itemLabel="Human Resource"
itemValue="Human Resource" />
<f:selectItem itemLabel="Information Technology"
itemValue="Information Technology" />
<f:selectItem itemLabel="Finance & Accounting"
itemValue="Finance & Accounting" />
<f:selectItem itemLabel="Store Operations - Stores"
itemValue="Store Operations - Stores" />
<f:selectItem itemLabel="Store Operations - Office"
itemValue="Store Operations - Office" />
</h:selectOneMenu></td>
</tr>
</table>
</td>
</tr>
<tr>
<td height="20px"></td>
</tr>
<tr>
<td align="center"><h:commandButton type="submit"
value="#{uic.staffInfo_start}" action="#{staff.startQuiz}" /></td>
</tr>
</table>
</h:form>
</f:view>
</ui:define>
</ui:composition>


去哪裡順著找你所說的bean呢?
stevech 2010-06-21
  • 打赏
  • 举报
回复
楼上的这个说法有道理。
很有可能是用annotation定义的staff。
比如:

@ManagedBean(name="staff")
@SessionScoped
public class StaffBean {
// some code...
}

这种情况,在faces-config.xml中是找不到staff的定义的,因此只能在代码中去找了。(在JSF1.2中不能用此种方法)
abc130314 2010-06-21
  • 打赏
  • 举报
回复
JSF2.0 可以用注解代替配置文件定义ManagerBean
而且好像和spring整合后,#{}能调用spring定义的bean,request\session\application的bean也可以调用。只是不能自动生成新的对象
daocha 2010-06-21
  • 打赏
  • 举报
回复

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


<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF\applicationContext.xml;WEB-INF\applicationContext-security.xml</param-value>
</context-param>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<listener>
<listener-class>
org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>

<!-- place constraints on a single user's ability to log in to your application -->
<listener>
<listener-class>org.springframework.security.web.session.HttpSessionEventPublisher</listener-class>
</listener>

<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<!-- DelegatingFilterProxy delegates to a filter implementation which is defined as a Spring bean in your APP
web security are configured using http element-->
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>

<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>FORWARD</dispatcher>
<dispatcher>REQUEST</dispatcher>
<dispatcher>INCLUDE</dispatcher>
<dispatcher>ERROR</dispatcher>

</filter-mapping>

<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>*.jsf</url-pattern>
<url-pattern>/faces/*</url-pattern>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>

<servlet>
<servlet-name>Resource Servlet</servlet-name>
<servlet-class>org.primefaces.resource.ResourceServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>Resource Servlet</servlet-name>
<url-pattern>/primefaces_resource/*</url-pattern>
</servlet-mapping>

<resource-ref>
<description>
Resource reference to a factory for java.sql.Connection
instances that may be used for talking to a particular
database that is configured in the configurartion for the web application.
</description>
<res-ref-name>jdbc/QuizAdmin</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>

<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>



</web-app>


我看了半天看不明白 在另外一個項目里面也有類似的應用 #{admin. 什么什么的 跟這個#{staff.什么的類似 我都不知道這個實體是什么類型的
stevech 2010-06-21
  • 打赏
  • 举报
回复
这样啊,那再去看看web.xml里呢。应该在faces-config里啊
stevech 2010-06-21
  • 打赏
  • 举报
回复
感觉更像是Message Bundle,定义在一个properties文件里面,当然也可以是一个Managed Bean。不管是哪种,都去faces-config里面去看。如果staff是在<managed-bean>里面定义的是Bean,如果在<resource-bundle>里面则是properties。
daocha 2010-06-21
  • 打赏
  • 举报
回复

<?xml version="1.0" encoding="utf-8"?>
<faces-config xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"
version="2.0">

<application>
<el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
<locale-config>
<default-locale>en</default-locale>
<supported-locale>th</supported-locale>
<supported-locale>zh_CN</supported-locale>
<supported-locale>zh_HK</supported-locale>
</locale-config>
<resource-bundle>
<base-name>com.toysrus.quiz.ui.UIControl</base-name>
<var>uic</var>
</resource-bundle>
</application>

<!--
========================= navigation-rule
=============================
-->
<navigation-rule>
<from-view-id>/staffInfo.xhtml</from-view-id>
<navigation-case>
<from-outcome>success</from-outcome>
<to-view-id>/showQuestion.xhtml</to-view-id>
</navigation-case>
<navigation-case>
<from-outcome>failure</from-outcome>
<to-view-id>/staffInfo.xhtml</to-view-id>
</navigation-case>
</navigation-rule>

<navigation-rule>
<from-view-id>/showQuestion.xhtml</from-view-id>
<navigation-case>
<from-outcome>success</from-outcome>
<to-view-id>/checkAnswer.xhtml</to-view-id>
</navigation-case>
<navigation-case>
<from-outcome>failure</from-outcome>
<to-view-id>/login.xhtml</to-view-id>
</navigation-case>
</navigation-rule>

</faces-config>

也沒看出在哪里定義了這個staff 實體類 這個uic倒是看到了 有的是這么用的 #{uic.admin_region}
可是staff實在是沒看出來怎么弄
chl343000406 2010-06-21
  • 打赏
  • 举报
回复
这个应该是bean类里面定义了一个方法,或者是staff是一个实体类,errMsg值是错误信息!
具体你还要看看faces-config.xml里面这个页面Managed Beans里面关联哪个bean类了!

81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧