Struts2_Day01

struts2

Struts的初步配置

  1. 创建一个动态WBE网站

  2. 将下面的jar包导入到WebContent——>WEB-INF——>lib下

enter description here

  1. 将struts.xml添加到src下

enter description here

  1. 在web.xml添加以下内容,(注册一个过滤器)

<!-- 配置Struts2的过滤器【拦截所有请求】 -->
    <filter>
		<filter-name>struts2</filter-name>
		<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
	</filter>
	<!-- struts2过滤器拦截请求的规则 -->
	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
  1. 配置struts.xml文件

enter description here

  1. 在浏览器状态栏输入localhost:8080/Day0_1Struts2_Demo01/hello 输出以下内容

enter description here

Struts2执行流程(面试题)

enter description here

  1. Tomcat启动时加载web.xml,读取到过滤器

  2. 实例化并且初始化过滤器

  3. 根据过滤器里面的配置加载struts.xml

  4. 当用户访问hello页面时,

  5. 请求到达过滤器

  6. 过滤器进行拦截,获取到动作名hello,从struts.xml里面寻找action标签里面name为hello的

  7. 找到name为hello,实例化class里面对应的类

  8. 在实例化的类里面寻找method对应的方法名,方法名有返回值

  9. 根据返回值去result标签里面寻找name等于返回值的列,找到对应的结果视图

  10. 将结果视图返回

  11. 相应浏览器,展示结果视图

struts2架构图讲解

enter description here

Struts2配置修改访问后缀

1. 方法一
  1. 在struts2.xml里面添加以下内容
	<!-- 定义一个常量 -->
	<!-- 1_将文件访问后缀修改为lld -->
	<constant name="struts.action.extension" value="lld"></constant>

enter description here

2. 方法二
  1. 在src下创建struts.properties
  2. 添加struts.action.extension=lld
  • 注意:这样做会覆盖struts2里面的相关配置
3.方法三

enter description here

  • 注意:配置的方法会按照以下顺序生效,最终后面配置的会把前面配置的进行覆盖,如果三种方法都进行了配置,最后只会有web.xml里面的生效

enter description here
enter description here

配置struts2的开发模式

  • struts2的开发模式在default.properties里面默认是关闭的

enter description here

  • 现在我们要在struts2.xml里面进行配置,将默认配置覆盖
	<!-- 2_配置struts2为开发模式 -->
	<constant name="struts.devMode" value="true"></constant>

enter description here

  • 预先定义好错误

enter description here

  • 在启动服务器,在网页查看报错信息

enter description here

Struts2中package包讲解

  • 在struts2的配置文件中引入了面向对象思想,使用了分包管理。易于管理动作类。便于模块化开发动作类。
  1. name属性:必须写,并且唯一
  2. extends属性:一般情况下需要继承struts-default包,但不是必须的,如果不进行继承,将无法使用struts2提供的核心功能;struts-default中定义着struts-default这个包
  3. abstract属性:参数(true/false)用于将包声明为抽象包,抽象包就是用来继承的,只要里面元素,就可以生命为抽象包
  4. namespace属性:命名空间。【访问路径=名称空间+动作名称】,一般用于划分项目结构;namespace在使用时要在命名空间前加/斜杠

Action动作类的三种写法

1. action的属性
  • name:动作名称
  • class:动作类全名。
  • method:动作类中的方法名称。默认是public String execute(){}
    方法的要求:
    1. public的
    2. 返回值必须是String
    3. 没有参数
2. 动作类写法一
  • 直接写一个action类,不需要继承任何的类,然后在struts.xml里面进行配置

enter description here

3. 动作类写法二
  • 写个Action类实现Action接口,然后实现excute方法

enter description here

enter description here

4. 动作类写法三
  • 写个Action类继承ActionSuport类【推荐使用这种方式】

enter description here

动作访问之通配符

