JSF2表单提交------JSF学习笔记3

1.jsp文件关键代码

 <h:form>
	 <h:outputText  value="#{(user.reMsg!=null)?user.reMsg:''}"/>
	<h3>Please enter your name and password.</h3>
	<table>
	   <tr>
		  <td>Name:</td>
		  <td>
			 <h:inputText value="#{user.name}"  required="true" id="name" requiredMessage="请输入Name" />
			 <span style="color: red">*</span>
			 <h:message for="name"></h:message><br>
		  </td>
	   </tr>             
	   <tr>
		  <td>Password:</td>
		  <td>
			 <h:inputSecret value="#{user.password }" id="password" requiredMessage="请输入Password" required="true">
				<f:validateLength minimum="6" maximum="16"></f:validateLength>	<!-- 密码长度6到16位 -->
			 </h:inputSecret>
			<span style="color: red">*</span>
			<h:message for="password"></h:message><br>
		  </td>
	   </tr>
	</table>
	<p>
	   <h:commandButton value="Login" action="#{user.logonValidate }"/>
	</p>
 </h:form>

 

2.faces-config.xml配置文件(使用l注解 无managedbean)

<navigation-rule>
	<from-view-id>/logon/logon.jsp</from-view-id>
	<navigation-case>
		<from-action>#{user.logonValidate }</from-action>
		<from-outcome>logonSuccess</from-outcome>	<!-- 对应bean文件内logonValidate方法d返回值 -->
		<to-view-id>/logon/logonSuccess.jsp</to-view-id>
	</navigation-case>
	
	<navigation-case>
		<from-action>#{user.logonValidate }</from-action>
		<from-outcome>logonFailure</from-outcome>	<!-- 对应bean文件内logonValidate方法d返回值 -->
		<to-view-id>/logon/logon.jsp</to-view-id>
	</navigation-case>
</navigation-rule>

  

3.bean文件

@ManagedBean(name="user")
@SessionScoped
public class UserBean {
	private String name;
	private String password;
	private String reMsg;

	//此处get set方法 省略了	
	
	public String logonValidate(){
		reMsg=null;
		if(name.equalsIgnoreCase("GuoJing")&& password.equalsIgnoreCase("888888")){
			return "logonSuccess";
		}else{
			this.setReMsg("Name or Password is wrong!");
			return "logonFailure";
		}
	}
	
}

 

你可能感兴趣的:(学习笔记)