1、写登录LoginAction
[[封装对象 user]]
public class User {
private long id;
private String username;
private String password;
private String email;
private String phone;
private String sex;
private int age;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getUsername() {
System.out.println(username +" username");
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
System.out.println(password + " password");
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
Action:
public class LoginAction extends ActionSupport{
/**
*
*/
private static final long serialVersionUID = 1L;
private User user;
private String message;
private String password;
private String realpath;
public String execute(){
message = "hello world";
return SUCCESS;
}
public User getUser() {
System.out.println("!!!!!!!!!!!!!!!user.getUser()");
return user;
}
public void setUser(User user) {
System.out.println("user.setUser");
if(user != null){
System.out.println(user.getUsername() +" @@@@@@@@@@@@@@@@ "+"user.getUsername()");
}
this.user = user;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getPassword() {
System.out.println("password bukaixin############################## "+password);
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getRealpath() {
realpath="error";
return realpath;
}
public void setRealpath(String realpath) {
this.realpath = realpath;
}
}
2、写拦截器类
public class InterceptLogin extends AbstractInterceptor{
private static final long serialVersionUID = 1L;
@Override
public String intercept(ActionInvocation invocation) throws Exception {
String result = null;
LoginAction action =(LoginAction)invocation.getAction();
if(action != null){
System.out.println("拦截开始 " +action.getMessage() + action.getUser() );
System.out.println(action.toString()+" fdfdfdfdfd");
}
if(action == null){
System.out.println("action is null");
}
long date = System.currentTimeMillis();
// System.out.println(date);
if(action.getUser().getUsername().equals("1234")){
result = invocation.invoke();
}else{
return "error";
}
long end = System.currentTimeMillis();
System.out.println("拦截结束");
System.out.println(end - date);
System.out.println(result +" " +action.getMessage() +" "+ action.getUser());
return result;
}
3、写配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="default-qizheng" namespace="/aaa" extends="struts-default">
<interceptors>
<interceptor name="interceptLogin" class="com.qizheng.manager.util.InterceptLogin" />
<interceptor-stack name="interLogin">
<interceptor-ref name="defaultStack"></interceptor-ref>
<interceptor-ref name="interceptLogin"></interceptor-ref>
</interceptor-stack>
</interceptors>
<global-results>
<result name="login">/login.jsp</result>
</global-results>
<action name="su" class="com.qizheng.manager.action.LoginAction">
<result name="success" type="dispatcher">/success.jsp</result>
<result name="error" type="dispatcher">/${realpath}.jsp</result>
<interceptor-ref name="interLogin"></interceptor-ref>
</action>
</package>
</struts>
里面有许多自己调试过的地方和尝试不同方式的地方,有一定基础的话,你应该懂的。里面用到了一点ogal的ValueStack的知识,应该要学的。