1. 单个通配符
<!-- <package name="p1" extends="struts-default" namespace="/student">
	
		<action name="add" class="web.lld.Action.Student" method="add">
			<result name="success">/add.jsp</result>
		</action >
		
		<action name="del" class="web.lld.Action.Student" method="del">
			<result name="success">/del.jsp</result>
		</action>
		
	</package> -->
	
	<!-- 下面使用*通配符进行优化 -->
	<package name="p1" extends="struts-default" namespace="/student">
	
		<action name="*" class="web.lld.Action.Student" method="{1}">
			<result name="success">/{1}.jsp</result>
		</action >
		
		<action name="*" class="web.lld.Action.Student" method="{1}">
			<result name="success">/{1}.jsp</result>
		</action>
		
	</package>

enter description here

2. 多个通配符

enter description here

配置访问后缀

enter description here

enter description here

动态方法调用

在这里插入图片描述

result元素

1. 作用
  • 为动作指定结果视图
2. 属性
  • name:逻辑视图的名称,对应着动作方法的返回值。默认值是success
  • type:结果类型,指的是以什么方式转发到定义的页面,默认是dispatcher

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

3.param子元素
  • 转发
<result name="success" type="chain">
	<param name="namespace">要转发到的命名空间</param>
	<param name="actionName">action的name</param>
</result>
  • 重定向
<result name="success" type="redirectAction">
	<param name="namespace">要重定向到的命名空间</param>
	<param name="actionName">action的name</param>
</result>

自定义结果类型

  • 结果类型其实就是一个类,这些类都实现com.opensymphony.xwork2.Result接口。

  • 或者继承自该接口的实现类org.apache.struts2.dispatcher.StrutsResultSupport。

  • 这些类都有一个doExecute方法,用于执行结果视图。【查看源码各种结果类型的类结构】

  • 综上所述,自己实现一个结果视图

  • 例子:输出验证码图像的结果类型
    在这里插入图片描述
    在这里插入图片描述

  • java部分代码(要导入关于验证码的jar包)

package web.lld.result;

import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.dispatcher.StrutsResultSupport;
import com.opensymphony.xwork2.ActionInvocation;
import cn.dsna.util.images.ValidateCode;

/**
 * 验证码的结果类型
 * @author Administrator
 * 继承StrutsResultSupport
 */
public class CAPTCHAresult extends StrutsResultSupport{
	//实现doExecute方法
	@Override
	protected void doExecute(String arg0, ActionInvocation arg1) throws Exception {
		//生成验证码
		/*
		 * ValidateCode验证码生成工具类
		 * 1_宽度
		 * 2_高度
		 * 3_字符个数
		 * 4_干扰线条个数
		 */
		ValidateCode code = new ValidateCode(200, 30, 4, 6);
		//获取response对象
		HttpServletResponse response = ServletActionContext.getResponse();
		//把图片返回给客户端
		code.write(response.getOutputStream());
	}
}

  • struts.xml配置
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
	"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
	<package name="p1" extends="struts-default" namespace="/n1">
		<!-- 声明一个结果类型 -->
		<result-types>
			<result-type name="captcha" class="web.lld.result.CAPTCHAresult"></result-type>
		</result-types>
		
		<!-- 配置action -->
		<action name="checkcode">
			<result type="captcha"></result>
		</action>
	</package>
	
</struts>

优化自定义结果类型

  • java代码
package web.lld.result;

import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.dispatcher.StrutsResultSupport;
import com.opensymphony.xwork2.ActionInvocation;
import cn.dsna.util.images.ValidateCode;

/**
 * 验证码的结果类型
 * @author Administrator
 * 继承StrutsResultSupport
 */
public class CAPTCHAresult extends StrutsResultSupport{
	
	//定义长
	private int width;
	//定义宽
	private int height;
	
	
	
	public int getWidth() {
		return width;
	}



	public void setWidth(int width) {
		this.width = width;
	}



	public int getHeight() {
		return height;
	}



	public void setHeight(int height) {
		this.height = height;
	}



