• struts


    1.创建实体类

    package com.entity;

    public class Elogin {
     private String username;
     private String password;

     public Elogin() {
      super();
      // TODO Auto-generated constructor stub
     }

     public Elogin(String username, String password) {
      super();
      this.username = username;
      this.password = password;
     }

     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;
     }

    }

    package com.entity;
    /**
     *
    * @ClassName: User
    * @Description: (一个人)
    * @author Administrator
    * @date 2017年10月12日 上午1:47:26
    *
     */
    public class User {
     private String name;
     private int age;
     private String sex;

     public User() {
      super();
      // TODO Auto-generated constructor stub
     }

     public User(String name, int age, String sex) {
      super();
      this.name = name;
      this.age = age;
      this.sex = sex;
     }

     public String getName() {
      return name;
     }

     public void setName(String name) {
      this.name = name;
     }

     public int getAge() {
      return age;
     }

     public void setAge(int age) {
      this.age = age;
     }

     public String getSex() {
      return sex;
     }

     public void setSex(String sex) {
      this.sex = sex;
     }

    }

    2.

    package com.zking.Interceptor;

    import javax.servlet.ServletContext;
    import javax.servlet.http.HttpServletRequest;

    import org.apache.struts2.ServletActionContext;

    import com.opensymphony.xwork2.Action;
    import com.opensymphony.xwork2.ActionInvocation;
    import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;

    public class MyInterceptor extends  MethodFilterInterceptor{

     /**
      *
     * @Title: doIntercept
     * @Description: (放行的方法)
     * @param arg0
     * @return
     * @throws Exception
     * @return String
     * @throws
      */
     @Override
     protected String doIntercept(ActionInvocation actionInvocation) throws Exception {
     //取到session里面的值
      HttpServletRequest request=ServletActionContext.getRequest();
      String name=(String) request.getSession().getAttribute("login");
      if(name !=null){
       //取到则继续执行
       actionInvocation.invoke();
       //执行成功以后我也要回到的一个界面
       return Action.SUCCESS;
      }else{
       //没有获取到信息 应该要返回一个界面
       return Action.LOGIN;
      }
     }

    }

    3.

    package com.action;

    import java.util.ArrayList;
    import java.util.List;

    import com.entity.User;
    import com.opensymphony.xwork2.ActionContext;
    import com.opensymphony.xwork2.ActionSupport;
    import com.opensymphony.xwork2.util.ValueStack;

    public class IndexAction extends ActionSupport {

     private String name;

     public String getName() {
      return name;
     }

     private List<User> lu = new ArrayList<>();

     public List<User> getLu() {
      return lu;
     }

     @Override
     public String execute() throws Exception {
      System.out.println("不离不弃");
      return SUCCESS;
     }

     public String add() {

      // ActionContext actionContext =ActionContext.getContext();
      // ValueStack v1=actionContext.getValueStack();
      // v1.push("PUSH的值");
      // v1.set("zking","set的值");

      name = "悟空";
      
      lu.add(new User("悟空", 999, "石头屌"));

      return SUCCESS;
     }

    }

    package com.action;

    import com.entity.Elogin;
    import com.opensymphony.xwork2.ActionSupport;
    import com.opensymphony.xwork2.ModelDriven;

    public class LoginAction extends ActionSupport implements ModelDriven<Elogin>{

     @Override
     public String execute() throws Exception {
      // TODO Auto-generated method stub
      return SUCCESS;
     }
     public String admin(){
      System.out.println(elogin.getUsername()+"加油"+elogin.getPassword());
      return SUCCESS;
     }
     private Elogin elogin=new Elogin();
     
     @Override
     public Elogin getModel() {
      // TODO Auto-generated method stub
      return elogin;
     }
    }

    package com.action;

    import com.entity.Elogin;
    import com.opensymphony.xwork2.ActionSupport;

    public class LoginAction2 extends ActionSupport{
     
     @Override
     public String execute() throws Exception {
      // TODO Auto-generated method stub
      return SUCCESS;
     }
     
     public String admin(){
      System.out.println(elogin.getUsername()+"不放弃"+elogin.getPassword());
      return SUCCESS;
     }
     
     public Elogin elogin;

     public Elogin getElogin() {
      return elogin;
     }

     public void setElogin(Elogin elogin) {
      this.elogin = elogin;
     }
     
     

    }

    package com.action;

    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpSession;

    import org.apache.struts2.ServletActionContext;

    import com.opensymphony.xwork2.ActionSupport;

    public class LoginActionInterceptor extends ActionSupport{

     
     public String login(){
      HttpServletRequest request=ServletActionContext.getRequest();
      String name = request.getParameter("username");
      String pwd = request.getParameter("password");
      //成功存入session
      HttpSession session= request.getSession();
      if("admin".equalsIgnoreCase(name)&&"123".equals(pwd)){
       session.setAttribute("login", name);
       return SUCCESS;
      }else {
       return LOGIN;
      } 
     }
    }

    package com.action;

    import javax.servlet.http.HttpServletRequest;

    import org.apache.struts2.ServletActionContext;

    import com.opensymphony.xwork2.ActionSupport;

    public class TokenAction extends ActionSupport {

     @Override
     public String execute() throws Exception {

      HttpServletRequest request = ServletActionContext.getRequest();
      String name = request.getParameter("username");
      String pwd = request.getParameter("password");
      System.out.println(name + "------" + pwd);
      // 获取表单提交过的信息
      return SUCCESS;
     }
    }

  • 相关阅读:
    ASP.NET 防盗链的实现[HttpHandler]
    html打印表格每页都有的表头和打印分页
    spring是怎样管理mybatis的及注入mybatis mapper bean的
    浅谈Log4j和Log4j2的区别
    git tag — 标签相关操作
    java cocurrent包
    线程实现异步
    使用Shell脚本查找程序对应的进程ID,并杀死进程
    shell脚本监测文件变化
    spring boot的几种配置类型
  • 原文地址:https://www.cnblogs.com/199717-shi/p/7726461.html
Copyright © 2020-2023  润新知