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 < s : p r o p e r t y v a l u e = " r e s u l t " / > 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 ( " z s " , 22 ) ) ; v s . p u s h ( n e w E m p l o y e e ( " l s " , 22 ) ) ; v s . p u s h ( n e w E m p l o y e e ( " w w " , 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 ( " " , 2000 ) ) ; / / 1 v s . p u s h ( n e w S t u d e n t ( " " , " s 001 " ) ) ; / / 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 ( " n a m e " ) ) ; 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 ( " s a l a r y " ) ) ; r e t u r n n u l l ; a c t i o n c o n t e x t < {result },result 是一个key,是一个字符串。ValueStack取值:<s:property value="result"/>,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("zs", 22)); vs.push(new Employee("ls", 22)); vs.push(new Employee("ww", 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("张雇员", 2000));// 1 vs.push(new Student("小明同学", "s001"));// 0 System.out.println(vs.findValue("name")); System.out.println(vs.findValue("salary")); return null; } 一次请求只生成一个actioncontext对象 <% ActionContext context = ActionContext.getContext(); System.out.println(context); %> 3、struts标签3.1、通用标签u 数据标签 property set scope="action",action=request+actionContext push 修改页面 param <param name="color">blue</param> <param name="color" value="blue"/> 注1:它是子标签 注2:url/action date java.text.SimpleDateFormat/DecimalFormat debug url/param/a Action <h1>property标签</h1> <s:property default="xxx" value="result" /> <h1>set标签</h1> <s:set var="result" value="uname" scope="action" /> action: <s:property value="result" /> <br> request: {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; {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类 结果

猜你喜欢

转载自blog.csdn.net/t1136237940/article/details/83097003