struts2——第一周小结

来源:互联网 发布:手机怎么改淘宝会员名 编辑:程序博客网 时间:2024/06/11 21:52

1) struts2详细介绍及环境搭建

1.  找到开发Struts2应用需要使用到的jar文件.

2.  编写Struts2的配置文件

3.  在web.xml中加入Struts2 MVC框架启动配置

4.  添加Hibernate所依赖的jar文件

5.  同样添加hibernate的配置文件

6.  添加对应数据库操作的驱动文件jar

2) struts2的常量设置

<!-- 常量的配置 -->

<!-- struts2的后缀 -->

1. <constant name="struts.action.extension" value="action"/>

<!-- 编码方式 -->

2. <constant name="struts.i18n.encoding" value="UTF-8"/>

<!-- 浏览器静态缓存最好处于关闭状态 -->

3. <constant name="struts.serve.static.browserCache" value="false"/>

<!-- struts.xml文件当被修改后 重新加载,开发阶段最好打开 -->

4. <constant name="struts.configuration.xml.reload" value="true"/>  

<!-- 处于开发阶段 最好把开发模式打开 会打印更多的详细错误信息 -->

5. <constant name="struts.devMode" value="true"/>

<!–该属性设置Struts 2是否支持动态方法调用,该属性的默认值是true。如果需要关闭动态方法调用,则可设置该属性为false。 -->

6.<constant name="struts.enable.DynamicMethodInvocation" value="false"/>

<!--上传文件的大小限制-->  1kb

7.<constant name="struts.multipart.maxSize" value=1024"/>

3) struts2的动态方法和通配符的使用

1.动态方法的使用{不常用}

    http://localhost:8080/struts_dynamic/

  csdn/action名称!方法名称.action

2.通配符的使用案例

第一个*表示的是类,第二个*表示的是action方法

<package name="user" namespace="/csdn" extends="struts-default">

<action name="*_*" class="www.csdn.struts_convert.action.{1}"method="{2}">

        <result name="success">/index.jsp</result>

</action></package>

4) struts2自定义的类型转换器

有局部的和全局的类型转换器

1.局部转换器:Action类所在的包下放置ActionClassName-conversion.properties文件,ActionClassNameAction的类名,后面的-conversion.properties是固定写法,对于本例而言,文件的名称应为HelloWorldAction-conversion.properties 。在properties文件中的内容为:

属性名称=类型转换器的全类名

对于本例而言, HelloWorldAction-conversion.properties文件中的内容为:

createtime= cn.csdn.conversion.DateConverter

2.全局转换器:WEB-INF/classes下放置xwork-conversion.properties文件 。在properties文件中的内容为:

待转换的类型=类型转换器的全类名

对于本例而言, xwork-conversion.properties文件中的内容为:

java.util.Date= cn.csdnconversion.DateConverter

5) struts2中域的使用

session域的使用

import com.opensymphony.xwork2.ActionContext;

ActionContext actionContext = ActionContext.getContext();

actionContext.getSession();获取session

actionContext.getApplication();获取application

6) struts2中的标签

<%@ taglib uri="/struts-tags"  prefix="s" %>

<s:……>   ……   </s:……>

7) struts2多文件上传

按规定定义属性:

private File[] upload; // 上传的文件

private String[] uploadContentType;// 文件类型

private String[] uploadFileName;// 文件的名称

导包import org.apache.commons.io.FileUtils; 使用FileUtils.copyFile()方法

public String upload() {

// 首先确认你保存的路径 ServletContext application

File file = new File(getPath());// 创建文件

if (!file.exists()) {

file.mkdirs();

}

try {

if (upload != null) {

for (int i = 0; i < upload.length; i++) {

File uploadFile = upload[i];

    // commons-io FileUtils工具 copyFile(要拷贝的文件,拷贝给谁的那个文件);

FileUtils.copyFile(uploadFile, new File(file, System

.currentTimeMillis()

+ "_" + uploadFileName[i]));

}

return "upload";

}

} catch (IOException e) {

e.printStackTrace();

}

return "fail";

}