Struts2学习笔记

来源:互联网 发布:饥荒左上角数据怎么关 编辑:程序博客网 时间:2024/05/24 00:29

1Struts接收数据

属性驱动

模型驱动

struts动态方法调用

 

2、请求范围

3、页面访问ValueStack

4OGNL表达式

获取成员变量

与调用成员方法

访问Request中的数据

访问集合中的数据

访问StackContext中的数据

页面或者Action请求转发时,ValueStack是共享的。重定向不能共享

 

5、Struts2标签

表单标签

非表单标签

控制标签

文件上传和下载标签

 

6、拦截器Interceptor

实现Interceptor接口

继承AbstractInterceptor抽象类

Struts接收数据

 

1.使用属性驱动

    post提交的时候页面中的name=bean.bean中的属性

2.使用模型驱动

    post提交数据时在action类中实现ModelDriven<bean>接口,只需使得name=bean中的属性

3.struts动态方法调用 

    a.使用struts.xmlactionmethod的配置属性,但是在actionname中必须使用各自的调用名称进行调用,否则只能调用第一个方法

    b.!”来调用方法。action只需要写一个在页面中的name属性名+!+方法名。但要配置

<constant name="struts.enable.DynamicMethodInvocation" value="true"/> 开启动态调用方法功能

c.使用通配符方式调用,在actionname中必须使用名称+"_*"(例:userAction_*) method=“{1}”表示匹配第1*中的方法1代表*中的值,只要在页面中使用各自的userAction_add,userAcion_edit等方法就能进行调用

 

请求范围

ActionContext context = ActionContext.getContext();

 

Map<String, Object> sessionMap = context.getSession();

 

Map<String, Object> servletContextMap=context.getApplication();

 

Map<String, Object> requestMap = 

(Map<String, Object>) context.get("request");

 

页面访问ValueStack

this.request.setAttribute("req_key""Request作用域");

this.session.setAttribute("session_key""Session作用域");

this.servletContext.setAttribute("app_key"new Date());

Struts中的ValueStack

1:导入Struts的标签

2:调用<s:debug></s:debug>

访问ValueStack中的数据

Strust表达式:<s:property value="a_id"/>

<s:property value="userBean.id"/>

<s:property value="userBean.username"/>

访问StackContext中的数据

Strutrs表达式:

<s:property value="#request.req_key"/>

<s:property value="#parameters.param_one"/>

<s:property value="#session.session_key"/>

 

OGNL表达式

1:获取成员变量

userid = <s:property value="userid"/>

Userbean_id = <s:property value="userBean.id"/>

修改变量值:<s:property value="userBean.setId(300)"/>

Userbean_id = <s:property value="userBean.id"/>

2:与调用成员方法

两数相加 = <s:property value="add(1,3)"/>

两数相减 = <s:property value="subtract(10,3)"/>

字符串截取:<s:property value="userBean.username.length()"/>  <s:property value="userBean.username.substring(2)"/>

2:获取静态变量与静态方法(开启struts.ognl.allowStaticMethodAccess常量)

变量:<s:property value="@com.util.Common@PI"/>

方法:<s:property value="@com.util.Common@getDate()"/>

3:访问Request中的数据

username = <s:property value="username"/>

username = <s:property value="#parameters.username"/>

4:访问集合中的数据

Remove = <s:property value="strList.remove('bb')"/>

Size = <s:property value="strList.size()"/>

strList = <s:property value="strList"/>

userList = <s:property value="userList"/>

用户的信息 <s:property value="userList.get(1).id"/> 

 <s:property value="userList.get(1).getUsername()"/>

  

userMap = <s:property value="userMap"/>

取对应的值:<s:property value="userMap.key_1"/>

  <s:property value="userMap.get('key_1')"/>

  <s:property value="userMap['key_1']"/>

 

取key_5的值:=<s:property value="userMap.key_5.username"/>

5:取作用域中的数据

<s:property value="#request.req_key"/>

<s:property value="#request.req_2"/>

<s:property value="#session.session_key"/>

<s:property value="#session.session_2"/>

6:操作ValueStack

dog_name = <s:property value="dog_name"/>

<!-- dog和user对象中都包含username变量时需要指定valueStack下标 -->

username= <s:property value="username"/>

  <s:property value="[0].username"/> 

userAction中的username = <s:property value="[1].username"/>

abc = <s:property value="#abc"/>

7:页面或者Action请求转发时,ValueStack是共享的。重定向不能共享

<%-- 

<c:redirect url="/ognl_page_2.jsp"></c:redirect>

<jsp:forward page="/ognl_page_2.jsp"></jsp:forward>

--%>

Struts2标签

去掉Struts标签自动产生的多余标记.配置常量struts.ui.theme

1.表单标签

文本框:

<s:textfield name="tagBean.userid"/>

静态_单选框:

<s:radio list="{'男','女','未知'}" name="tagBean.usersex">

<s:radio list="#{1:'男',0:'女',2:'未知'}" name="tagBean.usersex_2"/>

动态_单选框:

<s:radio list="#userList" name="tagBean.usersex_3" 

listKey="type_id" listValue="type_name"/>

