Struts
2007/06/03 23:15
首先创建代表产品的类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<E> extends ArrayList<E> {
private static final long serialVersionUID = 1L ;
private Class<E> t = null ;
public AutoInitArrayList(Class<E> 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)异常。
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<E> extends ArrayList<E> {
private static final long serialVersionUID = 1L ;
private Class<E> t = null ;
public AutoInitArrayList(Class<E> 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)异常。
2007/04/11 23:35
首先当然是引入webworkxxx.jar,xwork.jar,common logging....
web.xml加入如下配置
<filter>
<filter-name>webwork</filter-name>
<filter-class>
com.opensymphony.webwork.dispatcher.FilterDispatcher
</filter-class>
</filter>
<filter-mapping>
<filter-name>webwork</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
web.xml加入如下配置
<filter>
<filter-name>webwork</filter-name>
<filter-class>
com.opensymphony.webwork.dispatcher.FilterDispatcher
</filter-class>
</filter>
<filter-mapping>
<filter-name>webwork</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
2007/04/03 00:06
东西是好啊,可是比spring还那个,一下子丢过来的新东西可以把我淹死。。。
2007/04/02 23:18
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
[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
2007/04/02 23:16
Interceptors provide an excellent means to wrap before/after processing. The concept reduces code duplication (think AOP).
Order of interceptors...
<interceptor-stack name="xaStack">
<interceptor-ref name="thisWillRunFirstInterceptor"/>
<interceptor-ref name="thisWillRunNextInterceptor"/>
<interceptor-ref name="followedByThisInterceptor"/>
<interceptor-ref name="thisWillRunLastInterceptor"/>
</interceptor-stack>
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
Order of interceptors...
<interceptor-stack name="xaStack">
<interceptor-ref name="thisWillRunFirstInterceptor"/>
<interceptor-ref name="thisWillRunNextInterceptor"/>
<interceptor-ref name="followedByThisInterceptor"/>
<interceptor-ref name="thisWillRunLastInterceptor"/>
</interceptor-stack>
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






