28

Struts2与Struts1的对比

Action 类:
• Struts1要求Action类继承一个抽象基类。Struts1的一个普遍问题是使用抽象类编程而不是接口。
• Struts 2 Action类可以实现一个Action接口,也可实现其他接口,使可选和定制的服务成为可能。Struts2提供一个ActionSupport基类去实现 常用的接口。Action接口不是必须的,任何有execute标识的POJO对象都可以用作Struts2的Action对象。
线程模式:
• Struts1 Action是单例模式并且必须是线程安全的,因为仅有Action的一个实例来处理所有的请求。单例策略限制了Struts1 Action能作的事,并且要在开发时特别小心。Action资源必须是线程安全的或同步的。
• Struts2 Action对象为每一个请求产生一个实例,因此没有线程安全问题。(实际上,servlet容器给每个请求产生许多可丢弃的对象,并且不会导致性能和垃圾回收问题) 阅读全文 »

作者:jock

03

首先创建代表产品的类cn.org.jock.Product,代码如下:


package cn.org.jock;

import java.sql.Date;

public class Product {
private String name;
private double price;
private Date dateOfProduction;

public Date getDateOfProduction() {
return dateOfProduction;
}

public void setDateOfProduction(Date dateOfProduction) {
this .dateOfProduction = dateOfProduction;
}

public String getName() {
return name;
}

public void setName(String name) {
this .name = name;
}

public double getPrice() {
return price;
}

public void setPrice( double price) {
this .price = price;
}
}

本例子中的dateOfProduction属性使用了java.sql.Date,而不是java.util.Date。这是因为Struts 1.x不支持请求参数到java.util.Date的转换,归根到底是由于org.apache.commons.beanutils.ConvertUtilsBean.convert()不支持关于java.util.Date的转换。另外,值得注意的是common-beanutils是通过java.sql.Date.valueOf()方法工作的,所以在页面输入的字符串的格式必须为“yyyy-MM-dd”。

表单(Actoin Form)+ 列表(List)

首先,我们要创建类cn.org.jock.struts.util.AutoInitArrayList,代码如下:

/**
*
*/
package cn.org.jock.struts.util;

import java.util.ArrayList;

/**
* @author Jock
*
*/
public class AutoInitArrayList extends ArrayList {
private static final long serialVersionUID = 1L ;

private Class t = null ;

public AutoInitArrayList(Class t) {
this.t = t;
}

@Override
public E get( int index) {
try {
while (index >= size()) {
add(t.newInstance());
}
} catch (Exception e) {
e.printStackTrace();
}
return super .get(index);
}

}

AutoInitArrayList继承ArrayList并重载get()方法,作用就是在Struts 1.x框架调用这个方法时,如果index超出列表大小,则会实例化新项放到列表中,避免出现(IndexOutOfBoundsException)异常。 阅读全文 »

作者:jock Tags: , ,

11

首先当然是引入webworkxxx.jar,xwork.jar,common logging….

web.xml加入如下配置


webwork

com.opensymphony.webwork.dispatcher.FilterDispatcher


webwork
/*

阅读全文 »

作者:jock Tags: , ,

02

东西是好啊,可是比spring还那个,一下子丢过来的新东西可以把我淹死。。。sweat

作者:jock Tags:

02

Interceptor that is based off of MultiPartRequestWrapper, which is automatically applied for any request that includes a file. It adds the following parameters, where [File Name] is the name given to the file uploaded by the HTML form:

[File Name] : File – the actual File
[File Name]ContentType : String – the content type of the file
[File Name]FileName : String – the actual name of the file uploaded (not the HTML name)

You can get access to these files by merely providing setters in your action that correspond to any of the three patterns above, such as setDocument(File document), setDocumentContentType(String contentType), etc.
See the example code section.

This interceptor will add several field errors, assuming that the action implements ValidationAware. These error messages are based on several i18n values stored in struts-messages.properties, a default i18n file processed for all i18n requests. You can override the text of these messages by providing text for the following keys:

struts.messages.error.uploading – a general error that occurs when the file could not be uploaded
struts.messages.error.file.too.large – occurs when the uploaded file is too large
struts.messages.error.content.type.not.allowed – occurs when the uploaded file does not match the expected content types specified 阅读全文 »

作者:jock Tags:

02

Interceptors provide an excellent means to wrap before/after processing. The concept reduces code duplication (think AOP).

Order of interceptors…








Note that some interceptors will interrupt the stack/chain/flow… so the order is very important.

Iterceptors implementing com.opensymphony.xwork.interceptor.PreResultListener will run after the Action executes its action method but before the Result executes

thisWillRunFirstInterceptor
thisWillRunNextInterceptor
followedByThisInterceptor
thisWillRunLastInterceptor
MyAction1
MyAction2 (chain)
MyPreResultListener
MyResult (result)
thisWillRunLastInterceptor
followedByThisInterceptor
thisWillRunNextInterceptor
thisWillRunFirstInterceptor

作者:jock

02

If none of the above interceptors suit your particular need, you will have to implement your own interceptor. Fortunately, this is an easy task to accomplish. Suppose we need an interceptor that places a greeting in the Session according to the time of the day (morning, afternoon or evening). Here's how we could implement it: 阅读全文 »

作者:jock Tags:

29

一、struts2。

跟webwork配置基本一致,主要是struts2.properties和struts.xml 2个配置文件,我的struts2.properties如下配置:

struts2.properties 阅读全文 »

作者:jock Tags: , , ,

03

这是从网上摘抄的,貌似就是上传单个文件的复制版。里面有些没用的代码,我也懒得去删除了。 阅读全文 »

作者:堕落天使 Tags: ,

03

本文用的是Struts1.1的org.apache.struts.upload.FormFile类。很方便,不用自己写。也不用写一个jsp调用jspsmartupload就可以搞定。

FormFile的几个方法:
String getContentType();
byte[] getFileData();
String getFileName();
int getFileSize();
如果有必要可以在validate里调用一下做一些判断。

选择上传文件页面:selfile.jsp

——————————————————————————–
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<html:html>
<html:form action="/uploadsAction.do" enctype="multipart/form-data">
<html:file property="theFile"/>
<html:submit/>
</html:form>
</html:html>

——————————————————————————–
UpLoadAction.java
——————————————————————————– 阅读全文 »

作者:堕落天使