步骤
-
新建dynamic web project,选择configuration,如图所示:
-
新建之后,显示如下:
-
创建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;
}
}
-
新建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>
-
新建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>
-
新建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>
-
修改配置文件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>
-
修改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>
-
Run on server,浏览器访问http://localhost:8080/FirstJSFTestProject/userChoose.jsf(否则,会报错!)
-
项目的代码结构如图所示:
-
Jar下载链接:http://archive.apache.org/dist/jakarta/taglibs/standard/binaries/
https://jingyan.baidu.com/article/75ab0bcbceb527d6864db286.html