关键词:
Struts作为一款Web框架自然少不了与页面的交互,开发过程中我们最常用的request、application、session等struts都为我们进行了一定的封装与处理
一、通过ActionContext获取
方法 | 说明 |
void put(String key,Object value) | 模拟HttpServletRequest中的setAttribute() |
Object get(Object key) | 通过参数key查找当前ActionContext中的值 |
Map getApplication() | 返回一个Application级的Map对象 |
static ActionContext getContext() | 获得当前线程的ActionContext对象 |
Map getParameters() | 返回一个Map类型HttpSession对象 |
void put(Object key,Object vlue) | 向前ActionContext对象中存入名值对信息 |
void setApplication(Map application) | 设置Application上下文 |
void setSession(Map session) | 设置一个Map类型的Session对象 |
Map setSession() |
返回一个Map类型的HttpSession对象 |
1.配置web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <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> </web-app>
2.配置struts.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN" "http://struts.apache.org/dtds/struts-2.5.dtd"> <struts> <!--struts中对应mvc的三层是,filterdispatcher->控制层、Action->模型层、Result->视图层--> <package name="test" extends="struts-default"> <!--设置允许动态调用的方法--> <global-allowed-methods>user</global-allowed-methods> <action name="user" class="test.TestAction"> <!--result顾名思义返回结果,name属性与Action中返回的字符串一样,作用就是返回视图--> <result name="success">/success.jsp</result> <result name="user">/user.jsp</result> </action> </package> <!--设置允许动态调用,除了execute()方法之外的其他自定义方法--> <constant name="struts.enable.DynamicMethodInvocation" value="true"></constant> <constant name="struts.i18n.encoding" value="UTF-8"></constant> </struts>
3.在test包下写TestAction类
package test; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; public class TestAction extends ActionSupport private static final long serialVersionUID = 1L; //我们也可以通过实现特定接口来获取对应的对象比如ServletRequestAware可以获取到HttpServletRequest //的实例,但是我们需要手动定义该对象,通过接口中的重写的方法来初始化 public String execute() return "success"; public String user() // struts通过Action访问servlet中的request、session、application等 ActionContext context = ActionContext.getContext(); context.put("name","request");//默认是request context.getSession().put("name","session");//session context.getApplication().put("name","application");//application return "user";
4.准备页面index.jsp、user.jsp
1)index.jsp
<%-- Created by IntelliJ IDEA. po: zgye Date: 2019/5/11 Time: 12:27 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>$Title$</title> </head> <body> <a href="user">user</a> <a href="user!user.action">select</a> </body> </html>
2)user.jsp
<%-- Created by IntelliJ IDEA. po: zgye Date: 2019/5/11 Time: 13:18 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>用户页面</title> </head> <body> 测试内置对象: $requestScope.name $sessionScope.name $applicationScope.name </body> </html>
5.启动服务点击select超链接测试
二、通过特定接口访问
ActionContext虽然可以间接的访问Servlet API,同时我们有时候也需要直接去访问Servlet API,Struts2为我们提供了对应的接口
1)ServletRequestAware
2)ServletResponseAware
3)ServletContextAware
·······
1.准备web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <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> </web-app>
2.配置struts,xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN" "http://struts.apache.org/dtds/struts-2.5.dtd"> <struts> <!--struts中对应mvc的三层是,filterdispatcher->控制层、Action->模型层、Result->视图层--> <package name="test" extends="struts-default"> <!--接口方式测试Servlet API--> <action name="servlet" class="test.TestServletAction"> <result name="servlet">/servlet.jsp</result> </action> </package> </struts>
3.准备TestServletAction.ava
package test; import com.opensymphony.xwork2.ActionSupport; import org.apache.struts2.interceptor.ServletRequestAware; import org.apache.struts2.interceptor.ServletResponseAware; import org.apache.struts2.util.ServletContextAware; import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class TestServletAction extends ActionSupport implements ServletRequestAware, ServletResponseAware, ServletContextAware private HttpServletRequest request; private HttpServletResponse response; private ServletContext context; @Override public void setServletRequest(javax.servlet.http.HttpServletRequest httpServletRequest) request=httpServletRequest; @Override public void setServletResponse(HttpServletResponse httpServletResponse) response=httpServletResponse; @Override public void setServletContext(ServletContext servletContext) context=servletContext; @Override public String execute() throws Exception request.setAttribute("name","request"); request.setAttribute("response",response); context.setAttribute("name","context"); return "servlet";
4.准备servlet.jsp页面
<%-- Created by IntelliJ IDEA. User: zgye Date: 2019/5/12 Time: 12:27 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>servlet测试页面</title> </head> <body> request:$requestScope.name<br> response:$requestScope.response<br> context:$applicationScope.name </body> </html>
5.启动服务请求http://localhost:8080/servlet查看测试结果
struts2学习第七课动态方法调用
动态方法调用:通过url动态调用Action中的方法。action声明:<packagename="struts-app2"namespace="/"extends="struts-default"><actionname="Product"class="org.simpleit.app.Product"></package>URI:--/struts-app2 查看详情
struts2学习第七课通配符映射
一个WEB应用可能有长百上千个action声明,可以利用struts提供的通配符映射机制吧多个彼此相识的映射关系简化为一个映射关系。通配符映射规则:--若找到多个匹配,没有通配符的那个将胜出(精确匹配)--若指定的动作不存在,str... 查看详情
struts2学习—拦截器之登录权限
大部分Action共享常见的关注点.一些Action需要输入验证.另外一些Action可能需要预处理文件上传.还有一些Action可能需要防止重复提交.许多Action需要在页面显示前生成下拉列表和其他控件.框架使用“拦截器”策略使得解决共享这些... 查看详情
struts学习
1.复习搭建Struts2的开发环境:3个步骤 2.actionVSAction类1).action:代表一个Struts2的请求。2).Action类:能够处理Struts2请求的类 >属性的名字必须遵守与JavaBeans属性名相同的命名规则. >属性的类型可以是任意类型. &n... 查看详情
git第二天:git安装与常用命令
↑↑Java语法基础—>小型项目练习—>MySQL更多学习内容均更新在专栏了,记得关注专栏哦↑↑ 查看详情
struts2学习小结
1基础使用:导入jar包,配置web.xml,并引入struts.xml文件DMI:动态方法调用,调用时使用!分隔action名与方法名,如index!add.action,可以进行快捷测试<!--需要修改配置文件,默认为false,需要修改为true--><constantname="struts.e... 查看详情
mybatis学习第二天——mapper的动态代理
传统的Dao层开发通过接口与实现类的方式,Mybatis中通过mapper动态代理是需要定义接口。1.传统Dao层封装那么可以将公共资源提取出来,剩余的封装成方法来实现。下面是UserDaoImpl.java中对查询的简单封装1packagecom.mybatis.dao.impl;23impo... 查看详情
[git]继续学习的第二天
进一步认识git分支操作。 首先是HEAD引用。一般HEAD引用指向的是当前分支,但有时候需要从某一历史版本开始新的分支,这时候就需要移动HEAD引用,命令与移动分支节点一样是gitchecko... 查看详情
记录学习第二天---系统基础
javawebday02---android小白的第二天学习笔记
CSS(美工部分知识,了解)1、CSS概述1.1、CSS是什么?*CSS指层叠样式表样式表:存储样式的地方层叠:一层一层叠加高大富有帅气人1.2、CSS有什么作用?*CSS就是用来更加方便修饰HTML标签(元素)1.3、CSS代码规范选择器名称{属性... 查看详情
javascript学习总结第二天
javascript 学习总结第二天函数和对象 对象 声明方式 newObject()   查看详情
记录学习第二天---系统基础
小知识:番茄工作法番茄工作法是简单易行的时间管理方法,是由弗朗西斯科·西里洛于1992年创立的一种相对于GTD更微观的时间管理方法。使用番茄工作法,选择一个待完成的任务,将番茄时间设为25分钟,专注工作,中途不允... 查看详情
struts2学习记录
一、interceptor拦截器的使用第一种情况(指定action使用该拦截器):struts.xml文件的配置:<interceptors> <interceptorname="myinterceptor"class="loginInterceptor"/> <interceptor-stackname="loginStack"> <int 查看详情
javascript学习之路——数组
Array类型恐怕是javascript中最常用的类型了。而且,javascript中的数组与其他多数语言中的数组有着相当大的区别。虽然 javascript数组与其他语言中的数组都是数据的有序列表,但与其他语言不同的是,javascript数组的每一项... 查看详情
alpha冲刺第二天
...立式会议项目进展团队成员在确定了所需技术之后,开始学习相关技术的使用,其中包括了HTML5,CSS与SSH框架等开发技术。并且在项目分工配合加以总结和完善,对现有发现的关于系统设计方面的不足进行了修补,并搭建完成开... 查看详情
html学习第二天
学习内容:1.向页面标题添加图标,在head标签中添加:<linkrel="shortcuticon"href="图标地址">1<head>2<metacharset="utf-8">3<title>添加图标</title>4<linkrel="shortcuticon"href="logo.ico">5</head&g 查看详情
学习第二天笔记
第二天 认识、配置IOS IOS,网络设备的操作系统; Internetoperatingsystem,因特网操作系统;(www.cisco.com->思科官网,下载IOS;) 一.交换机组成: 1.软件-系统文件-IOS & 查看详情
spark寒假自学第二天
...h、Hadoop已经安装完毕,Scala,spark还未安装然后进行java的学习,在以前的学习中java已经学习了一部分,这次还需要继续进行相应的学习。Hadoop学习:HDFS,MR计算框架,在大三上学期已经初步接触了这方面的内容。在学习中了解... 查看详情