传统三大框架(SSH)之基于Struts2的登录
1、Action方法的静态配置
Struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="UserLogin-1" extends="struts-default">
<action name="userAction" class="com.zq.control.UserAction" method="login" >
<result name="loginGet">/Login.jsp</result>
<result name="loginOK">/MyJsp.jsp</result>
<result name="loginFail">/Login.jsp</result>
</action>
</package>
</struts>
UserAction
package com.zq.control;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import com.zq.biz.UserBiz;
public class UserAction {
private String username;
private String password; //均需要和jsp页面的name相同
/**
* set get 自动帮你填充上边的值
* @return
*/
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;
}
public String login(){
String result = "loginFail";
HttpServletRequest request = ServletActionContext.getRequest();
if(request.getMethod().equals("GET")){
result = "loginGet";
}else{
UserBiz biz = new UserBiz();
boolean bRet = biz.login(username, password);
if(bRet)
result = "loginOK";
}
return result;
}
}
UserBiz
package com.zq.biz;
public class UserBiz {
public boolean login(String username,String password){
boolean bRet;
if(username != null && !username.equals("")&&password != null&& !password.equals(""))
{
bRet = true;
}else{
bRet = false;
}
return bRet;
}
}
Login.jsp
<body>
<form action="<%=basePath%>userAction.action" method ="post">
<table>
<tr>
<td>用户名:<input type="text"name="username"></input></td><br/>
<td>密码:<input type="password"name="password"/></td>
</tr>
<tr>
<td>
<input type="submit"value="登录"/>
</td>
</tr>
</table>
</form>
</body>
浏览器访问
http://localhost:8089/UserLogin/userAction.action
Action方法的静态配置 配的是login(配死了 不可取)
通过Struts中的method=login直接定位到login
2、动态配置
a、去掉method=login 在浏览器中输入 即在上述基础上加 叹号login
http://localhost:8089/UserLogin/userAction!login.action
类名 方法名 后缀
jsp中也需要改动
<form action="<%=basePath%>userAction!login.action" method ="post">
b、在Struts中修改
userAction_* method=“{1}” {1}代表前边第一个通配符
类名 方法名
<form action="<%=basePath%>userAction_login.action" method ="post">
推荐使用通配符模式