简单的JSF框架搭建



步骤

  1. 1

    新建dynamic web project,选择configuration,如图所示:

  2. 2

    新建之后,显示如下:

  3. 3

    创建MyJsfAction.java

    package com.dw.action;

    import javax.faces.event.ValueChangeEvent;

    public class MyJsfAction {

    private String type;

    public String getType() {

    return type;

    }

    public void setType(String type) {

    this.type = type;

    }

    // 定义监听值改变的方法

    public void selectType(ValueChangeEvent event){

    String nameTemp = (String)event.getNewValue();

    if("JAVA".equals(nameTemp)){

    type = "JAVA";

    }else{

    type = "C";

    }

    }

    public String gourl(){

    return type;

    }

    }

  4. 4

    新建userChoose.jsp

    <%@ page language="java" import="java.util.*"

    contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

    <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>

    <%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

    <html>

    <head>

    <title>My JSF 'userChoose.jsp' starting page</title>

    </head>

    <body>

    JSF测试信息!

    <br/>

    <f:view>

    <h:form>

    请选择查询项目:<br />

    <h:selectOneMenu value="#{dw.type}"

    valueChangeListener="#{dw.selectType}" id="selecttype">

    <f:selectItem itemValue="JAVA" itemLabel="JAVA" />

    <f:selectItem itemValue="C" itemLabel="C" />

    </h:selectOneMenu>

    <h:commandButton value="提交" action="#{dw.gourl}">

    </h:commandButton>

    </h:form>

    </f:view>

    </body>

    </html>

  5. 5

    新建newJava.jsp

    <%@ page language="java" contentType="text/html; charset=UTF-8"

        pageEncoding="UTF-8"%>

    <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>

    <%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

    <html>

    <head>

    <title>My JSP 'newJava.jsp' starting page</title>

    </head>

    <body>

    JSF测试信息!<br/>

    <f:view>

    <h:form>

    欢迎学习<h:outputText value="#{dw.type}"></h:outputText>语言,祝你成功!<br/>

    </h:form>

    </f:view>

    </body>

    </html>

  6. 6

    新建newC.jsp

    <%@ page language="java" contentType="text/html; charset=UTF-8"

        pageEncoding="UTF-8"%>

    <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>

    <%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

    <html>

    <head>

    <title>My JSP 'newC.jsp' starting page</title>

    </head>

    <body>

    JSF测试信息!<br/>

    <f:view>

    <h:form>

    欢迎学习<h:outputText value="#{dw.type}"></h:outputText>语言,祝你成功!<br/>

    </h:form>

    </f:view>

    </body>

    </html>

  7. 7

    修改配置文件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">

    <!-- 配置托管Bean -->

    <managed-bean>

    <managed-bean-name>dw</managed-bean-name>

    <managed-bean-class>com.dw.action.MyJsfAction</managed-bean-class>

    <managed-bean-scope>session</managed-bean-scope>

    </managed-bean>

    <!-- 定义导航规则与页面 -->

    <navigation-rule>

    <from-view-id>/userChoose.jsp</from-view-id>

    <navigation-case>

    <from-outcome>JAVA</from-outcome>

    <to-view-id>/newJava.jsp</to-view-id>

    </navigation-case>

    <navigation-case>

    <from-outcome>C</from-outcome>

    <to-view-id>/newC.jsp</to-view-id>

    </navigation-case>

    </navigation-rule>

    </faces-config>

  8. 8

    修改web.xml

    <?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"

    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"

    id="WebApp_ID" version="2.5">

    <display-name>FirstJSFTestProject</display-name>

    <listener>

    <listener-class>org.apache.myfaces.webapp.StartupServletContextListener</listener-class>

    </listener>

    <!-- 配置JSF的配置文件 -->

    <context-param>

    <param-name>javax.faces.CONFIG_FILES</param-name>

    <param-value>/WEB-INF/faces-config.xml</param-value>

    </context-param>

    <!-- 处理请求的servlet -->

    <servlet>

    <servlet-name>Faces Servlet</servlet-name>

    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>

    <load-on-startup>0</load-on-startup>

    </servlet>

    <!-- 配置所有.jsf结尾的请求都由JSF处理 -->

    <servlet-mapping>

    <servlet-name>Faces Servlet</servlet-name>

    <url-pattern>*.jsf</url-pattern>

    </servlet-mapping>

    </web-app>

  9. 9

    Run on server,浏览器访问http://localhost:8080/FirstJSFTestProject/userChoose.jsf(否则,会报错!)

  10. 10

    项目的代码结构如图所示:

  11. 11

    Jar下载链接:http://archive.apache.org/dist/jakarta/taglibs/standard/binaries/

    eclipse搭建JSF简单示例的教程(亲测)

https://jingyan.baidu.com/article/75ab0bcbceb527d6864db286.html


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值