JSF入个门及强项

我参照的书是 javaserver faces in action 中文

本书入个门还行吧.

开发环境是myeclipse 5.5

全部代码如下.

bean:

public class HelloBean {
 private int numControls;
 private HtmlPanelGrid controlPanel;

 public int getNumControls()
 {
    return numControls;
 }
 public void setNumControls(int numControls)
 {
    this.numControls = numControls;
 }
 public HtmlPanelGrid getControlPanel()
 {
    return controlPanel;
 }
 public void setControlPanel(HtmlPanelGrid controlPanel)
 {
    this.controlPanel = controlPanel;
 }
 public void addControls(ActionEvent actionEvent)
 {
    Application application = FacesContext.getCurrentInstance().getApplication();
    List children = controlPanel.getChildren();
    children.clear();
    for (int count = 0; count < numControls; count++)
    {
     HtmlOutputText output = (HtmlOutputText)application.createComponent(HtmlOutputText.COMPONENT_TYPE);
     output.setValue(" " + count + " ");
     output.setStyle("color: blue");
     children.add(output);
    }
 }
 public String goodbye()
 {
    return "success";
 }

}

页面 hello.jsp


<jsp:directive.page import="java.util.Random"/><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<html>
   <head>
    <title>
    JSF in Action - Hello, world!
     <% Random r = new Random();
       int a = r.nextInt(20);
        out.println(a);
       %>
    </title>
   </head>
   <f:view>
   <body>
    <h:form id="welcomeForm">
     <h:outputText id="welcomeOutput"
      value="Welcome to JavaServer Faces!"
      style="font-family: Arial, sans-serif; font-size: 24;
      color: green;"/>
     <p>
     <h:message id="errors" for="helloInput" style="color: red"/>
     </p>
     <p>
     <h:outputLabel for="helloInput">
      <h:outputText id="helloInputLabel" value="Enter number of controls to display:"/>
     </h:outputLabel>
     <h:inputText id="helloInput" value="#{helloBean.numControls}" required="true">
      <f:validateLongRange minimum="1" maximum="500"/>
     </h:inputText>
     </p>
     <p>
     <h:panelGrid id="controlPanel"
      binding="#{helloBean.controlPanel}"
      columns="20" border="1" cellspacing="0"/>
     </p>
     <h:commandButton id="redisplayCommand" type="submit" value="Redisplay" actionListener="#{helloBean.addControls}"/>
     <h:commandButton id="goodbyeCommand" type="submit" value="Goodbye" action="#{helloBean.goodbye}" immediate="true"/>
    </h:form>
    <blockquote>
      <f:verbatim>
        <c:import url="WEB-INF/web.xml"/>
      </f:verbatim>
    </blockquote>
   </body>
   </f:view>
</html>

 

goodbye.jsp

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<f:view>
<html>
   <head>
    <title>
    JSF in Action - Hello, world!
    </title>
   </head>
   <body>
    <h:form id="goodbyeForm">
    <p>
    <h:outputText id="welcomeOutput" value="Goodbye!"
     style="font-family: Arial, sans-serif; font-size: 24;
     font-style: bold; color: green;"/>
    </p>
    <p>
    <h:outputText id="helloBeanOutputLabel" value="Number of controls displayed:"/>
    <h:outputText id="helloBeanOutput" value="#{helloBean.numControls}"/>
    </p>
    </h:form>
   </body>
</html>
</f:view>

 

faces-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE faces-config PUBLIC "-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.1//EN" "http://java.sun.com/dtd/web-facesconfig_1_1.dtd">
<faces-config>
<managed-bean>
   <description>The one and only HelloBean.</description>
   <managed-bean-name>helloBean</managed-bean-name>
   <managed-bean-class>org.jia.hello.HelloBean</managed-bean-class>
   <managed-bean-scope>session</managed-bean-scope>
</managed-bean>
<navigation-rule>
   <description>Navigation from the hello page.</description>
   <from-view-id>/hello.jsp</from-view-id>
   <navigation-case>
    <from-outcome>success</from-outcome>
    <to-view-id>/goodbye.jsp</to-view-id>
   </navigation-case>
</navigation-rule>
</faces-config>

web.xm

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee   http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
  <!--
  <context-param>
    <param-name>javax.faces.CONFIG_FILES</param-name>
    <param-value>/WEB-INF/faces-config.xml</param-value>
  </context-param>
   -->
  <servlet>
    <servlet-name>Faces Servlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    <load-on-startup>0</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>/faces/*</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

调试通过.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值