Struts-day01

一.基本概念:

  Struts2是一种基于MVC模式的轻量级Web框架

二.入门案例:

  第一步:拷贝struts2必备jar包到web工程的lib目录

  

  第二步:在类的根路径下创建一个名称为struts.xml的文件,并导入约束

  

  

<?xml version="1.0" encoding="UTF-8"?>
<!-- 导入约束:
    约束的位置:在struts2的核心jar包中
            struts2-core-2.3.24.jar中包含一个名称为:
                struts-2.3.dtd的约束文件
-->
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts></struts>

  

  第三步:在web.xml配置struts2的核心控制器(一个过滤器)

扫描二维码关注公众号,回复: 3114418 查看本文章
<?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_2_5.xsd"
    id="WebApp_ID" version="2.5">
    <display-name>day01struts2template</display-name>
    
    <!-- 配置struts2的核心控制器:一个过滤器 -->
    <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>
    
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
    </welcome-file-list>
</web-app>

  第四步:Web应用中编写一个index.jsp页面,放到WebContent目录下。如下图:

  index.jsp里面的内容

<%@ page language="java" contentType="text/html; charset=UTF-8" 
        pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
                "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Struts2的入门案例</title>
    </head>
    <body>
        <%--struts2的核心控制器(过滤器)默认会拦截以.action为后缀或者没有后缀的请求。
            例如:hello.action或者hello --%>
        <a href="${pageContext.request.contextPath}/hello.action">
            访问struts2的第一个动作类中的方法.action
        </a>
        <a href="${pageContext.request.contextPath}/hello">
            访问struts2的第一个动作类中的方法
        </a>
    </body>
</html>

   第五步:创建Action

  HelloAction.java里面的代码

/**
 * 我们的第一个动作类
 *     动作类:
 *         Struts2中用于处理请求的类,叫做动作类。
 *         其实就是一个普通的java类。
*/
public class HelloAction {

    /**
     * 我们把动作类中的具有特定编写规则的方法叫做动作方法。
     * 规则是:
     *      1、访问修饰符必须是public
     *      2、方法的返回值必须是String
     *      3、方法的参数列表必须是空的
     * @return
     */
    public String sayHello(){
        System.out.println(this);
        System.out.println("HelloAction中的sayHello方法执行了。。。。");
        return "success";
    }
}

  第六步:在struts.xml文件中配置

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE struts PUBLIC
 3     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
 4     "http://struts.apache.org/dtds/struts-2.3.dtd">
 5 <struts>
 6     <!-- 目前照抄 -->
 7     <package name="p1" extends="struts-default">
 8         <action name="hello" class="com.itheima.web.action.HelloAction" method="sayHello">
 9             <result name="success" type="dispatcher">/success.jsp</result>
10         </action>
11     </package>
12 </struts>

  第七步:WebContent中编写一个success.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"  pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
执行成功!
</body>
</html>

  访问结果

三.执行过程:

首先是,启动tomcat服务器,这时候会加载web.xml,当读到filter标签时,会创建过滤器对象。struts2的核心过滤器(StrutsPrepareAndExecuteFilter)会负责加载类路径下的struts.xml配置文件。接下来,从客户端发送请求过来 先经过前端控制器(核心过滤器StrutsPrepareAndExecuteFilter),前端控制器会根据请求的名称在struts.xml中找到对应的配置,创建我们的动作类对象(每次访问时都会创建新的Action对象),然后执行指定的方法,根据方法的返回值找到Result的配置进行页面的跳转.最后响应浏览器。

四.struts.xml配置信息详解

<?xml version="1.0" encoding="UTF-8"?>
  <!DOCTYPE struts PUBLIC
      "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
      "http://struts.apache.org/dtds/struts-2.3.dtd">
  <struts>
      <!-- 目前照抄 -->
      <package name="p1" extends="struts-default">
          <action name="hello" class="com.itheima.web.action.HelloAction" method="sayHello">
              <result name="success" type="dispatcher">/success.jsp</result>
         </action>
     </package>
</struts>

package配置

  1.name属性  作用:定义一个包的名称,它必须唯一。

  2.namespace属性 作用:主要是与action标签的name属性联合使用来确定一个action 的访问路径

  3.extends属性 作用:指定继承自哪个包。一般值是strtus-defaultstrtus-default包是在strtus-default.xml文件中声明的。

  4.abstruct属性 它代表当前包是一个抽象的,主要是用于被继承 

action配置

  1.name属性 作用:主要是与packagenamespace联合使用来确定一个action的访问路

  2.class属性  作用:用于指示当前的action

  3.method属性  作用:用于指示当前的action类中的哪个方法执行

result配置

  它主要是用于指示结果视图

  1.name属性 作用是与action类的method方法的返回值进行匹配,来确定跳转路径

  2.type属性 作用是用于指定跳转方式

五.Struts类创建方式

  1.无侵入式创建

  2.实现Action接口

  3.继承ActionSupport

 

猜你喜欢

转载自www.cnblogs.com/hellopaidaxing/p/9619343.html