拦截器配置文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache SoftwareFoundation//DTD Struts Configuration 2.1.7//EN"
"http://struts.apache.org/dtds/struts-2.1.7.dtd">
<struts>
<constant name="struts.custom.i18n.resources" value="mess"></constant>
<package name="user" extends="struts-default" namespace="/">
<!--¶¨ÒåÀ¹½ØÆ÷ -->
<interceptors>
<!--ÆÕͨÀ¹½ØÆ÷;¿ÉÅäÖöà¸ö -->
<interceptor name="accessInter" class="interceptor.PageAccessInterceptor"></interceptor>
<!--À¹½ØÆ÷Õ» £»¿ÉÒýÓöà¸öÀ¹½ØÆ÷-->
<interceptor-stack name="interStack">
<interceptor-ref name="defaultStack"></interceptor-ref>
<interceptor-ref name="accessInter"></interceptor-ref>
</interceptor-stack>
</interceptors>
<default-action-ref name="error"></default-action-ref>
<!--¸øÕû¸öpackageϵÄËùÓÐactionÆ¥ÅäÀ¹½ØÆ÷ -->
<!--<default-interceptor-refname=""></default-interceptor-ref>
--><global-results>
<result name="login">/interceptor/fail.jsp</result>
<result name="sql">/exception.jsp</result>
<result name="root">/exception.jsp</result>
</global-results>
<global-exception-mappings>
<exception-mapping result="sql" exception="java.sql.SQLException"></exception-mapping>
<exception-mapping result="root" exception="java.lang.Exception"></exception-mapping>
</global-exception-mappings>
<action name="iterlogin" class="action.IterLoginAction">
<result name="success">/WEB-INF/interceptor/succ.jsp</result>
</action>
<action name="error">
<result>/error.jsp</result>
</action>
<action name="*">
<!--ijһ¸öactionÈ¥ÒýÓÃÌض¨µÄÀ¹½ØÆ÷»òÀ¹½ØÆ÷Õ»£»»á¸²¸Ç֮ǰ¶¨ÒåµÄĬÈÏÀ¹½ØÆ÷-->
<interceptor-ref name="interStack"></interceptor-ref>
<result>/WEB-INF/interceptor/{1}.jsp</result>
</action>
</package>
</struts>
拦截类
package interceptor;
import javax.interceptor.InvocationContext;
import com.opensymphony.xwork2.ActionInvocation;
importcom.opensymphony.xwork2.interceptor.AbstractInterceptor;
public class PageAccessInterceptor extendsAbstractInterceptor {
@Override
publicString intercept(ActionInvocation invocation) throws Exception {
System.out.println("== 现在进入拦截器 == ");
if(invocation.getInvocationContext().getSession().get("LOGIN_USER")==null){
return"login";
}else{
//通过此次拦截,继续下面操作.
returninvocation.invoke();
}
}
}
java和SQL Server中的数据类型转换
格式一定要一模一样
Timestamp dd =Timestamp.valueOf("2012-11-12 " +
new SimpleDateFormat("HH:mm:ss.SS").format(new Date()).toString());
一个db里面的data type和java中的一个type的mapping分为两步:
1。db里面的data type map到jdbc的一个type
2。jdbc的一个type map到java的一个type
对于sql server,下面是sql server2000的data type和jdbc的data type的mapping:
SQL Server Data Type JDBC Data Type
bigint BIGINT
binary BINARY
bit BIT
char CHAR
datetime TIMESTAMP
decimal ECIMAL
float FLOAT
image LONGVARBINARY
int INTEGER
money DECIMAL
nchar CHAR
ntext LONGVARCHAR
numeric NUMERIC
nvarchar VARCHAR
real REAL
smalldatetime TIMESTAMP
smallint SMALLINT
smallmoney DECIMAL
sql_variant VARCHAR
sysname VARCHAR
text LONGVARCHAR
timestamp BINARY
tinyint TINYINT
uniqueidentifier CHAR
varbinary VARBINARY
varchar VARCHAR
下面是jdbc data type和java data type之间的mapping:
JDBC Type Java Type
CHAR String
VARCHAR String
LONGVARCHAR String
NUMERIC java.math.BigDecimal
DECIMAL java.math.BigDecimal
BIT boolean
BOOLEAN boolean
TINYINT byte
SMALLINT short
INTEGER int
BIGINT long
REAL float
FLOAT double
DOUBLE double
BINARY byte[]
VARBINARY byte[]
LONGVARBINARY byte[]
DATE java.sql.Date
TIME java.sql.Time
TIMESTAMP java.sql.Timestamp
CLOB Clob
BLOB Blob
ARRAY Array
DATALINK java.net.URL
JAVA_OBJECT underlying Java class
最后,对于db和jdbc之间的mapping,可以参考相应的jdbc driver的文档。
对于jdbc和java之间的mapping,可以参考jdbc3。0的spec。 :)
总结
1.拦截器只能针对action进行拦截,如果要对页面访问者进行过滤,必须要过滤器
2.sql = sql + " where diary_title = ? and diary_author = ? and diary_author = ? anddiary_context like '%'+?+'%'";
3.struts默认都将项目放到WEB-INF下面,都是使用服务器转发的方式跳转
4.隐藏项目,PackageExplorer--->filter--->Closed projects, 再右键点需要隐藏的项目
struts2.xml中result type属性说明
1.chain:用来处理Action链,被跳转的action中仍能获取上个页面的值,如request信息。
com.opensymphony.xwork2.ActionChainResult
2.dispatcher:用来转向页面,通常处理JSP
org.apache.struts2.dispatcher.ServletDispatcherResult
3.freemaker:处理FreeMarker模板
org.apache.struts2.views.freemarker.FreemarkerResult
4.httpheader:控制特殊HTTP行为的结果类型
org.apache.struts2.dispatcher.HttpHeaderResult
5.stream:向浏览器发送InputSream对象,用来处理文件下载,还可用于返回AJAX数据
org.apache.struts2.dispatcher.StreamResult
6.velocity :处理Velocity模板
org.apache.struts2.dispatcher.VelocityResult
7.xsl:
处理XML/XLST模板
org.apache.struts2.views.xslt.XSLTResult
8.plainText:显示原始文件内容,例如文件源代码
org.apache.struts2.dispatcher.PlainTextResult
9.plaintext:显示原始文件内容,例如文件源代码
org.apache.struts2.dispatcher.PlainTextResult
10.redirect:重定向到一个URL,被跳转的页面中丢失传递的信息,如request
org.apache.struts2.dispatcher.ServletRedirectResult
11.redirectAction :重定向到一个Action,跳转的页面中丢失传递的信息,如request
org.apache.struts2.dispatcher.ServletActionRedirectResult
12.redirect-action:重定向到一个Action,跳转的页面中丢失传递的信息,如request
org.apache.struts2.dispatcher.ServletActionRedirectResult
二.redirect与redirect-action区别
一、使用redirect需要后缀名 使用redirect-action不需要后缀名
二、type="redirect" 的值可以转到其它命名空间下的action,而redirect-action只能转到同一命名空下的 action,因此它可以省略.action的后缀直接写action的名称。如:
<result name="success"type="redirect">viewTask.action</result>
<result name="success"type="redirect-action">viewTask</result>
附:redirect-action 传递参数
Xml代码
<action name="enterpreinfo" class ="preinfoBusinessAction" method= "enterPreinfoSub" >
<result name = "success" type = "redirect -action">
showpreinfo?preinfo.order_number=${preinfo.order_number}&preinfo.company_name=${preinfo.company_name}
</ result >
<result name = "error" type = "redirect " >
< param name = "location" > /error.jsp </ param >
</result >
</ action >
因为使用了redirect -action,所以要注意不能将 showpreinf?preinfo.order_number=${preinfo.order_number}写成showpreinf.action?preinfo.order_number=${preinfo.order_number}
其中${}为EL表达式,获取action:enterpreinfo中属性的值 ; 在这个配置文件里,多个参数的连接符使用了"&",但XML的语法规范,应该使用"&"代替"&",原理和HTML中的转义相同,开始没有注意,在struts 分析配置文件时,总是报出这样的错误:
The reference to entity "preinfo" must end with the ';' delimiter.
The reference to entity"preinfo" must end with the ';' delimiter.
进行上面说明的替换后,就正常了。
Java对List集合重新排序(比较器方式)2010-03-0210:13import java.util.ArrayList;
importjava.util.Collections;
importjava.util.List;
publicclass CompareClient {
/**
* @paramargs
*/
publicstatic void main(String[] args) {
Listlist = new ArrayList();
list.add(new Content(15000, "1asdfasd5000"));
list.add(new Content(10000, "10000"));
list.add(new Content(20000, "20000"));
list.add(new Content(30000, "30000"));
list.add(new Content(25000, "25000"));
list.add(new Content(13000, "13000"));
list.add(new Content(15000, "15000"));
list.add(new Content(89000, "89000"));
ContentComparator comp = new ContentComparator();
Collections.sort(list, comp);
Contentcontent;
for (inti = 0; i < list.size(); i++) {
content = (Content) list.get(i);
System.out.println(" content.getName() " +content.getName());
}
}
}
实体类:
public class Content {
private longkey;
privateString name;
publicContent(long key, String name) {
this.key= key;
this.name = name;
}
public longgetKey() {
returnkey;
}
public voidsetKey(long key) {
this.key= key;
}
publicString getName() {
returnname;
}
public voidsetName(String name) {
this.name = name;
}
}
比较器实现:
import java.util.Comparator;
public class ContentComparator implements Comparator{
public int compare(Object o1, Object o2){
Contentc1 = (Content) o1;
Contentc2 = (Content) o2;
if(c1.getKey() > c2.getKey()) {
return 1;
} else{
if(c1.getKey() == c2.getKey()) {
return 0;
} else {
return -1;
}
}
}
}
代码应该比较简单,我们主要是实现了Comparable接口,然后调用了Collections.sort方法来完成对List的排序操作。大家学习愉快!