1、struts2入门1.1、

环境搭建 1.1 jar 1.2 web.xml 1.3 struts.xml struts.xml(核心配置文件) 目录结构: Struts.xml Struts.base.xml regex:. Struts.sy.xml /hello.jsp 1.2、Actionu 不需要指定父类(ActionSupport)u 业务方法的定义 public String xxx();//executeu Action是多例模式(注:在spring中的配置中一定要注意) Action用来接收参数 1.3、参数赋值u Action中定义属性,并提供get/set方法 userName, getUserName/setUserName u ModelDriven 返回实体,不能为null,不需要提供get/set方法 u ModelDriven返回实体和Action中属性重名,ModelDriven中优先级更高 注:ognl,ActionContext学完就知道了 案例:Action代码(user1不提供set、get方法):private String uname; private User user1 = new User(); private User user2; public String execute() { System.out.println(“uname:”+uname); System.out.println(“user1:”+user1); System.out.println(“user2:”+user2); return “hello”; } 浏览器输入:http://localhost:8080/Y2_struts_ognl/sy/helloAction?uname=zs结果: 之后 说明modeldriven接口拦截了uname赋值 浏览器输入:http://localhost:8080/Y2_struts_ognl/sy/helloAction?user2.uname=zs结果: 说明struts支持对象导航 1.4、 与J2EE容器交互u 非注入 耦合(ServletActionContext) 解耦(建立使用解耦模式(ActionContext) u 注入 耦合(XxxAware) 解耦 1.5 核心文件配置u include 包含文件 file u package 包 name 包名 extends 继承 namespace 虚拟路径 abstract 通常用来被继承 u action 子控制器 name:helloAction,helloAction_ class 全限定名 method:execute,{1} u result 结果码处理 Name 结果码配置 type:dispatcher|redirect 1.6、动态方法调用 /rs.jsp 注1:动态方法调用,新版本中已禁用,可自行开启或关闭注2:子控制器的访问路径:名称空间+"/"+子控制器名字_xxx+".action" 2、Ognl1、OGNL的全称是Object Graph Navigation Language(对象图导航语言),它是一种强大的表达式语言2、OgnlContext(ongl上下文)其实就是Map (教室、老师、学生)OgnlContext=根对象(1)+非根对象(N)非根对象要通过"#key"访问,根对象可以省略"#key" 注1:context:英文原意上下文,环境/容器 3、根对象和非根对象的理解4、ValueStack 先进后出的数据结构,弹夹 push/pop 使用ValueStack作为根对象,是因为放到值栈中的对象都可视为根对象5、struts2中传递数据可以使用作用域,但更多的是利用ValueStack或ActionContext作用域取值:从小到大(page -> request -> session -> application)ValueStack取值:从上到下 作用域取值: r e s u l t , r e s u l t 是 一 个 k e y , 是 一 个 字 符 串 。 V a l u e S t a c k 取 值 : &lt; s : p r o p e r t y v a l u e = &quot; r e s u l t &quot; / &gt; , r e s u l t 是 一 个 表 达 式 。 6 、 保 证 同 一 请 求 中 只 创 建 一 个 上 下 文 图 解 : a c t i o n C o n t e x t 上 下 文 就 好 比 一 个 大 容 器 其 中 根 对 象 容 器 : v a l u e s t a c k 非 跟 对 象 容 器 : r e q u e s t 、 s e s s i o n 、 a p p l i c a t i o n 、 p a r a m e t e r s V a l u e s t a c k 中 始 终 是 从 上 往 下 去 找 属 性 值 , 找 到 后 就 不 会 向 下 去 找 了 。 N a m e : 小 明 同 学 S a l a r y : 2000 因 为 v s 中 最 上 面 的 是 S t u d e n t 对 象 、 其 次 是 E m p l o y e e 对 象 。 S t u d e n t 对 象 中 没 有 s a l a r y 属 性 , 会 在 e m p l o y e e 对 象 中 去 找 。 p u b l i c S t r i n g e x e c u t e ( ) A c t i o n C o n t e x t c o n t e x t = A c t i o n C o n t e x t . g e t C o n t e x t ( ) ; / / 栈 : 表 示 一 个 先 进 后 出 的 数 据 结 构 V a l u e S t a c k v s = c o n t e x t . g e t V a l u e S t a c k ( ) ; / / p u s h 方 法 把 项 压 入 栈 顶 v s . p u s h ( n e w E m p l o y e e ( &quot; z s &quot; , 22 ) ) ; v s . p u s h ( n e w E m p l o y e e ( &quot; l s &quot; , 22 ) ) ; v s . p u s h ( n e w E m p l o y e e ( &quot; w w &quot; , 22 ) ) ; / / p o p 方 法 移 除 栈 顶 对 象 并 作 为 此 函 数 的 值 返 回 该 对 象 E m p l o y e e e = ( E m p l o y e e ) v s . p o p ( ) ; S y s t e m . o u t . p r i n t l n ( e . g e t N a m e ( ) ) ; e = ( E m p l o y e e ) v s . p o p ( ) ; S y s t e m . o u t . p r i n t l n ( e . g e t N a m e ( ) ) ; e = ( E m p l o y e e ) v s . p o p ( ) ; S y s t e m . o u t . p r i n t l n ( e . g e t N a m e ( ) ) ; v s . p u s h ( n e w E m p l o y e e ( &quot; 张 雇 员 &quot; , 2000 ) ) ; / / 1 v s . p u s h ( n e w S t u d e n t ( &quot; 小 明 同 学 &quot; , &quot; s 001 &quot; ) ) ; / / 0 S y s t e m . o u t . p r i n t l n ( v s . f i n d V a l u e ( &quot; n a m e &quot; ) ) ; S y s t e m . o u t . p r i n t l n ( v s . f i n d V a l u e ( &quot; s a l a r y &quot; ) ) ; r e t u r n n u l l ; 一 次 请 求 只 生 成 一 个 a c t i o n c o n t e x t 对 象 &lt; {result },result 是一个key,是一个字符串。ValueStack取值:&lt;s:property value=&quot;result&quot;/&gt;,result 是一个表达式。6、保证同一请求中只创建一个上下文 图解:actionContext上下文就好比一个大容器其中根对象容器:valuestack非跟对象容器:request、session、application、parameters Valuestack中始终是从上往下去找属性值,找到后就不会向下去找了。Name:小明同学Salary:2000因为vs中最上面的是Student对象、其次是Employee对象。Student对象中没有salary属性,会在employee对象中去找。 public String execute() { ActionContext context = ActionContext.getContext(); // 栈:表示一个先进后出的数据结构 ValueStack vs = context.getValueStack(); // push方法把项压入栈顶 vs.push(new Employee(&quot;zs&quot;, 22)); vs.push(new Employee(&quot;ls&quot;, 22)); vs.push(new Employee(&quot;ww&quot;, 22)); // pop方法移除栈顶对象并作为此函数的值返回该对象 Employee e = (Employee) vs.pop(); System.out.println(e.getName()); e = (Employee) vs.pop(); System.out.println(e.getName()); e = (Employee) vs.pop(); System.out.println(e.getName()); vs.push(new Employee(&quot;张雇员&quot;, 2000));// 1 vs.push(new Student(&quot;小明同学&quot;, &quot;s001&quot;));// 0 System.out.println(vs.findValue(&quot;name&quot;)); System.out.println(vs.findValue(&quot;salary&quot;)); return null; } 一次请求只生成一个actioncontext对象 &lt;% ActionContext context = ActionContext.getContext(); System.out.println(context); %&gt; 3、struts标签3.1、通用标签u 数据标签 property set scope=&quot;action&quot;,action=request+actionContext push 修改页面 param &lt;param name=&quot;color&quot;&gt;blue&lt;/param&gt; &lt;param name=&quot;color&quot; value=&quot;blue&quot;/&gt; 注1:它是子标签 注2:url/action date java.text.SimpleDateFormat/DecimalFormat debug url/param/a Action &lt;h1&gt;property标签&lt;/h1&gt; &lt;s:property default=&quot;xxx&quot; value=&quot;result&quot; /&gt; &lt;h1&gt;set标签&lt;/h1&gt; &lt;s:set var=&quot;result&quot; value=&quot;uname&quot; scope=&quot;action&quot; /&gt; action: &lt;s:property value=&quot;result&quot; /&gt; &lt;br&gt; request: resultresultkeyValueStack<s:propertyvalue="result"/>result6actionContextvaluestackrequestsessionapplicationparametersValuestackName:Salary:2000vsStudentEmployeeStudentsalaryemployeepublicStringexecute()ActionContextcontext=ActionContext.getContext();//ValueStackvs=context.getValueStack();//pushvs.push(newEmployee("zs",22));vs.push(newEmployee("ls",22));vs.push(newEmployee("ww",22));//popEmployeee=(Employee)vs.pop();System.out.println(e.getName());e=(Employee)vs.pop();System.out.println(e.getName());e=(Employee)vs.pop();System.out.println(e.getName());vs.push(newEmployee("",2000));//1vs.push(newStudent("","s001"));//0System.out.println(vs.findValue("name"));System.out.println(vs.findValue("salary"));returnnull;actioncontext<{result }