	//实现doExecute方法
	@Override
	protected void doExecute(String arg0, ActionInvocation arg1) throws Exception {
		//生成验证码
		/*
		 * ValidateCode验证码生成工具类
		 * 1_宽度
		 * 2_高度
		 * 3_字符个数
		 * 4_干扰线条个数
		 */
		ValidateCode code = new ValidateCode(width, height, 4, 6);
		//获取response对象
		HttpServletResponse response = ServletActionContext.getResponse();
		//把图片返回给客户端
		code.write(response.getOutputStream());
	}
}

  • struts.xml代码
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
	"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
	<package name="p1" extends="struts-default" namespace="/n1">
		<!-- 声明一个结果类型;局部结果视图配置 -->
		<result-types>
			<result-type name="captcha" class="web.lld.result.CAPTCHAresult"></result-type>
		</result-types>
		
		<!-- 配置action -->
		<action name="checkcode">
			<result type="captcha">
				<!-- 会将width和heigth存到方法里面的对应参数上 -->
				<param name="width">150</param>
				<param name="height">50</param>
			</result>
		</action>
	</package>
	
</struts>

局部视图和全局视图

  • 局部视图
    在这里插入图片描述

  • 全局视图

在这里插入图片描述

获取Servlet的api的方式

  1. 第一种:通过ServletActionContext获取 【推荐使用】
package web.lld.Action;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class testAction extends ActionSupport{
	
	public String test() {
		/**
		 * 获取Servlet中API
		 * 1_request
		 * 2_response
		 * 3_session
		 * 4_application
		 */
		
		//方法一;通过ServletActionContext
		//获取request:属于struts2
		HttpServletRequest request = ServletActionContext.getRequest();
		//获取response:属于Tomcat
		HttpServletResponse response = ServletActionContext.getResponse();
		//获取session:属于Tomcat
		HttpSession session = request.getSession();
		//获取servletContext:属于Tomcat
		ServletContext application = ServletActionContext.getServletContext();
		
		System.out.println(request);//org.apache.struts2.dispatcher.StrutsRequestWrapper@6ff48d11
		System.out.println(response);//org.apache.catalina.connector.ResponseFacade@513ee8ce
		System.out.println(session);//org.apache.catalina.session.StandardSessionFacade@1d92eba2
		System.out.println(application);//org.apache.catalina.core.ApplicationContextFacade@30e8248a
		
		return NONE;
		
	}

}

  1. 第二种:第二种:通过注入方式:先让Action实现两个接口
package web.lld.Action;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.util.ServletContextAware;

import com.opensymphony.xwork2.ActionSupport;

public class test2Action extends ActionSupport implements ServletRequestAware,ServletResponseAware,ServletContextAware{
	
	HttpServletRequest request;
	HttpServletResponse response;
	ServletContext application;
	public String test() {
		/**
		 * 获取Servlet中API
		 * 1_request
		 * 2_response
		 * 3_session
		 * 4_application
		 */
		
		//方法二:通过实现接口:ServletRequestAware,ServletResponseAware,ServletContextAware
		//会实现他们的三个set方法,在访问的时候,拦截器会将相应的对象注入进来
		
		System.out.println(request);//org.apache.struts2.dispatcher.StrutsRequestWrapper@6ff48d11
		System.out.println(response);//org.apache.catalina.connector.ResponseFacade@513ee8ce
		System.out.println(application);//org.apache.catalina.core.ApplicationContextFacade@30e8248a
		
		return NONE;
		
	}
	@Override
	public void setServletContext(ServletContext arg0) {
		// TODO Auto-generated method stub
		application = arg0;
	}
	@Override
	public void setServletResponse(HttpServletResponse arg0) {
		// TODO Auto-generated method stub
		response = arg0;
	}
	@Override
	public void setServletRequest(HttpServletRequest arg0) {
		// TODO Auto-generated method stub
		request = arg0;
	}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值