下拉框_静态: 

<s:select list="{'选项1','选项2','选项3'}" name="tagBean.sex_1/>

下拉框_动态:

<s:select list="#dogList" name="tagBean.select_2" listKey="dog_id" listValue="dog_name"/>

复选框:

<s:checkbox name="tagBean.is_marry"></s:checkbox>

复选框组:

<s:checkboxlist list="#typeList" listKey="type_id" 

listValue="type_name" name="tagBean.userLike"/>

2.非表单标签

不可以放在s:form标签中

上下移动框:

<s:updownselect list="#dogList" name="tagBean.updown_1" 

   listKey="dog_id" listValue="dog_name" 

   allowMoveDown="true" 

   moveDownLabel="向下移动" 

moveUpLabel="向上移动" 

selectAllLabel="全部选中"

cssStyle="width:300px;height:200px;">

</s:updownselect>

 

传输下拉框:

<s:optiontransferselect list="#dogList" name="tagBean.tran_1"

listKey="dog_id" listValue="dog_name"

cssStyle="width:300px;"

doubleCssStyle="width:300px;"

allowUpDownOnLeft="false"

 allowUpDownOnRight="false"

doubleList="#typeList"  doubleName="tagBean.tran_2"

doubleListKey="type_id" doubleListValue="type_name"

addToLeftLabel="向左移动" addToRightLabel="向右移动" >

</s:optiontransferselect>

3.控制标签

1:判断标签

2:迭代标签

3:合并与追加标签

4:截取集合

4.文件上传和下载标签

A:加入commons-fileupload-1.2.2.jar,commons-io-2.0.1.jar

B:表单必须要以流的方式提交。enctype=”multipart/form-data

/**

 * 将文件保存到文件夹,表中保存文件的名称与文件的路径

 * @return

 * @throws Exception

 */

public String SaveToFile() throws Exception {

 

System.out.println(this.username);

System.out.println(this.nickname);

 

System.out.println(this.userface);

System.out.println(this.userfaceFileName);

 

 

String uploadDir = 

this.servletContext.getRealPath("/uploadfile");

 

System.out.println(uploadDir);

 

String filepath = Common.getTimeStamp() + "_" +

 this.userfaceFileName;

 

File destFile = new File(uploadDir + "/" + filepath);

 

// 调用CommontIO中的方法

// FileUtils.copyFile(this.userface, destFile);

 

// 流的方式

InputStream inputStream = new FileInputStream(this.userface);

OutputStream outputStream = new FileOutputStream(destFile);

byte[] b = new byte[10240];

int i = 0;

while ((i = inputStream.read(b)) != -1) {

outputStream.write(b, 0, i);

}

outputStream.flush();

 

// 关闭流

 

inputStream.close();

inputStream = null;

 

outputStream.close();

outputStream = null;

 

// 把数据写入数据库的表中

 

Class.forName("oracle.jdbc.OracleDriver");

String url = "jdbc:oracle:thin:@127.0.0.1:1521:orcl";

String db_username = "scott";

String password = "tiger";

 

Connection conn = null;

java.sql.Statement stmt = null;

try {

conn = DriverManager.getConnection(url, db_username, password);

stmt = conn.createStatement();

 

// create table T_USER

// (

// USERID NUMBER not null,

// USERNAME VARCHAR2(20),

// NICKNAME VARCHAR2(20),

// FILENAME VARCHAR2(100),

// FILEPATH VARCHAR2(100),

// CONTENT_LOB CLOB,

// FACE_LOB BLOB,

// constraint PK_USER_ID primary key (USERID)

// )

 

StringBuffer insertSQL = new StringBuffer();

insertSQL.append("Insert into T_User(");

insertSQL.append("userid,username,nickname,");

insertSQL.append("filename,filepath");

insertSQL.append(") values(");

insertSQL.append("seq_user.nextval,");

insertSQL.append("'" + username + "',");

insertSQL.append("'" + nickname + "',");

insertSQL.append("'" + this.userfaceFileName + "',");

insertSQL.append("'" + filepath + "'");

insertSQL.append(")");

stmt.executeUpdate(insertSQL.toString());

 

catch (Exception e) {

e.printStackTrace();

finally {

stmt.close();

stmt = null;

 

conn.close();

conn = null;

}

return this.NONE;

}

拦截器Interceptor

默认拦截器栈放在自定义栈后面

<interceptor-ref name="defaultStack"></interceptor-ref>

在调用多个拦截器时,执行顺序是按照引用的先后顺序定的

实现Interceptor接口

public String intercept(ActionInvocation invocation) throws Exception {

 

ActionContext context = invocation.getInvocationContext();

 

ValueStack valueStack = invocation.getStack();

// 拦截器正在拦截的对象

Object obj = invocation.getAction();

 

System.out.println("第一个拦截器运行");

 

// 执行Action中调用方法

String invokeResult = invocation.invoke();

 

System.out.println("第一个拦截器结束");

 

System.out.println(context);

System.out.println(valueStack);

 

System.out.println(obj);

System.out.println(invokeResult);

 

return invokeResult;

}

继承AbstractInterceptor抽象类

 

0 0