Struts2学习—你好,Nice to meet you !

初识Struts2

一、struts2介绍

Apache Struts2是流行和成熟的基于MVC设计模式的Web应用程序框架。它成功地结合了WebWork和Struts1.x两种Web框架。使用OGNL表达式和Struts2标签来解决应

用程序数据。通过Struts2可以减少使用MVC模式开发Web应用程序时间。


二、第一个struts2程序

1.下载struts2相关jar包.


2.struts2引入最少包,如下(若缺少会报错)




3.实现Action类

package com.ittx.struts.action;

import org.apache.log4j.Logger;

import com.opensymphony.xwork2.ActionSupport;

public class UserAction extends ActionSupport{
	private static Logger log = Logger.getLogger(UserAction.class);
	
	@Override
	public String execute() throws Exception {
		
		
		/*  
		 *  接收请求
		 *  执行业务处理
		 *  返回响应
		 */
		
		log.debug("执行添加用户操作 >>>>>>");
		return SUCCESS;
	}

}

4.核心配置文件struts.xml

<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
    "http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
	<!-- 开发模式 -->
	<constant name="struts.devMode" value="true" />
	<!-- 所有匹配*.do的请求都由struts2处理 -->
<!-- 	<constant name="struts.action.extension" value="do" /> -->
	<!-- 让struts2支持动态方法调用<default-action-ref name="index"/> -->
	<!-- <constant name="struts.enable.DynamicMethodInvocation" value="true"/> -->

	<!-- package提供了将多个Action组织为一个模块的方式, package的名字必须是唯一的. namespace:定义package命名空间 
		该命名空间影响到url的地址, 例如此命名空间为/test那么访问是的地址为http://localhost:8080/struts2/test/XX.action -->
	<package name="student" namespace="/" extends="struts-default">

		<action name="user" class="com.ittx.struts.action.UserAction">
			<result name="success">/success.jsp</result>
		</action>

	</package>
</struts>

5.配置web.xml

<?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_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>Struts</display-name>
  
  	<filter>
		<filter-name>struts2</filter-name>
		  <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
	</filter>

	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping> 
  
  	<welcome-file-list>
    	<welcome-file>success.jsp</welcome-file>
  	</welcome-file-list>
</web-app>

6. 执行请求

运行Tomcat服务器:







三、struts2工作原理





1.体系结构:应用流程注解

一个请求在Struts2框架中的处理大概分为以下几个步骤(可查看源码:点击打开链接https://github.com/apache/struts):

1. 客户端初始化一个指向Servlet容器(例如Tomcat)的请求
2. 这个请求经过一系列的过滤器(Filter)(这些过滤器中有一个叫做ActionContextCleanUp的可选过滤器,这个过滤器对于Struts2和其他框架的集成很有帮助,例如:SiteMesh Plugin) 
3.  接着StrutsPrepareAndExecuteFilter被调用,StrutsPrepareAndExecuteFilter询问ActionMapper来决定这个请是否需要调用某个Action 
4.  如果ActionMapper决定需要调用某个Action,StrutsPrepareAndExecuteFilter把请求的处理交给ActionProxy 
5.  ActionProxy通过Configuration Manager询问框架的配置文件struts.xml,找到需要调用的Action类 
6.  ActionProxy创建一个ActionInvocation的实例。
7.  ActionInvocation实例使用命名模式来调用,在调用Action的过程前后,涉及到相关拦截器(Intercepter)的调用。 
8.  一旦Action执行完毕,ActionInvocation负责根据struts.xml中的配置找到对应的返回结果。返回结果通常是(但不总是,也可能是另外的一个Action链)一个需要被表示的JSP或者FreeMarker的模版。在表示的过程中可以使用Struts2 框架中继承的标签。在这个过程中需要涉及到ActionMapper




   
   







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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值