Here is a simple login page demo, a loginform, a loginaction.
LoginForm as follows:
--------------------------------------------------------------------
Package cn.itcast;
Import Org.apache.struts.action.ActionForm;
public class LoginForm extends Actionform {
/**
*
*/
Private static final long serialversionuid = 8854535717875180957L;
Private String username = null;
Private String password = null;
Public String GetUserName () {
return username;
}
public void Setusername (String username) {
This.username = Username;
}
Public String GetPassword () {
return password;
}
public void SetPassword (String password) {
This.password = password;
}
}
-----------------------------------------------------------------------------
Loginaction as follows:
-----------------------------------------------------------------------------
Package cn.itcast;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;
Import org.apache.struts.action.Action;
Import Org.apache.struts.action.ActionForm;
Import Org.apache.struts.action.ActionForward;
Import org.apache.struts.action.ActionMapping;
public class Loginaction extends Action {
@Override
Public Actionforward Execute (actionmapping mapping, Actionform form,
HttpServletRequest request, HttpServletResponse response)
Throws Exception {
LoginForm loginform = (loginform) Form;
if (Loginform.getusername (). Equals ("Itcast")) {
Return Mapping.findforward ("loginsuccess");
}else {
Return Mapping.findforward ("Loginfailure");
}
}
}
------------------------------------------------------------------------------------
We need to manage loginform and loginaction in Strust-config.xml and add the following code to this file:
<form-beans>
<form-bean name= "LoginForm" type= "Cn.itcast.LoginForm" ></form-bean>
</form-beans>
<action-mappings>
<action path= "/login" name= "LoginForm" type= "Cn.itcast.LoginAction" >
<forward name= "loginsuccess" path= "/loginsuccess.jsp" ></forward>
<forward name= "Loginfailure" path= "/loginfailure.jsp" ></forward>
</action>
</action-mappings>
Among them, the Loginaction class is responsible for login operations, LoginForm is automatically get form content.