登陆案例
步骤一:struts.xml文件
<!-- 登陆的action -->
<action name="login" class="cn.happy.action.LoginAction">
<result name="success">
login/success.jsp
</result>
<result name="login">
login/login.jsp
</result>
</action>
步骤二:LoginAction类的创建
package cn.happy.action;
import com.opensymphony.xwork2.Action;
public class LoginAction implements Action{
private String username = "";
private String password = "";
public String execute() throws Exception {
if (username.equals("1")&&password.equals("1")) {
return SUCCESS;
}else {
return LOGIN;
}
}
}
步骤三:创建登陆界面
<s:form name="form1" namespace="/" method="post" action="login">
请输入用户名: <s:textfield name="username"></s:textfield> <br/>
<s:textfield name="password"></s:textfield><br/>
<s:submit value="登陆"></s:submit>
</s:form>
步骤四:在浏览器中访问
登陆案例强化:关于自动装配问题
在开发中,通常会以JavaBean方式保存数据。所以可以有如下写法
Action类
public class LoginSecondAction2 implements Action{
private User user;
public String execute() throws Exception{
if (user.getUname().equals("admin")&&user.getUpwd().equals("admin")){
}else{
return LOGIN;
}
}
jsp页面