zoukankan      html  css  js  c++  java
  • Struts2的运行机制简介

    1、客户端通过URL请求tomcat

    2、URL找到对应站点的WEB.xml  发现里面有  struts2配置

    3、执行StrutsPrepareAndExecuteFilter类的init方法

    4、在init方法中,找到  struts.xml 文件

    5、解析 struts.xml文件,根据URL找到对应的类及执行的方法(暂时命名为RunClass)

    6、方法会返回一个String

    7、在 struts.xml中,根据返回的String ,返回给客户端页面JSP内容

    (备注:在返回的JSP中,可以使用RunClass类中的成员变量,直接引用变量名即可    <s:property value="str" />  str即为变量名)

     相关配置及代码:

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.5" 
        xmlns="http://java.sun.com/xml/ns/javaee" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
        http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
      <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
      </welcome-file-list>
      
      <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>
    </web-app>
    web.xml
    <?xml version="1.0" encoding="UTF-8" ?> 
    <!DOCTYPE struts PUBLIC
          "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
          "http://struts.apache.org/dtds/struts-2.0.dtd">
    <struts>
        <package name="test" namespace="/np" extends="struts-default"> 
            <action name="hello" class="test.HelloAction" method="hello"> 
                <result name="success">/hello.jsp</result> 
            </action> 
        </package>
    </struts>
    struct.xml
    package test;
    
    import com.opensymphony.xwork2.ActionSupport;
    
    public class HelloAction extends ActionSupport {
        @Override
        public String execute() throws Exception {
            // TODO Auto-generated method stub
            return super.execute();
        }
    
        private String str = "";
    
        public String hello() {
            this.str = "hello!!!";
            return "success";
        }
    
        public String getStr() {
            return str;
        }
    
        public void setStr(String str) {
            this.str = str;
        }
    }
    HelloAction.java
    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <%@ taglib prefix="s" uri="/struts-tags"%>
    <!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>Test</title>
        </head>
        <body>
            <h1>
                <s:property value="str" />
            </h1>
        </body>
    </html>
    hello.jsp
  • 相关阅读:
    yii2:如果获取config/web.php配置的值?
    yii2:引用项目外的文件或类
    yii2: oracle汉字占用字节长度
    yii2: oralce中文,有的汉字是2个字节,有的汉字是3个字节
    yii2:frontend/frontactoin curl生成
    Appium做Android功能自动化测试
    appium server日志分析
    appium的初始化准备工作
    Appium的Java封装
    使用Runtime.getRuntime().exec()在java中调用python脚本
  • 原文地址:https://www.cnblogs.com/tengpan-cn/p/5210155.html
Copyright © 2011-2022 走看看