jsf user login

The overall logic is to provide a login page for the user to log in. If it succeeds, it will jump to the success page, and if it fails, it will stay on the login page.

Login page: bind parameters through bean object.property and bean object.method

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
	xmlns:ui="http://java.sun.com/jsf/facelets"
	xmlns:f="http://java.sun.com/jsf/core"
	xmlns:h="http://java.sun.com/jsf/html">
<head>	
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Insert title here</title>
</head>
<body>
<h:form>
	用户名:<h:inputText value="#{userBean.user}"></h:inputText><br/><br/>
	密码:<h:inputSecret value="#{userBean.pwd}"></h:inputSecret><br/><br/>
	<h:commandButton value="登录" action="#{userBean.login()}"></h:commandButton>
	<h:commandButton value="注册" ></h:commandButton>
</h:form>
</body>
</html>

bean class

package bean;

public class UserBean {
	private String user;
	private String pwd;

	public String getUser() {
		return user;
	}

	public void setUser(String user) {
		this.user = user;
	}

	public String getPwd() {
		return pwd;
	}

	public void setPwd(String pwd) {
		this.pwd = pwd;
	}

	public String login() {
		if ("1".equals(this.user) && "1".equals(this.pwd)) {
			return "success";
		}
		return "fail";
	}

}

faces-config.xml

<managed-bean>
	    <managed-bean-name>userBean</managed-bean-name>
	    <managed-bean-class>bean.UserBean</managed-bean-class>
	    <managed-bean-scope>session</managed-bean-scope>
	</managed-bean>

	<navigation-rule>
	    <display-name>userBean</display-name>
	    <from-view-id>login.xhtml</from-view-id>
	    <navigation-case>
	        <from-outcome>success</from-outcome>
	        <to-view-id>success.xhtml</to-view-id>
	    </navigation-case>
	    <navigation-case>
	        <from-outcome>fail</from-outcome>
	        <to-view-id>login.xhtml</to-view-id>
	    </navigation-case>
	</navigation-rule>

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325599464&siteId=291194637
jsf