zoukankan      html  css  js  c++  java
  • struts2框架实例

    一,Struts2框架介绍

      它是一个View框架,对Servle进行了封装,使用核心过滤器对servlet进行了解耦,可以自动封装数据  核心是结果视图导航

    二,程序实例

    1.导入框架依赖包

    2.注册框架核心过滤器,一般工具会有自动生成  web.xml

    <?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">
      <display-name></display-name>    
      <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>

    3.编写action文件  Demo1Action.java

    package com.zhaolong.action;
    
    import com.opensymphony.xwork2.ActionSupport;
    
    public class Demo1Action  extends ActionSupport{
        
        private String message;
    
        public String getMessage() {
            return message;
        }
    
        public void setMessage(String message) {
            this.message = message;
        }
    
        @Override
        public String execute() throws Exception {
            
            this.message="Hello,World!";
            
            return super.execute();
        }
        
        
    }

    4.注册action  struts.xml

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" 
      "http://struts.apache.org/dtds/struts-2.1.dtd"
    > <struts> <!-- 允许OGNL表达式,访问静态方法和属性 --> <constant name="struts.ognl.allowStaticMethodAccess" value="true"/> <package name="tt" namespace="/" extends="struts-default"> <action name="*_*" class="com.zhaolong.action.{1}Action" method="{2}"> <result name="success">/{1}.jsp</result> </action> </package> </struts>

      

  • 相关阅读:
    Request和Session的生命周期
    了解EBP指针
    esp跟ebp跟踪记录
    深入浅出Node.js (6)
    洛谷 P2404 自然数的拆分问题
    洛谷 P1852 奇怪的字符串
    洛谷 P1433 吃奶酪
    洛谷 P1881 绳子对折
    洛谷 P1162 填涂颜色
    P1145 约瑟夫
  • 原文地址:https://www.cnblogs.com/hackxiyu/p/7074023.html
Copyright © 2011-2022 走看看