index.xhtml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core"> <h:head> <title>welcome</title> <Meta http-equiv="keywords" content="enter,your,keywords,here" /> <Meta http-equiv="description" content="A short description of this page." /> <Meta http-equiv="content-type" content="text/html; charset=UTF-8" /> </h:head> <h:body> <h:form prependId="false"> <h3>请输入用户名和密码</h3> <table> <tr> <td>姓名:</td> <td><h:inputText value="#{user.name}" id="name"></h:inputText></td> </tr> <tr> <td>密码:</td> <td><h:inputSecret value="#{user.password}" id="password"></h:inputSecret></td> </tr> </table> <p> <h:commandButton value="Login"> <f:ajax execute="name password" render="out"></f:ajax> </h:commandButton> </p> <h3><h:outputText id="out" value="#{user.greeting}"/></h3> </h:form> </h:body> </html>
UserBean
package com.corejsf; import java.io.Serializable; import javax.faces.bean.ManagedBean; import javax.faces.bean.SessionScoped; @ManagedBean(name="user") @SessionScoped public class UserBean implements Serializable{ private static final long serialVersionUID = 1L; private String name; private String password; private String greeting; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getGreeting() { if(name == null) return " "; return "welcome to jsf2+ajax"+name+"!"; } public void setGreeting(String greeting) { this.greeting = greeting; } }
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" version="2.5"> <display-name></display-name> <welcome-file-list> <welcome-file>faces/index.xhtml</welcome-file> </welcome-file-list> <servlet> <servlet-name>Faces Servlet</servlet-name> <servlet-class>javax.faces.webapp.FacesServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>Faces Servlet</servlet-name> <url-pattern>/faces/*</url-pattern> </servlet-mapping> <context-param> <param-name>javax.faces.PROJECT_STAGE</param-name> <param-value>DEVELOPMENT</param-value> </context-param> </web-app>
工程目录结构
URL
http://localhost:8080/JSFDemo1/faces/login/web/index.xhtml
页面效果
原文链接:https://www.f2er.com/ajax/163252.html