url/param标签

<s:set var=“blue” value=“result” /> <s:url var=“url1” namespace="/sy" action=“tagAction_formSubmit”> <s:param name=“color1” value=“blue”></s:param> <s:param name=“color2”>blue</s:param> </s:url> <s:property value="#url1" />

date标签

<% request.setAttribute(“d”, new Date()); %> <s:date name="#request.d" format=“yyyyMMdd” /> <s:debug />

push标签

<s:property /> <s:push value=“result”> <s:property /> </s:push>
u 控制标签 iterator/if/elseif/else <% request.setAttribute(“score”, new Integer(70)); request.setAttribute(“names”, new String[] { “zs”, “ls”, “ww” }); %>

控制标签

<s:if test="#request.score >= 90"> A </s:if> <s:elseif test="#request.score >= 80"> B </s:elseif> <s:elseif test="#request.score >= 70"> C </s:elseif> <s:elseif test="#request.score >= 60"> D </s:elseif> <s:else> E </s:else>
  • <s:iterator var=“v” value="#request.names">
  • <s:property value="#v" />
  • </s:iterator>
3.2、UI标签u 表单标签 主题:xhtml/simple(自带样式,无法table排列样式) u 非表单标签3.3、标签的属性类型说明String 字符串 Boolean true|false Object Object/String 传过去字符串,但会被认为是一个OGNL表达式进行计算 %{str}:str会被强制转换成OGNL表达式计算 <s:a href="%{#url1}">bbb</s:a>3.4、标签的公共属性var 将值保存到上下文(ActionContext)中的一个keyscope 4+action action=request+actionContext4、struts实现crud1、定义baseAction,存放结果码常量,请求、响应、上下文、公用的传值2、Struts标签的使用 s:iterator S:action S:url S:form s:textfield S:select S:radio S:param s:textarea List.jsp <s:action var=“clzAction” namespace="/sy" name=“clazzAction”/> <s:form namespace="/sy" action=“studentAction_list” > <s:textfield name=“sname” label=“姓名”></s:textfield> <%-- <s:select list="#{1:‘aa’,2:‘bb’,3:‘cc’}" headerKey="" headerValue="= 请选择="></s:select> --%> <s:select label=“班级” name=“cid” list="#clzAction.result" headerKey="" headerValue="= 请选择=" listKey=“cid” listValue=“cname”></s:select> <s:submit value=“查询”></s:submit> </s:form> 新增 新增2 <s:iterator var=“s” value=“result”> </s:iterator>
序号学号姓名拼音性别标记班级操作
序号<s:property value="#s.sid"/><s:property value="#s.sname"/><s:property value="#s.spin"/><s:property value="#s.sex"/><s:property value="#s.mark"/><s:property value="#s.cname"/> <s:url var=“durl” namespace="/sy" action=“studentAction_del”> <s:param name=“sid” value="#s.sid"/> </s:url> <s:a href="%{#durl}">删除</s:a> <s:url var=“eurl” namespace="/sy" action=“studentAction_load”> <s:param name=“sid” value="#s.sid"/> </s:url> <s:a href="%{#eurl}">修改</s:a>
<z:page pageBean=" p a g e B e a n &quot; / &gt; A d d . j s p &lt; h 1 &gt; a d d &lt; / h 1 &gt; &lt; s : a c t i o n v a r = &quot; c a c t i o n &quot; n a m e s p a c e = &quot; / s y &quot; n a m e = &quot; c l a z z A c t i o n &quot; &gt; &lt; / s : a c t i o n &gt; &lt; s : f o r m n a m e s p a c e = &quot; / s y &quot; a c t i o n = &quot; s t u d e n t A c t i o n a d d &quot; &gt; &lt; {pageBean}&quot;/&gt; Add.jsp&lt;h1&gt;add&lt;/h1&gt; &lt;s:action var=&quot;caction&quot; namespace=&quot;/sy&quot; name=&quot;clazzAction&quot;&gt;&lt;/s:action&gt; &lt;s:form namespace=&quot;/sy&quot; action=&quot;studentAction_add&quot;&gt; &lt;%-- &lt;s:hidden name=&quot;sid&quot; value=&quot;5&quot;/&gt; --%&gt; &lt;s:textfield label=&quot;学号&quot; name=&quot;sid&quot; /&gt; &lt;s:textfield label=&quot;姓名&quot; name=&quot;sname&quot; /&gt; &lt;s:radio label=&quot;性别&quot; name=&quot;sex&quot; list=&quot;{&#x27;男&#x27;,&#x27;女&#x27;}&quot;/&gt; &lt;s:select label=&quot;班级&quot; name=&quot;cid&quot; headerKey=&quot;&quot; headerValue=&quot;===请选择===&quot; list=&quot;#caction.result&quot; listKey=&quot;cid&quot; listValue=&quot;cname&quot; cssStyle=&quot;width:160px;&quot; /&gt; &lt;s:textarea label=&quot;备注&quot; name=&quot;mark&quot;&gt;&lt;/s:textarea&gt; &lt;s:submit value=&quot;确定&quot;/&gt; &lt;/s:form&gt; Edit.jsp&lt;h1&gt;edit&lt;/h1&gt; &lt;s:action var=&quot;caction&quot; namespace=&quot;/sy&quot; name=&quot;clazzAction&quot;&gt;&lt;/s:action&gt; &lt;s:push value=&quot;result&quot;&gt; &lt;s:form namespace=&quot;/sy&quot; action=&quot;studentAction_edit&quot;&gt; &lt;s:textfield label=&quot;姓名&quot; name=&quot;sname&quot; /&gt; &lt;s:radio label=&quot;性别&quot; name=&quot;sex&quot; list=&quot;{&#x27;男&#x27;,&#x27;女&#x27;}&quot;/&gt; &lt;s:select label=&quot;班级&quot; name=&quot;cid&quot; headerKey=&quot;&quot; headerValue=&quot;===请选择===&quot; list=&quot;#caction.result&quot; listKey=&quot;cid&quot; listValue=&quot;cname&quot; cssStyle=&quot;width:160px;&quot; /&gt; &lt;s:textarea label=&quot;备注&quot; name=&quot;mark&quot;&gt;&lt;/s:textarea&gt; &lt;s:hidden name=&quot;sid&quot;/&gt; &lt;s:submit value=&quot;确定&quot;/&gt; &lt;/s:form&gt; &lt;/s:push&gt; Baseaction/* * 默认结果码 */ protected static final String SUCCESS = &quot;success&quot;; protected static final String FAIL = &quot;fail&quot;; protected static final String LIST = &quot;list&quot;; protected static final String DETAIL = &quot;detail&quot;; protected static final String EDIT = &quot;edit&quot;; protected static final String ADD = &quot;add&quot;; protected HttpServletResponse resp; protected HttpServletRequest req; protected HttpSession session; protected ServletContext servletContext; /* * 服务端向客户端传值,不需要提供set方法,只需要提供get方法 */ protected Object result;//用于传值 protected Object message;//用于展示处理结果 protected int code;//用于结果码处理 /* * 客户端往服务端传值,需要提供set、get方法 */ private String forward; 5、拦截器与上传下载5.1、struts之拦截器注意:struts2.5之后的新特性,动态调用需要新增配置。&lt;global-allowed-methods&gt;regex:.*&lt;/global-allowed-methods&gt; Jsp页面&lt;form action=&quot; pageBean"/>Add.jsp<h1>add</h1><s:actionvar="caction"namespace="/sy"name="clazzAction"></s:action><s:formnamespace="/sy"action="studentActionadd"><{pageContext.request.contextPath }/sy/userActionQuery.action" method=“post”> Xml配置 /querySuccess.jsp /error.jsp 定义拦截器的类 结果 5.2、struts之上传下载注意:private File file;
private String fileContentType;
private String fileFileName;开头的file与文件传递的name相对应enctype=“multipart/form-data” private String uploadDir = “/upload”;// web项目下的目录,虚拟路径,会跟随服务器的位置而改变 Jsp上传页面 Jsp下载页面<s:url var=“saveAsUrl” namespace="/sy" action=“userFileUpload_saveAs.action”></s:url><s:property value=“saveAsUrl”/><s:a href="%{#saveAsUrl}">直接打开</s:a> <s:url var=“openAsUrl” namespace="/sy" action=“userFileUpload_openAs.action”></s:url><s:property value=“openAsUrl”/> 上传的图片 Xml配置 /querySuccess.jsp Java类 结果

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值