struts2框架快速入门小案例

struts2快速入门:
  index.jsp------>HelloAction--------->hello.jsp    struts2流程
  1.导入jar包
    struts2的目录结构:
      apps: 例子程序
      docs:文档
      lib:struts2框架所应用的jar以及插件包
      src:源代码
        core 它是struts2的源代码
        xwork-core struts2底层使用了xwork,xwork的源代码
    注意:在struts2开发,通常状况下最少导入的jar包,去apps下的struts2-blank示例程序中copy
  2.建立index.jsp页面前端

  3.对struts2框架进行配置
    1.web.xml文件中配置前端控制器(核心控制器)-----就是一个Filter(目的:是为了让struts2框架能够运行。 web

<filter>
  <filter-name>struts2</filter-name>
  <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
  <filter-name>struts2</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>

    2.在src下(对应Tomcat的classes下)建立一个struts.xml配置文件 ,这个是struts2框架配置文件(目的:是为了struts2框架流程能够执行。
  4.建立一个HelloAction类apache

//要求,在HelloAction类中建立一个返回值是String类型的方法,注意,无参数。
public String say(){
  return "good";
}

  5.在struts.xml文件中配置HelloAction
app

<package name="default" namespace="/" extends="struts-default">
  <action name="hello" class="cn.yzu.action.HelloAction" method="say">
    <result name="good">/hello.jsp</result>
  </action>
</package>

  6.在index.jsp中添加链接,测试框架

<a href="${pageContext.request.contextPath}/hello">第一次使用struts2</a>

  在地址栏中输入:http://localhost/struts2_day01/index.jsp 访问链接,就能够看到HelloAction类中的say方法执行了,也跳转到了hello.jsp.jsp

运行流程:测试

 用filter模拟Struts2工做原理:this

public class StrutsFilter implements Filter {
    public void init(FilterConfig filterConfig) throws ServletException {}
    public void destroy() {}
    public void doFilter(ServletRequest req, ServletResponse resp,
            FilterChain chain) throws IOException, ServletException {
        // 1.强转
        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) resp;
        // 2.操做
        // 2.1 获得请求资源路径
        String uri = request.getRequestURI();
        String contextPath = request.getContextPath();
        String path = uri.substring(contextPath.length() + 1);
        // 2.2 使用path去struts.xml文件中查找某一个<action name=path>这个标签
        SAXReader reader = new SAXReader();
        try {
            // 获得struts.xml文件的document对象。
            Document document = reader.read(new File(this.getClass().getResource("/struts.xml").getPath()));
            // 查找<action name='hello'>这样的标签
            Element actionElement = (Element) document.selectSingleNode("//action[@name='" + path + "']"); 
            if (actionElement != null) {
                // 获得<action>标签上的class属性以及method属性
                String className = actionElement.attributeValue("class"); // 获得了action类的名称
                String methodName = actionElement.attributeValue("method");// 获得action类中的方法名称。
                // 2.3经过反射,获得Class对象,获得Method对象
                Class actionClass = Class.forName(className);
                Method method = actionClass.getDeclaredMethod(methodName);
                // 2.4 让method执行.
                String returnValue = (String) method.invoke(actionClass.newInstance()); // 是让action类中的方法执行,并获取方法的返回值。
                // 2.5 使用returnValue去action下查找其子元素result的name属性值,与returnValue作对比。
                Element resultElement = actionElement.element("result");
                String nameValue = resultElement.attributeValue("name");
                if (returnValue.equals(nameValue)) {
                    // 2.6获得了要跳转的路径。
                    String skipPath = resultElement.getText();
                    request.getRequestDispatcher(skipPath).forward(request,response);
                    return;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } 
        // 3.放行
        chain.doFilter(request, response);
    }
}
相关文章
相关标签/搜索