struts2总结

struts2

作者 knight 日期 2017-10-18
struts2总结

人身难得今已得,佛法难闻今已闻,此身不向今生度,更待何生度此身。

1.结果跳转方式

  1. 转发

    <action name="DemoAction" class="cn.knight.DemoAction" method="execute">
    <result name="success" type="dispatcher">/hello.jsp</result>
    </action>
  2. 重定向

    <action name="DemoAction" class="cn.knight.DemoAction" method="execute">
    <result name="success" type="redirect">/hello.jsp</result>
    </action>
  3. 转发Action

    <action name="DemoAction" class="cn.knight.DemoAction" method="execute">
    <result name="success" type="chain">
    <!--action的名字-->
    <param name ="actionName">DemoAction</param>
    <!--action的所在命名空间-->
    <param name ="namespace">/</param>
    </result>
    </action>
  4. 重定向Action

    <action name="DemoAction" class="cn.knight.DemoAction" method="execute">
    <result name="success" type="redirectAction">
    <!--action的名字-->
    <param name ="actionName">DemoAction</param>
    <!--action的所在命名空间-->
    <param name ="namespace">/</param>
    </result>
    </action>

2.访问ServletAPI方式

  1. 原理
  2. 通过ActionContext

    //如何在action中获得原生ServletAPI
    public class Demo5Action extends ActionSupport {
    public String execute() throws Exception {
    //request域=> map (struts2并不推荐使用原生request域)
    //不推荐
    Map<String, Object> requestScope = (Map<String, Object>) ActionContext.getContext().get("request");
    //推荐
    ActionContext.getContext().put("name", "requestTom");
    //session域 => map
    Map<String, Object> sessionScope = ActionContext.getContext().getSession();
    sessionScope.put("name", "sessionTom");
    //application域=>map
    Map<String, Object> applicationScope = ActionContext.getContext().getApplication();
    applicationScope.put("name", "applicationTom");
    return SUCCESS;
    }
    }
  3. 通过ServletActionContext

    //并不推荐
    public String execute() throws Exception {
    //原生request
    HttpServletRequest request = ServletActionContext.getRequest();
    //原生session
    HttpSession session = request.getSession();
    //原生response
    HttpServletResponse response = ServletActionContext.getResponse();
    //原生servletContext
    ServletContext servletContext = ServletActionContext.getServletContext();
    return SUCCESS;
    }
  4. 通过实现接口的方式

    public class Demo7Action extends ActionSupport implements ServletRequestAware {
    private HttpServletRequest request;
    public String execute() throws Exception {
    System.out.println("原生request:"+request);
    return SUCCESS;
    }
    @Override
    public void setServletRequest(HttpServletRequest request) {
    this.request = request;
    }
    }

3.参数

  1. 属性驱动
    |-Action准备与参数键同名属性
    
    jsp
    <form action="${pageContext.request.contextPath}/Demo8Action">
    用户名:<input type="text" name="name" /><br>
    年龄:<input type="text" name="age" /><br>
    生日:<input type="text" name="birthday" /><br>
    <input type="submit" value="提交" />
    </form>

Action

//准备与参数键名称相同的属性
private String name;
//自动类型转换 只能转换8大基本数据类型以及对应包装类
private Integer age;
//支持特定类型字符串转换为Date ,例如 yyyy-MM-dd
private Date birthday;

  1. 对象驱动
    |-Action准备与参数键同名属性. xxx.yyy
    
    jsp
    <form action="${pageContext.request.contextPath}/Demo9Action">
    用户名:<input type="text" name="user.name" /><br>
    年龄:<input type="text" name="user.age" /><br>
    生日:<input type="text" name="user.birthday" /><br>
    <input type="submit" value="提交" />
    </form>

Action

public class Demo9Action extends ActionSupport {
//准备user对象
private User user;
public String execute() throws Exception {
System.out.println(user);
return SUCCESS;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}

  1. 模型驱动
    jsp

    <form action="${pageContext.request.contextPath}/Demo10Action">
    用户名:<input type="text" name="name" /><br>
    年龄:<input type="text" name="age" /><br>
    生日:<input type="text" name="birthday" /><br>
    <input type="submit" value="提交" />
    </form>

    |- 1.实现ModelDriven接口
    |- 2.实现getModel方法.返回需要封装参数的对象

    public class Demo10Action extends ActionSupport implements ModelDriven<User> {
    //准备user 成员变量
    private User user =new User();
    public String execute() throws Exception {
    System.out.println(user);
    return SUCCESS;
    }
    @Override
    public User getModel() {
    return user;
    }
    }
  1. 复杂参数封装
    List集合
    提交键: list
    list[index]
    
    Map结合
    提交键: map['key']