一、struts2 入门

2017-12-27  本文已影响0人  cqzhangjian

Struts2是一个基于MVC设计模式的Web应用框架,它本质上相当于一个servlet,在MVC设计模式中,Struts2作为控制器(Controller)来建立模型与视图的数据交互。

1.MVC 设计模式

2.三层架构

缺点:

3.Struts2 历史

早期现有struts1,struts1和struts2都是apache的产品,struts1在2003年左右比较火.在当时,表现层框架有很多:webwork框架。

Struts1比较出名因为后台是apache,webwork不是很出名

Webwork框架的设计思想比较前卫(AOP),设计思想沿用至今,struts1的耦合性(servlet api)很紧密,入侵性,就导致使用struts1扩展性差

图片.png

4.第一个Struts2 小程序

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns="http://java.sun.com/xml/ns/javaee"
   xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
   id="WebApp_ID" version="3.0">
   
   
   <!-- 配置一个 过滤器:该过滤器是 struts2 提供的 过滤器 :StrutsPrepareAndExecuteFilter (前端控制器、中央控制器)
        在 玩 struts2 的使用认识两种控制器 : StrutsPrepareAndExecuteFilter
                          控制器(页面(逻辑)控制器): 自定义的 
   -->
   <filter>
       <filter-name>struts2</filter-name>
       <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
   </filter>
   <filter-mapping>
       <filter-name>struts2</filter-name>
       <url-pattern>*.action</url-pattern>
   </filter-mapping>
   
</web-app>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
        "http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
    <package name="p1" extends="struts-default" namespace="/hello">
        <action name="struts2" class="com.xingxue.xingxue.web.controller.HelloController">
            <result name="ok">
                /ok.jsp
            </result>
        </action>
    </package>
</struts>
package com.xingxue.xingxue.web.controller;

public class HelloController {

    public String execute() {
        System.out.println("execute 方法执行了...");
        return "ok";
    }

}

5.Struts2 执行流程

6.Action 动作类创建三种方式

public class HelloController {
/**
     * The action execution was successful. Show result
     * view to the end user.
     */
    public static final String SUCCESS = "success";

    /**
     * The action execution was successful but do not
     * show a view. This is useful for actions that are
     * handling the view in another fashion like redirect.
     */
    public static final String NONE = "none";

    /**
     * The action execution was a failure.
     * Show an error view, possibly asking the
     * user to retry entering data.
     */
    public static final String ERROR = "error";

    /**
     * <p>
     * The action execution require more input
     * in order to succeed.
     * This result is typically used if a form
     * handling action has been executed so as
     * to provide defaults for a form. The
     * form associated with the handler should be
     * shown to the end user.
     * </p>
     *
     * <p>
     * This result is also used if the given input
     * params are invalid, meaning the user
     * should try providing input again.
     * </p>
     */
    public static final String INPUT = "input";

    /**
     * The action could not execute, since the
     * user most was not logged in. The login view
     * should be shown.
     */
    public static final String LOGIN = "login";
public class HelloController2 implements Action {

    @Override
    public String execute() throws Exception {
        return null;
    }
public class HelloController3 extends ActionSupport {

}

7.接收请求数据

public class RequestController {
    // 动作类的方法:1,修饰符是 public 2.动作方法没有形参,3.方法的返回值有 String 或者 void

    public String name;
    public String password;

public class RequestController {
    // 动作类的方法:1,修饰符是 public 2.动作方法没有形参,3.方法的返回值有 String 或者 void

    // public String name;
    // public String password;

    // 定义接收参数的模型
    public UserModel model = new UserModel();
<a href="${pageContext.request.contextPath }/p1/req2.action?model.name=lisi&model.password=123">a标签提交请求参数</a>

8.动态视图数据处理

处理的数据带到页面的操作

    HttpServletRequest request = ServletActionContext.getRequest();
    HttpSession session = request.getSession();
    ServletContext servletContext = ServletActionContext.getServletContext();
    ActionContext context = ActionContext.getContext();
        // 请求域中放数据
    context.put("", "");
    Map<String, Object> session = context.getSession();
    Map<String, Object> application = context.getApplication();
上一篇 下一篇

猜你喜欢

热点阅读