|
方式一将VO的东西封装到Action里面 编写Action方法 package action; import comopensymphonyxworkActionSupport; public class LoginAction extends ActionSupport{ private String username; private String password; public String getUsername() { return username; } public void setUsername(String username) { thisusername = username; } public String getPassword() { return password; } public void setPassword(String password) { thispassword = password; } @Override public String execute() throws Exception { if (aaaequalsIgnoreCase(username)&&aaaequalsIgnoreCase(password)) { return loginSuc; }else { return loginFail; } } } 配置文件 <action name=mylogin class=actionLoginAction> <result name=input>/myloginjsp</result> <result name=loginSuc>/okjsp</result> <result name=loginFail>/errokjsp</result> </action> 然后编写登录的JSP页面 <s:form action=myloginaction> <table width=% align=center> <tr> <td> <s:textfield key=username label=USERNAME /> </td> </tr> <tr> <td> <s:password key=password label=PASSWORD /> </td> </tr> <tr> <td> <s:submit value=SUBMIT /> </td> </tr> </table> </s:form> 注意这里面的JSP里面的表单名称要与Action里面的名称保持一致的 也可以将其抽出来/ 方式二定义一个VO对象 自定义vo文件名LoginVOjava 文件内容 package strutslogin; public class LoginVO { private String username; private String password; public String getUsername() { return username; } public void setUsername(String username) { thisusername = username; } public String getPassword() { return password; } public void setPassword(String password) { thispassword = password; } } 在Action文件中要使用这个vo 文件内容 package strutslogin; public class LoginAction { private LoginVO user = null; public String execute() { Systemoutprintln (LoginActionclasshashCode()); if (usergetUsername()equalsIgnoreCase(aaa) && usergetPassword()equals(aaaaaa)) { return loginSuc; } else { return loginFail; } } public LoginVO getUser() { return user; } public void setUser(LoginVO user) { thisuser = user; } } 登陆成功的文件如下 <%@ page contentType=text/html; charset=gb %> <%@ taglib uri=/strutstags prefix=s%> <meta httpequiv=contenttype content=text/html;charset=gb> 欢迎您<s:property name=userusername>登录成功 注意login文件的部分也要进行修改 文件内容如下 <meta httpequiv=contenttype content=text/html;charset=gb> <title>login</title> <form action=loginaction method=post> username:<input type=input name=userusername><br> password:<input type=input name=userpassword><br> <input type=submit value=登录> </form> |