jsf 应用程序开发入门 hello示例

本文介绍了一个简单的JSF应用程序示例,包括使用hello.jsp页面收集用户输入,并根据输入内容导航到不同的页面显示定制问候语。文章详细展示了从配置文件、视图层到控制器层的实现过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

jsf 应用程序开发入门
hello

1. jsf-api.jar,jsf-impl.jar ,jstl-1.2.jar(https://maven-repository.dev.java.net/repository/jstl/jars/)

2. 

hello.jsp
<%@page contentType="text/html; charset=UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<html>
<head>
<title>Hello Application</title>
</head>
<body>
<f:view>
  <h:form id="helloForm">
    <h3>Please enter your name </h3>
    <table>
      <tr>
        <td>Name:</td>
        <td>
          <h:inputText id="userName"
                       value="#{hello.userName}" required="true"/>
          <h:message for="userName" />
        </td>
      </tr>     
    </table>
    <p>
    <h:commandButton id="helloCommand"
                     type="submit"
                     value="Submit"
                     action="#{hello.sayHelloAction}"/>
  </h:form>
  <h:messages globalOnly="true">
  </h:messages>
</f:view>
</body>
</html>
3.package com.jsfabc.jsh.view.bean;

import javax.el.ValueExpression;
import javax.faces.component.html.HtmlOutputText;
import javax.faces.context.FacesContext;

public class HelloBean {
    //用于登录的用户名 
    private String userName;
    //用于显示New Friend的文本输出组件
    private HtmlOutputText htmlOutputText;

    //构造函数
    public HelloBean() {
    }

    // 用户名的取得与设置
    public String getUserName() {
      return userName;
    }

    public void setUserName(String newValue) {
         this.userName = newValue;
    }
   
    //输出组件的取得与设置
    public HtmlOutputText getHtmlOutputText(){
     return htmlOutputText;
    }
   
    public void setHtmlOutputText(HtmlOutputText newValue){
     this.htmlOutputText=newValue;
    }   

    public String sayHelloAction(){
     if(userName.equalsIgnoreCase("stranger")){
        //获得应用程序实例,以便创建HtmlOutputText组件
        FacesContext context = FacesContext.getCurrentInstance();
        //声明一个输出文本组件
          HtmlOutputText output = new HtmlOutputText();
          //获得一个值表达式
           ValueExpression value = context.getApplication().getExpressionFactory()
                 .createValueExpression(context.getELContext(),
                 "#{hello.htmlOutputText}", String.class);
           output.setValueExpression("value", value);
 
          //为HtmlOutputText组件设置值
          output.setValue("New Friend!");
          //为HtmlOutputText组件设置CSS风格
          output.setStyle("color:red");
          //将创建的HtmlOutputText组件实例赋给Bean的属性
          this.htmlOutputText=output;
          //返回陌生人访问时的导航逻辑字符串
          return "newFriend";
     }
     else{
        //返回熟人访问时的导航逻辑字符串 
        return "oldFriend";
     }
    }
}

4.如果是stranger 就指向howDoYouDo.jsp
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<html>
    <f:view>
      <head>
          <title>A Simple JavaServer Faces Application</title>
       </head>
       <body>
          <h3>
             How do you do,
             <h:outputText binding="#{hello.htmlOutputText}"/>
          </h3>
       </body>
    </f:view>
</html>
5其它的就是old friend指向howAreYou.jsp
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<html>
    <f:view>
      <head>
          <title>A Simple JavaServer Faces Application</title>
       </head>
       <body>
          <h3>
             How are you,
             <h:outputText value="#{hello.userName}"/>!
          </h3>
       </body>
    </f:view>
</html>
6.faces-config.xml
<?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_1_2.xsd"
    version="1.2">

  <navigation-rule>
    <from-view-id>/hello.jsp</from-view-id>
    <navigation-case>
      <from-outcome>newFriend</from-outcome>
      <to-view-id>/howDoYouDo.jsp</to-view-id>
    </navigation-case>
    <navigation-case>
      <from-outcome>oldFriend</from-outcome>
      <to-view-id>/howAreYou.jsp</to-view-id>
    </navigation-case>
  </navigation-rule>

  <managed-bean>
    <managed-bean-name>hello</managed-bean-name>
    <managed-bean-class>com.jsfabc.jsh.view.bean.HelloBean</managed-bean-class>
    <managed-bean-scope>request</managed-bean-scope>
  </managed-bean>
   
</faces-config>
7.web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" 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>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>
    <servlet-name>Faces Servlet_tmp</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、付费专栏及课程。

余额充值