Struts2简单学习记录(未完待续)

struts2是web应用框架,启动需要tomcat

Struts2 MVC架构

  • 模型——属于软件设计模式的底层基础,主要负责数据维护。
  • 视图——这部分是负责向用户呈现全部或部分数据。
  • 控制器——通过软件代码控制模型和视图之间的交互。
Struts2 的模型-视图-控制器模式是通过以下五个核心部分进行实现的:
  • 操作(Actions):Struts2应用程序的关键,通过它实现大部分的业务逻辑
  • 拦截器(Interceptors):
  • 值栈(Value Stack)/OGNL
  • 结果(Result)/结果类型
  • 视图技术
    在这里插入图片描述
在Struts2 中用户的请求生命周期:
  • 用户发送一个资源需求的请求到服务器(例如:页面)。
  • 核心控制器查看请求后确定适当的动作。
  • 使用验证、文件上传等配置拦截器功能。
  • 执行选择的动作来完成请求的操作。
  • 另外,如果需要的话,配置的拦截器可做任何后期处理。
  • 最后,由视图显示结果并返回给用户
配置文件:web.xml(视图,部署描述,过滤器)、struts.xml(连接动作)、struts-config.xml(控制器)以及struts.properties(改变框架的默认行为)。后两种很少用到
  • web.xml
  1. 决定servlet容器的HTTP元素需求如何进行处理
  2. WebContent/WEB-INF文件夹下创建。
  3. 这个文件为每个web应用程序提供接入点。在部署描述符(web.xml)中,Struts2 应用程序的接入点将会定义为一个过滤器。因此我们将在web.xml里定义一个FilterDispatcher类的接入点
 <?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" 
  xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
  http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
  id="WebApp_ID" version="3.0">
  
  <display-name>Struts 2</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.FilterDispatcher
     </filter-class>
  </filter>

  <filter-mapping>
     <filter-name>struts2</filter-name>
     <!-- 将Struts2 过滤器映射到 /* ,而不是 /*.action ,这意味着所有的url都会被Struts过滤器解析-->
     <url-pattern>/*</url-pattern>
  </filter-mapping>

</web-app>
  • struts.xml
  1. 含有随着Actions的开发你将要修改的配置信息
  2. 这个文件可在WEB-INF/classes文件夹下创建
  3. < struts >是根标记元素,在其下,使用< package >标签声明不同的包。 这里的< package >标签允许配置的分离和模块化

< package > 标签具有的属性:

属性描述
name(必需)为package的唯一标识
extends指定package继承另一package的所有配置。通常情况下,我们使用struts-default作为package的基础。
abstract定义package为抽象的。如果标记为true,则package不能被最终用户使用。
namespaceActions的唯一命名空间
<?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>
		<!--<constant>标签以及name和value属性将用于覆盖default.properties中定义的任一属性,就像我们设置的struts.devMode属性一样。
		设置struts.devMode属性开发这模式:热部署,提高错误信息提示--->
   <constant name="struts.devMode" value="true" />
   
	   <!--package用来管理action,和项目中的包没有关系
		name:按照当前action的分类随意命名,不能重复
		namespace:访问路径地址的前缀,跟name没有关系
		extends:要继承struts-default
		abstract:抽象声明,表示当前这个配置文件不能独立运行,等待被继承
		 -->
   <package name="helloworld" extends="struts-default">
   
	     <!--<action>配置
	     	name:给action起的名字,决定访问路径最后定的地址
	     	class:action类的完整路径名
	     	method:访问类中的方法名
	     -->
      <action name="hello" 
	          class="cn.struts2.HelloWorldAction" 
	          method="execute">
	          
	            <!--Results(结果)
	            	name:action类中method返回值
	            	type:dispatcher转发(默认)
	            		  redirect重定向
	            	标签中的值:跳转页面的地址	  
	            -->
            <result name="success">/HelloWorld.jsp</result>
      </action>
      <-- more actions can be listed here -->

   </package>
   <!--struts.xml可以拆分成多个,引入多个xml文件:
	<struts>
	     <include file="my-struts1.xml"/>
	     <include file="my-struts2.xml"/>
	</struts>
	-->

</struts>

动态方法

<action name="userAction_*" class="cn.hd.dynamic.UserAction"method="{1}">
    <result name="success">/hello.html</result>
</action>
<!--{}中的数字代表第几个*-->
<action name="userAction_*_*_*" class="cn.hd.dynamic.{2}Action"method="{1}">
    <result name="success">/{3}hello.html</result>
</action>
  • struts-config.xml
    1. 是Web Client中View和Model组件之间的链接
    2. 大部分项目里不必使用这些设置
拦截器说明
struts-config这是配置文件的根节点。
form-beans这是你将ActionForm子类映射到name的位置,你可以在struts-config.xml文件的其余部分,甚至在JSP页面上,将这个name用作ActionForm的别名
global forwards此部分将你在webapp上的页面映射到name,你可以使用这个name来引用实际页面。这避免了对你网页上的URL进行硬编码。
action-mappings这是你声明表单处理程序的地方,也被称为操作映射(action mappings)
controller这部分是配置Struts的内部,在实际情况中很少使用。
plug-in这部分告诉Struts在哪里找到属性文件,它包含提示和错误消息。
  • struts.properties文件
    • 提供了一种机制来改变框架的默认行为
    • 所有属性也可以在web.xml中配置使用init-param,以及在struts.xml配置文件中使用constant标签
### When set to true, Struts will act much more friendly for developers
struts.devMode = true

### Enables reloading of internationalization files
struts.i18n.reload = true

### Enables reloading of XML configuration files
struts.configuration.xml.reload = true

### Sets the port that the server is run on
struts.url.http.port = 8080
Actions动作
  1. 每个URL映射到特定的action,提供处理来自用户的请求所需要的处理逻辑
  2. 在将数据从请求传递到视图方面起重要作用
  3. 必须协助框架确定那个结果应该呈现在响应请求的视图中

使用execute方法返回处理结果

拦截器
拦截器说明
alias允许参数在请求之间使用不同的别名
checkbox通过为未检查的复选框添加参数值false,以辅助管理复选框
conversionError将字符串转换为参数类型的错误信息放置到action的错误字段中
createSession自动创建HTTP会话(如果尚不存在)
debugging为开发人员提供一些不同的调试屏幕
execAndWait当action在后台执行时,将用户发送到中间的等待页面
exception映射从action到结果抛出的异常,允许通过重定向自动处理异常
fileUpload便于文件上传
i18n在用户会话期间跟踪选定的区域
logger通过输出正在执行的action的名称提供简单的日志记录。
params设置action上的请求参数
prepare这通常用于执行预处理工作,例如设置数据库连接
profile允许记录action的简单分析信息
scope在会话或应用程序范围内存储和检索action的状态
ServletConfig提供可访问各种基于servlet信息的action
timer以action执行时间的形式提供简单的分析信息
token检查action的有效性,以防止重复提交表单
validation提供action的验证支持

自定义拦截器

  1. 创建自定义拦截器,需要扩展的是以下Interceptor接口:
public interface Interceptor extends Serializable{
   void destroy();
   void init();
   String intercept(ActionInvocation invocation) throws Exception;
}
  1. 如果不需要初始化或清理代码,可以扩展AbstractInterceptor类
public class MyInterceptor extends AbstractInterceptor {

   public String intercept(ActionInvocation invocation)throws Exception{

      /* let us do some pre-processing */
      String output = "Pre-Processing"; 
      System.out.println(output);

      /* let us call action or next interceptor */
      /*action将通过拦截器使用invocation.invoke()*/
      String result = invocation.invoke();

      /* let us do some post-processing */
      output = "Post-Processing"; 
      System.out.println(output);

      return result;
   }
}

struts.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>
   <constant name="struts.devMode" value="true" />
   <package name="helloworld" extends="struts-default">

      <interceptors>
      <!--自定义拦截起-->
         <interceptor name="myinterceptor"
            class="cn.demo.struts2.MyInterceptor" />
      </interceptors>

      <action name="hello" 
         class="cn.demo.struts2.HelloWorldAction" 
         method="execute">
         <!--框架提供的拦截器-->
         <interceptor-ref name="params"/>
         <!--自定义拦截器-->
         <interceptor-ref name="myinterceptor" />
         <result name="success">/HelloWorld.jsp</result>
      </action>

   </package>
</struts>
Struts2 值栈/OGNL
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

可能遇到的问题

配置欢迎页跳转报404.
<welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
</welcome-file-list>

因为welcome-file的.list一个一个去检查是否web目录下面存在这个文件,如果存在,继续下面的工作。所以一定要在webapp的根目录下创建一个index.jsp

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值