zoukankan      html  css  js  c++  java
  • JAVA框架Struts2

    一、介绍:

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

    属于WEB层的。

    web层框架特点:

    1、都是一个特点,前端控制器模式。

    2、记住:前端控制器(核心控制器)。

    3、Struts2框架前端控制器就是过滤器(filter)。

    二、入门

    下载:

    https://struts.apache.org/download.cgi#struts2516

    包结构:

    其中apps结构:

    拷贝这个jar重命名为.zip ,war文件和zip文件格式是一致的。

    里面有Struts2的最少jar包引用。

    拷贝里面的jar包。

    过滤器的注册:

    需要注册的filter:

    1     <filter>
    2         <filter-name>StrutsPrepareAndExecuteFilter</filter-name>
    3         <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    4     </filter>
    5     <filter-mapping>
    6         <filter-name>StrutsPrepareAndExecuteFilter</filter-name>
    7         <url-pattern>/*</url-pattern>
    8     </filter-mapping>

    需要注意的:Struts2url后缀一般带有action的结尾的。表示使用的是Struts2.

    配置文件配置:.

    拷贝Struts2的配置文件,需要放在src下面。在之前的空白内的WEB-INF下面。有相应的配置,去掉其他配置只留package标签。

    添加如下内容:

     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 
     6 <struts>
     7     
     8     <package name="" namespace="/" extends="struts-default">
     9         <action name="hello" class="jd.com.action.SayHello" method="helloSttust"/>
    10 
    11 
    12 
    13     </package>
    14 </struts>

     

    一代表:你跳转的url的名称,开头不需要带/

    二代表:action类(Action类是动作类,是Struts2处理请求,封装数据,响应页面的核心控制器。需要自己编写),也就说,请求进行之后,调用那个类。

    三代表:调用action类中那个方法。

    Action类,方法签名要求:

    1、方法必须是public修饰。

    2、方法返回必须是String。

    3、方法名字可以是任意,但是不能有参数列表。

     简单action类:

     1 package jd.com.action;
     2 
     3 public class SayHello {
     4     /**
     5      * Action类的方法签名有要求:
     6      * 1:方法必须是public。
     7      * 2:方法返回值必须是String字符串。
     8      *3:方法名字是任意但是不能有列表参数。
     9      */
    10     public String  helloSttust(){
    11         System.out.println("SayHello!");
    12         return null;
    13     }
    14 }

    在Struts2中,都是由action类来处理用户请求。

  • 相关阅读:
    Longest Substring Without Repeating Characters
    Valid Palindrome
    Longest Common Prefix
    模型压缩之网络剪枝(Network Pruning)篇
    不使用现有检测框架实现一个简单的物体检测网络
    Mac 上传本地项目到GitHub
    Ubuntu 解决Unable to acquire the dpkg frontend lock (/var/lib/dpkg/lock-frontend)
    github clone 速度慢解决方法
    bibitem格式参考文献批量生成
    python matplotlib给图中的点加标签
  • 原文地址:https://www.cnblogs.com/evilliu/p/8776814.html
Copyright © 2011-2022 走看看