全部程序如下:
/*
*包名?JavaBean
*类名:UserForm
*功能:接收表单数据
*/
package JavaBean;
public class UserForm extends ActionForm{//ActionForm的子类
private String username=null;
private String passwd=null;
public UserForm(){}
//Set The Propety
public void setUsername(String username){
this.username=username;
}
public void setPasswd(String passwd){
this.passwd=passwd;
}
//Get The Propety
public String getUsername(){
return username;
}
public String getPasswd(){
return passwd;
}
}
//简单测试的BEAN
package JavaBean;
//纯javabean
public class CheckName{
String username=new String();
String passwd=new String();
public CheckName(String username,String passwd){
this.username=username;
this.passwd=passwd;
}
public Boolean Succsse(){
Boolean f=new Boolean(false);
try{
if(username=="ming206"&&passwd=="123456"){
f=new Boolean(true);
}
}catch(Exception e){System.out.print("错误:"+e.getMessage());}
finally{
//返回正确与否
return f;
}
}
}
/*
*包名?JavaBean
*类名:LoginAction
*功能:处理表单数据
*/
package JavaBean;
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 final class LoginAction extends Action{//Action的子类
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception{
UserForm userform=(UserForm) form;
String name=userform.getUsername();
String pwd=userform.getPasswd();
Boolean c=new Boolean(false);
try{
Boolean c=new Boolean(new CheckName(name,pwd).Succsse());
if(c){
//用户名和密码都正确时转向right.jsp页面
return mapping.findForword("Successed");
}else{
//否则转向error.jsp页面
return mapping.findForword("Failed");
}
}catch(Exception e){out.print(e.getMessage());}
}
}
struts-config.xml文件:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE struts-config PUBLIC
"-//Apache software Foundtion//DTD Struts Configration 1.1//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<strutes-config>
<!--ActionForm Bean-->
<form-beans>
<form-bean name="formBean1" type="JavaBean.UserForm"/>
</form-beans>
<!--ActionForward-->
<global-forwards>
<forward name="Failed" path="/error.jsp"/>
<forward name="Successed" path="/right.jsp"/>
</global-forwards>
<!--Action Bean-->
<action-mappings>
<action path="/login" type="JavaBean.LoginAction" name="formBean1" scope="request" input="login.jsp"/>
<action path="/regist" forward="regist.jsp"/>
</action-mappings>
</struts-config>