利用MyEclipse实现一简单的JSF

本文详细介绍了如何使用MyEclipse创建一个简单的JSF应用,包括新建Web项目、导入必需的JSF库、配置faces-config.xml和web.xml文件、编写Model和Control层代码,以及创建表示层的index.jsp页面。

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

1打开MyEclipse,新建一个web project,命名为Hello
2将所需要的包导入到系统,所需要的包主要有如下:
  commons-beanutils.jar 
  commons-collections.jar      
  commons-digester.jar  
  commons-logging.jar 
  jsf-api.jar   
  jsf-impl.jar
3 选择工程名,右键MyEclipse->Add JSF Capabilities,系统自动生成faces-config.xml
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>
  <managed-bean-name>SayHello</managed-bean-name>
  <managed-bean-class>org.bromon.jsf.model.hello.SayHello</managed-bean-class>
  <managed-bean-scope>request</managed-bean-scope>
 </managed-bean>
 
 <managed-bean>
  <managed-bean-name>HelloDelegater</managed-bean-name>
  <managed-bean-class>org.bromon.jsf.control.hello.HelloDelegater</managed-bean-class>
  <managed-bean-scope>request</managed-bean-scope>
  <managed-property>
    <property-name>sayHello</property-name>
    <value>#{SayHello}</value>
  </managed-property>
 </managed-bean>

</faces-config>

4 web.xml内容
<?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>

5 开发Model层,是一个简单的Bean
package org.bromon.jsf.model.hello;

public class SayHello {

 public String say(String name)
 {
   return "你好:"+name ; 
 }
}
6 开发Control层,它负责存取web层的数据,并且调用Model层的逻辑。
package org.bromon.jsf.control.hello;

import org.bromon.jsf.model.hello.SayHello;

public class HelloDelegater {

 private String name;

 private String result;

 private SayHello sayHello;

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }

 public String getResult() {
  return result;
 }

 public void setResult(String result) {
  this.result = result;
 }

 public SayHello getSayHello() {
  return sayHello;
 }

 public void setSayHello(SayHello sayHello) {
  this.sayHello = sayHello;
 }
 
 //逻辑方法
 public String say()
 {
    this.setResult(sayHello.say(this.getName())) ;
    return "OK";
 }

}

7 最后编写一个表示层的页面index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="GB2312"%>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
   
    <title>My JSP 'index.jsp' starting page</title>
 <meta http-equiv="pragma" content="no-cache">
 <meta http-equiv="cache-control" content="no-cache">
 <meta http-equiv="expires" content="0">   
 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
 <meta http-equiv="description" content="This is my page">
 <!--
 <link rel="stylesheet" type="text/css" href="styles.css">
 -->
  </head>
 
  <body>
    <f:view>
      <h:form>
        <h:panelGrid columns="3">
          <h:outputLabel for="name" value="姓名"/>
          <h:inputText id="name" value="#{HelloDelegater.name}" required="true"></h:inputText>
          <h:message for="name"></h:message>
          <h:outputLabel value="#{HelloDelegater.result}"></h:outputLabel>
        </h:panelGrid>
        <h:panelGroup>
           <h:commandButton action="#{HelloDelegater.say}" value="提交"></h:commandButton>
        </h:panelGroup>
      </h:form>
    </f:view>
  </body>
</html>

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值