Y购买金豆怎么每次都是请求iphone备份会话失败败

extends Repository { S save(S entity);① T findOne(ID primaryKey);② Iterable findAll();③ Long count();④ void delete(T entity);⑤ boolean exists(ID primaryKey);⑥ // & 省略其他方法 }]]&
① 保存给定的实体。
②返回指定ID的实体。
③返回全部实体。
④返回实体的总数。
⑤删除指定的实体。
⑥判断给定的ID是否存在。
通常我们要扩展功能的方法,那么我们就需要在接口上做子接口。那么我们要添加功能的时候,就在CrudRepository的基础上去增加。
PagingAndSortingRepository&是一个继承CrudRepository的接口,他扩展了分页与排序的功能。
例1.2 PagingAndSortingRepositorytruejava1 extends CrudRepository { Iterable findAll(Sort sort); Page findAll(Pageable pageable); }]]&
由编辑于 12:16:08
暂无贡献等级
最近登录: 11:12:54
jsp/servlet 表单上传图片、但不能传值了、或传值中文乱码问题解决...
jsp/servlet 表单上传图片、但不能传值了、或传值中文乱码问题解决
暂无贡献等级
最近登录: 11:12:54
1.& 确认工程编码是UTF-8&&& 右击工程名,单击properties,单击Resource,把Text file encoding设置为UTF-82. 确认工作空间为UTF-8&&& 单击windows-&Gener...
1.& 确认工程编码是UTF-8
&&& 右击工程名,单击properties,单击Resource,把Text file encoding设置为UTF-8
2. 确认工作空间为UTF-8
&&& 单击windows-&General-&Workspace,把Text file encoding设置为UTF-8
3. 将jsp请求设置为UTF-8
&&& 在页头添加&%@page contentType=&text/ charset=UTF-8& %&
4. 将jsp响应设置为UTF-8
&&& &meta http-equiv=&content-type& content=&text/ charset=UTF-8&&
5. 如果还有乱码则在struts.xml中设置中文字符集:
&&& &constant name=&struts.i18n.encoding& value=&utf-8&&&/constant&
6. 如果还有乱码,在web.xml中添加中文过滤器:
&&& &filter&
&& &&& &&filter-name&CharacterEncoding&/filter-name&
&& &&& &&filter-class&org.springframework.web.filter.CharacterEncodingFilter&/filter-class&
&& &&& &&init-param&
& && &&& &&& &&param-name&encoding&/param-name&
& && &&& &&& &&param-value&UTF-8&/param-value&
& && &&& &&/init-param&
& && &&& &&init-param&
& && &&& &&& &&param-name&forceEncoding&/param-name&
& && &&& &&& &&param-value&true&/param-value&
& && &&& &&/init-param&
&&& &/filter&
&&& &filter-mapping&
&& &&& &&filter-name&CharacterEncoding&/filter-name&
&& &&& &&url-pattern&/*&/url-pattern&
&&& &/filter-mapping&
7. 如果还有乱码,在Action中的response字符集设置为UTF-8:
&&& ServletActionContext.getResponse.setCharacterEncoding(&utf-8&);
8. 如果还有乱码,则将服务器跳转配置设置为UTF-8:
&&& resin服务器的设置如下,修改conf目录下的resin.conf:
&&& &web-app id='/' document-directory=&E:\WorkSpace\StrutsDemo\WebRoot&&
&&&& &&& &&character-encoding&utf-8&/character-encoding&& &!--指定字符集编码--&
&&&&&&& &work-dir&D:/tools/resin-3.0.22/work&/work-dir&& &!--指定work工作目录--&
&&& &&& &&temp-dir&D:/tools/resin-3.0.22/temp&/temp-dir&& &!--指定临时工作目录--&
&&& &/web-app&
&&& tomcat服务器设置如下,修改conf目录下的server.xml:
&&& &Connector port=&80& maxHttpHeaderSize=&8192&
&&&&&&&&&&&&&& maxThreads=&150& minSpareThreads=&25& maxSpareThreads=&75&
&&&&&&&&&&&&&& enableLookups=&false& redirectPort=&8443& acceptCount=&100&
&&&&&&&&&&&&&& connectionTimeout=&20000& disableUploadTimeout=&true& URIEncoding=&UTF-8&/&&&&
由编辑于 21:06:49
暂无贡献等级
最近登录: 11:12:54
防止中文乱码的过滤器&&&...
防止中文乱码的过滤器
暂无贡献等级
最近登录: 11:12:54
{代码...}昨天在群里跟大家讨论了下java反射调用可变参数的问题,这个问题起因是我们需要反射调用另一个部门提供的方法,我同事说java不能反射调用可变参数的方法,于是我写了个demo证明了他这个观点的错误。但是测试过程中,有一点我不明白,就是反射调用可变参数的方法时,为什么一定要保证传入的参数数组长度为1,在...
昨天在群里跟大家讨论了下java反射调用可变参数的问题,这个问题起因是我们需要反射调用另一个部门提供的方法,我同事说java不能反射调用可变参数的方法,于是我写了个demo证明了他这个观点的错误。但是测试过程中,有一点我不明白,就是反射调用可变参数的方法时,为什么一定要保证传入的参数数组长度为1,在群里跟大家讨论了很多,没有得到确切的答案,参照网上大牛写的东西和我自己跟源码的过程,记录如下:
1.两个类,一个父类,一个子类
package com.reflect.
public class BaseObject {
public void getObjectName(){
System.out.println(&BaseObject&);
package com.reflect.
public class SubObject extends BaseObject{
public void getObjectName() {
System.out.println(&SubObject&);
public void getParamsLength(String...params){
System.out.println(&param's length is:&+params.length);
public void getParamsLength(String param1,String param2){
System.out.println(param1 + &-& + param2);
2.测试类,主要测试重载方法的调用、可变参数方法的调用、定参方法的调用
package com.reflect.
import java.lang.reflect.M
public class ReflectTest {
private static final String BASE_OBJECT_PATH = &com.reflect.test.BaseObject&;
private static final String SUB_OBJECT_PATH = &com.reflect.test.SubObject&;
public static void main(String[] args) throws Exception{
Class&?& bClazz = Class.forName(BASE_OBJECT_PATH);
Class&?& sClazz = Class.forName(SUB_OBJECT_PATH);
Object bObj = bClazz.newInstance();//父类实例
Object sObj = sClazz.newInstance();//子类实例
//1.反射调用子类父类的重载方法
//多态+动态绑定
Method bMethod = bClazz.getDeclaredMethod(&getObjectName&);
bMethod.invoke(bObj);//父类的bMethod调用父类的getObjectName()
bMethod.invoke(sObj);//父类的bMethod调用子类的getObjectName();
Method sMethod = sClazz.getDeclaredMethod(&getObjectName&);
//不符合多态和动态绑定
//sMethod.invoke(bObj);//sMethod调用父类的getObjectName(),会报错:java.lang.IllegalArgumentException: object is not an instance of declaring class
sMethod.invoke(sObj);
//2.反射调用可变参数的方法
Method changeMethod = sClazz.getDeclaredMethod(&getParamsLength&, String[].class);
//可变参数必须这样封装,因为java反射内部实现做了参数个数为1的判断,如果参数长度不为1,则会抛出异常
String[] strParams = {&a&,&b&,&c&};
Object[] cParams = {strParams};
changeMethod.invoke(sObj, cParams);
//3.反射调用固定长度参数的方法
Method unChangeMethod1 = sClazz.getDeclaredMethod(&getParamsLength&, String.class,String.class);
unChangeMethod1.invoke(sObj, &Hello&,&Java&);
//也可以写成这样
Class&?&[] clazzs = {String.class,String.class};
Method unChangeMethod2 = sClazz.getDeclaredMethod(&getParamsLength&, clazzs);
unChangeMethod2.invoke(sObj, &Hello&,&Java&);
//下面的这种调用形式也是可以的,不过会报警告
//String[] params1 = {&Hello&,&Java&};
//unChangeMethod1.invoke(sObj, params1);
下面是JDK里面Method 的invoke方法的源码
从代码中可以看出,先检查&AccessibleObject的override属性是否为true(override属性默认为false)。AccessibleObject是Method,Field,Constructor的父类,可调用setAccessible方法改变,如果设置为true,则表示可以忽略访问权限的限制,直接调用。
如果不是ture,则要进行访问权限检测。用Reflection的quickCheckMemberAccess方法先检查是不是public的,如果不是再用Reflection.getCallerClass()方法获得到调用这个方法的Class,然后做是否有权限访问的校验,校验之后缓存一次,以便下次如果还是这个类来调用就不用去做校验了,直接用上次的结果。
@CallerSensitive
public Object invoke(Object obj, Object... args)
throws IllegalAccessException, IllegalArgumentException,
InvocationTargetException
if (!override) {
if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) {
// Until there is hotspot @CallerSensitive support
// can't call Reflection.getCallerClass() here
// Workaround for now: add a frame getCallerClass to
// make the caller at stack depth 2
Class&?& caller = getCallerClass();
checkAccess(caller, clazz, obj, modifiers);
MethodAccessor ma = methodA
// read volatile
if (ma == null) {
ma = acquireMethodAccessor();
return ma.invoke(obj, args);
//验证的代码,securityCheckCache就是JDK做的缓存
volatile Object securityCheckC
void checkAccess(Class&?& caller, Class&?& clazz, Object obj, int modifiers)
throws IllegalAccessException
if (caller == clazz) {
// quick check
// ACCESS IS OK
Object cache = securityCheckC
// read volatile
Class&?& targetClass =
if (obj != null
&& Modifier.isProtected(modifiers)
&& ((targetClass = obj.getClass()) != clazz)) {
// Must match a 2-list of { caller, targetClass }.
if (cache instanceof Class[]) {
Class&?&[] cache2 = (Class&?&[])
if (cache2[1] == targetClass &&
cache2[0] == caller) {
// ACCESS IS OK
// (Test cache[1] first since range check for [1]
// subsumes range check for [0].)
} else if (cache == caller) {
// Non-protected case (or obj.class == this.clazz).
// ACCESS IS OK
// If no return, fall through to the slow path.
slowCheckMemberAccess(caller, clazz, obj, modifiers, targetClass);
然后就是调用MethodAccessor的invoke方法了。
调用MethodAccessor的invoke方法。每个Method对象包含一个root对象,root对象里持有一个MethodAccessor对象。这个对象由ReflectionFactory方法生成,ReflectionFactory对象在Method类中是static&final的由native方法实例化。代码片段如下;
//Method类中的代码片段,生成MethodAccessor
private volatile MethodAccessor methodA
private MethodAccessor acquireMethodAccessor() {
// First check to see if one has been created yet, and take it
MethodAccessor tmp =
if (root != null) tmp = root.getMethodAccessor();
if (tmp != null) {
methodAccessor =
// Otherwise fabricate one and propagate it up to the root
tmp = reflectionFactory.newMethodAccessor(this);
setMethodAccessor(tmp);
// reflectionFactory在父类AccessibleObject中定义,代码片段如下:
static final ReflectionFactory reflectionFactory =
AccessController.doPrivileged(
new sun.reflect.ReflectionFactory.GetReflectionFactoryAction());
ReflectionFactory生成MethodAccessor:如果noInflation的属性为true则直接返回MethodAccessorGenerator创建的一个MethodAccessor,否则返回DelegatingMethodAccessorImpl,并将他与一个NativeMethodAccessorImpl互相引用。但DelegatingMethodAccessorImpl执行invoke方法的时候又委托给NativeMethodAccessorImpl了。代码片段如下:
public MethodAccessor newMethodAccessor(Method paramMethod) {
checkInitted();
if (noInflation) {
return new MethodAccessorGenerator().generateMethod(paramMethod.getDeclaringClass(), paramMethod.getName(), paramMethod.getParameterTypes(), paramMethod.getReturnType(), paramMethod.getExceptionTypes(), paramMethod.getModifiers());
NativeMethodAccessorImpl localNativeMethodAccessorImpl = new NativeMethodAccessorImpl(paramMethod);
DelegatingMethodAccessorImpl localDelegatingMethodAccessorImpl = new DelegatingMethodAccessorImpl(localNativeMethodAccessorImpl);
localNativeMethodAccessorImpl.setParent(localDelegatingMethodAccessorImpl);
return localDelegatingMethodAccessorI
MethodAccessor实现有两个版本,一个是Java实现的,另一个是native code实现的。Java实现的版本在初始化时需要较多时间,但长久来说性能较好;native版本正好相反,启动时相对较快,但运行时间长了之后速度就比不过Java版了。这是HotSpot的优化方式带来的性能特性,同时也是许多虚拟机的共同点:跨越native边界会对优化有阻碍作用,它就像个黑箱一样让虚拟机难以分析也将其内联,于是运行时间长了之后反而是托管版本的代码更快些。&为了权衡两个版本的性能,Sun的JDK使用了&inflation&的技巧:让Java方法在被反射调用时,开头若干次使用native版,等反射调用次数超过阈值时则生成一个专用的MethodAccessor实现类,生成其中的invoke()方法的字节码,以后对该Java方法的反射调用就会使用Java版。
看下NativeMethodAccessorImpl 中的invoke方法:
代码片段如下:
package sun.
import java.lang.reflect.InvocationTargetE
import java.lang.reflect.M
class NativeMethodAccessorImpl extends MethodAccessorImpl
private DelegatingMethodAccessorI
private int numI
NativeMethodAccessorImpl(Method paramMethod)
this.method = paramM
public Object invoke(Object paramObject, Object[] paramArrayOfObject)
throws IllegalArgumentException, InvocationTargetException
if (++this.numInvocations & ReflectionFactory.inflationThreshold()) {
MethodAccessorImpl localMethodAccessorImpl = (MethodAccessorImpl)new MethodAccessorGenerator().generateMethod(this.method.getDeclaringClass(), this.method.getName(), this.method.getParameterTypes(), this.method.getReturnType(), this.method.getExceptionTypes(), this.method.getModifiers());
this.parent.setDelegate(localMethodAccessorImpl);
return invoke0(this.method, paramObject, paramArrayOfObject);
void setParent(DelegatingMethodAccessorImpl paramDelegatingMethodAccessorImpl) {
this.parent = paramDelegatingMethodAccessorI
private static native Object invoke0(Method paramMethod, Object paramObject, Object[] paramArrayOfObject);
调用natiave方法invoke0执行方法调用.
注意这里有一个计数器numInvocations,每调用一次方法+1,当比&ReflectionFactory.inflationThreshold(15)大的时候,用MethodAccessorGenerator创建一个MethodAccessor,并把之前的DelegatingMethodAccessorImpl引用替换为现在新创建的。下一次DelegatingMethodAccessorImpl就不会再交给NativeMethodAccessorImpl执行了,而是交给新生成的java字节码的MethodAccessor
每次NativeMethodAccessorImpl.invoke()方法被调用时,都会增加一个调用次数计数器,看超过阈值没有;一旦超过,则调用MethodAccessorGenerator.generateMethod()来生成Java版的MethodAccessor的实现类,并且改变DelegatingMethodAccessorImpl所引用的MethodAccessor为Java版。后续经由DelegatingMethodAccessorImpl.invoke()调用到的就是Java版的实现了。
注意到关键的invoke0()方法是个native方法。它在HotSpot VM里是由JVM_InvokeMethod()函数所支持的,是用C写的
为了验证这个结论,我故意写出一个非法参数,循环调用16次并catch下异常,结果如下:从结果中看出,前15次都是调用NativeMethodAccessorImpl,第16次开始就是调用DelegatingMethodAccessorImpl了。
java.lang.IllegalArgumentException: wrong number of arguments
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.reflect.test.ReflectTest.main(ReflectTest.java:44)
java.lang.IllegalArgumentException: wrong number of arguments
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.reflect.test.ReflectTest.main(ReflectTest.java:44)
java.lang.IllegalArgumentException: wrong number of arguments
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.reflect.test.ReflectTest.main(ReflectTest.java:44)
java.lang.IllegalArgumentException: wrong number of arguments
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.reflect.test.ReflectTest.main(ReflectTest.java:44)
java.lang.IllegalArgumentException: wrong number of arguments
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.reflect.test.ReflectTest.main(ReflectTest.java:44)
java.lang.IllegalArgumentException: wrong number of arguments
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.reflect.test.ReflectTest.main(ReflectTest.java:44)
java.lang.IllegalArgumentException: wrong number of arguments
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.reflect.test.ReflectTest.main(ReflectTest.java:44)
java.lang.IllegalArgumentException: wrong number of arguments
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.reflect.test.ReflectTest.main(ReflectTest.java:44)
java.lang.IllegalArgumentException: wrong number of arguments
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.reflect.test.ReflectTest.main(ReflectTest.java:44)
java.lang.IllegalArgumentException: wrong number of arguments
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.reflect.test.ReflectTest.main(ReflectTest.java:44)
java.lang.IllegalArgumentException: wrong number of arguments
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.reflect.test.ReflectTest.main(ReflectTest.java:44)
java.lang.IllegalArgumentException: wrong number of arguments
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.reflect.test.ReflectTest.main(ReflectTest.java:44)
java.lang.IllegalArgumentException: wrong number of arguments
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.reflect.test.ReflectTest.main(ReflectTest.java:44)
java.lang.IllegalArgumentException: wrong number of arguments
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.reflect.test.ReflectTest.main(ReflectTest.java:44)
java.lang.IllegalArgumentException: wrong number of arguments
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.reflect.test.ReflectTest.main(ReflectTest.java:44)
java.lang.IllegalArgumentException
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.reflect.test.ReflectTest.main(ReflectTest.java:44)
下面看看java版的DelegatingMethodAccessorImpl的实现:
package sun.
import java.lang.reflect.InvocationTargetE
class DelegatingMethodAccessorImpl extends MethodAccessorImpl
private MethodAccessorI
DelegatingMethodAccessorImpl(MethodAccessorImpl paramMethodAccessorImpl)
setDelegate(paramMethodAccessorImpl);
public Object invoke(Object paramObject, Object[] paramArrayOfObject)
throws IllegalArgumentException, InvocationTargetException
return this.delegate.invoke(paramObject, paramArrayOfObject);
void setDelegate(MethodAccessorImpl paramMethodAccessorImpl) {
this.delegate = paramMethodAccessorI
package sun.
public class GeneratedMethodAccessor1 extends MethodAccessorImpl {
public GeneratedMethodAccessor1() {
public Object invoke(Object obj, Object[] args)
throws IllegalArgumentException, InvocationTargetException {
// prepare the target and parameters
if (obj == null) throw new NullPointerException();
A target = (A)
if (args.length != 1) throw new IllegalArgumentException();
String arg0 = (String) args[0];
} catch (ClassCastException e) {
throw new IllegalArgumentException(e.toString());
} catch (NullPointerException e) {
throw new IllegalArgumentException(e.toString());
// make the invocation
target.foo(arg0);
} catch (Throwable t) {
throw new InvocationTargetException(t);
if (args.length != 1) throw new IllegalArgumentException();这一句就能解释我之前的疑问了,这块会判断参数数组的长度,如果长度不等于1,就会抛出非法参数的异常。
而且MethodAccessor会做强制类型转换再进行方法调用,但父类强制转化成子类的的时候就会报错类型不匹配错误了,所以如果变量的引用声明是父但实际指向的对象是子,那么这种调用也是可以的。
暂无贡献等级
最近登录: 11:12:54
{代码...}经过两天工作闲余时间的奋战,终于flex3+struts 1.3+spring +ibatis 2.x 整合成功,下面介绍下详细的步骤和核心代码:IDE:myeclipse(当然:前提是FLEX+java整合成功的情况下,关于flex+java整合的文章就比较多,google下就OK):1.成功整合...
经过两天工作闲余时间的奋战,终于flex3+struts 1.3+spring +ibatis 2.x 整合成功,下面介绍下详细的步骤和核心代码:
IDE:myeclipse
(当然:前提是FLEX+java整合成功的情况下,关于flex+java整合的文章就比较多,google下就OK):
1.成功整合FLEX+JAVA后,右键单击项目选择 MyEclipse---》add struts(添加STRUTS比较简单这里就不多说了),下面贴代码:
&?xml version=&1.0& encoding=&UTF-8&?&
&!DOCTYPE struts-config PUBLIC &-//Apache Software Foundation//DTD Struts Configuration 1.3//EN& &http://struts.apache.org/dtds/struts-config_1_3.dtd&&
&struts-config&
&form-beans /&
&global-exceptions /&
&action-mappings &
path=&/user&
type=&com.yinhai.struts.action.UserAction&
scope=&request&
&forward name=&sucess& path=&/index.jsp& /&
&/action-mappings&
&/struts-config&
在web-info下的struts-config.xml中添加如上代码
(2)UserAction
package com.yinhai.struts.
import java.io.IOE
import java.io.PrintW
import java.sql.C
import java.sql.DriverM
import java.sql.SQLE
import java.util.L
import javax.servlet.http.HttpServletR
import javax.servlet.http.HttpServletR
import model.U
import org.apache.struts.action.A
import org.apache.struts.action.ActionF
import org.apache.struts.action.ActionF
import org.apache.struts.action.ActionM
import org.springframework.beans.factory.BeanF
import org.springframework.context.support.ClassPathXmlApplicationC
import dao.ContextH
import dao.UserDaoI
* MyEclipse Struts
* Creation date: 08-05-2009
* XDoclet definition:
* @struts.action parameter=&method& validate=&true&
* @struts.action-forward name=&fail& path=&/fail.jsp&
* @struts.action-forward name=&sucess& path=&/sucess.jsp&
public class UserAction extends Action
* Generated Methods
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
* @throws IOException
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
ClassPathXmlApplicationContext ctxss = new ClassPathXmlApplicationContext(&applicationContext.xml&);
BeanFactory ctx=(BeanFactory)
UserDaoImpl um = (UserDaoImpl) ctx.getBean(&userDao&);
UserDaoImpl um =(UserDaoImpl)ContextHelper.getContext().getBean(&userDao&);
List users = um.getUsers();
User user=
String xml=&&?xml version=\&1.0\& encoding=\&utf-8\&?&&users&&;
for(int i=0;i&users.size();i++){
user=(User)users.get(i);
xml=xml+&&user&&+
&&usernumber&&+user.getUsernumber()+&&/usernumber&& +
&&name&&+user.getName()+&&/name&&+
&&password&&+user.getPassword()+&&/password&&+
&&/user&&;
xml=xml+&&/users&&;
System.out.println(xml);
PrintWriter p =
p=response.getWriter();
response.getWriter().write(xml);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
//p.write(xml);
p.print(&&?xml version=\&1.0\& encoding=\&utf-8\&?&&users&&user&&usernumber&1&/usernumber&&name&caicai&/name&&password&dddddd&/password&&/user&&user&&usernumber&2&/usernumber&&name&yingying&/name&&password&dddddd&/password&&/user&&user&&usernumber&3&/usernumber&&name&ceshi&/name&&password&dddddd&/password&&/user&&user&&usernumber&4&/usernumber&&name&ceshi&/name&&password&dddddd&/password&&/user&&);
p.close();
(3)获取bean的工具类
import org.springframework.context.support.ClassPathXmlApplicationC
Time: 17:28:48 To change this template use File | Settings |
* File Templates.
public class ContextHelper {
private static ClassPathXmlApplicationContext _
_ctx = new ClassPathXmlApplicationContext(&/applicationContext.xml&);
public static ClassPathXmlApplicationContext getContext() {
2.添加SRPING
右键单击项目选择 MyEclipse---》add spring
spring的配置文件
&?xml version=& 1.0 & encoding=& UTF-8 & ?&
xmlns=& http://www.springframework.org/schema/beans &
xmlns:xsi=& http://www.w3.org/2001/XMLSchema-instance &
xsi:schemaLocation=& http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd &&
&bean id=& dataSource & class=& mons.dbcp.BasicDataSource & destroy - method=& close &&
&property name=& driverClassName & value=& com.mysql.jdbc.Driver & /&
&property name=& url & value=& jdbc:mysql://localhost:3306/test & /&
&property name=& username & value=& root & /&
&property name=& password & value=& root & /&
&bean id=& sqlMapClient & class=& org.springframework.orm.ibatis.SqlMapClientFactoryBean &&
&property name=& configLocation & value=& sqlmap-config.xml & /&
&property name=& dataSource & ref=& dataSource & /&
&bean id=& userDao & class=& dao.UserDaoImpl &&
&property name=& sqlMapClient & ref= & sqlMapClient & /&
(3)添加IBATIS
首先将ibatis的JAR包导入,放在LIB下即可
IBTIS配置文件
sqlmapconfig.xml
&?xml version=& 1.0 & encoding=& UTF-8 & ?&
&! DOCTYPE sqlMapConfig
PUBLIC & -//ibatis.apache.org//DTD SQL Map Config 2.0//EN &
& http://ibatis.apache.org/dtd/sql-map-config-2.dtd &&
&sqlMapConfig&
&!-- Configure a built - in transaction manager. If you ' re using an
app server, you probably want to use its transaction manager
and a managed datasource --&
&!-- List the SQL Map XML files. They can be loaded from the
classpath, as they are here (com.domain.data) --&
&sqlMap resource=& dao/User.xml & /&
&!-- List more here
&sqlMap resource=& com/mydomain/data/Order.xml & /&
&sqlMap resource= & com/mydomain/data/Documents.xml & /&
&/sqlMapConfig&
&?xml version=& 1.0 & encoding=& UTF-8 & ?&
&! DOCTYPE sqlMap
PUBLIC & -//ibatis.apache.org//DTD SQL Map 2.0//EN &
& http://ibatis.apache.org/dtd/sql-map-2.dtd &&
&sqlMap namespace=& User &&
&!-- Use type aliases to avoid typing the full classname every time. --&
&typeAlias alias=& user & type=& model.User & /&
&!-- Select with no parameters using the result map for Account class . --&
&select id=& getUsers & resultClass= & user &&
注意:User.xml一定要和相应的DAO的实现类放一个包下
4.FLEX文件
&?xml version=& 1.0 & encoding=& utf-8 & ?&
&mx:Application xmlns:mx=& /2006/mxml & layout=& absolute &&
&mx:Script&
&! [CDATA[
import mx.rpc.events.FaultE
import mx.controls.A
import mx.rpc.events.ResultE
import mx.controls.DataG
[Bindable]
public var gridXml:XML;
public function httpHandler(event:ResultEvent): void {
Alert.show(&hello2&);
gridXml=XML(event.result);
griddata.dataProvider=gridXml.children();//XMLList
public function initHandler(): void {
testHttp.url=&http://localhost:8080/LCSYS/user.do&;
Alert.show(&hello1&);
testHttp.send();
public function httpFail(event:FaultEvent): void {
Alert.show(&调用失败&);
Alert.show(event.message.toString());
&/mx:Script&
&mx:HTTPService id=& testHttp & result=& httpHandler(event) & fault=& httpFail(event) & resultFormat=& e4x & /&
&mx:Panel x=& & y=& & width=& & height=& & layout=& absolute & id=& userPannel & title=& userPannel1 &&
&mx:DataGrid x=& & y=& & width=& 100% & height=& 100% & id=& griddata &&
&mx:columns&
&mx:DataGridColumn dataField=& usernumber & headerText=& usernumber & /&
&mx:DataGridColumn dataField=& name & headerText=& name & /&
&mx:DataGridColumn dataField=& password & headerText=& password & /&
&/mx:columns&
&/mx:DataGrid&
&mx:ControlBar height=& & y=& &&
&mx:Button label=& 查询人员列表 & click= & initHandler() & /&
&/mx:ControlBar&
&/mx:Panel&
&/mx:Application&
5.其他文件:
工具类,解决乱码
import java.io.UnsupportedEncodingE
import org.springframework.context.support.ClassPathXmlApplicationC
public class Tool {
public static String change(String str){
String str2;
str2=new String(str.getBytes(&iso-8859-1&),&UTF-8&);
return str2;
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
userDaoImpl
import java.util.L
import org.springframework.orm.ibatis.support.SqlMapClientDaoS
public class UserDaoImpl extends SqlMapClientDaoSupport implements UserDao {
public List getUsers() {
// TODO Auto-generated method stub
return this.getSqlMapClientTemplate().queryForList(&getUsers&);
时间关系,介绍得比较粗糙也没撒说明,不过只要对框架较熟悉的应该很快能搭建起环境
本文章主要是集成框架介绍
注意:需要IBATIS的JAR吧,dbcp连接池的包,还有相应的驱动包
暂无贡献等级
最近登录: 11:12:54
{代码...}&这两天一直在研究ibatis与spring的整合 一个小小的demo搞的我头晕目眩的,但程序一旦跑起来了,突然有一种豁然开朗,重见天日,感觉生活很美好的感觉!,也许,这就是那一行行的代码带给我们的不同享受吧。呵呵,废话就不多说了。在此先引用几句别人的资料。。。Spring通过DAO模式,...
&这两天一直在研究ibatis与spring的整合 一个小小的demo搞的我头晕目眩的,但程序一旦跑起来了,突然有一种豁然开朗,重见天日,感觉生活很美好的感觉!,也许,这就是那一行行的代码带给我们的不同享受吧。呵呵,废话就不多说了。
在此先引用几句别人的资料。。。
Spring通过DAO模式,提供了对iBATIS的良好支持。SqlMapClient对象是iBATIS中的主要对象,我们可以通过配置让spring来管理SqlMapClient对象的创建。
与hibernate类似,Spring提供了SqlMapClientDaoSupport对象,我们的DAO可以继承这个类,通过它所提供的SqlMapClientTemplate对象来操纵数据库。看起来这些概念都与hibernate类似。
通过SqlMapClientTemplate来操纵数据库的CRUD是没有问题的。此篇文章没有进行事务处理。
本文采用ibatis+spring+mysql 进行编写
数据库脚本如下
create table person(
id int primary key,
name varchar(10),
一:要有一个PO类
&& Person.java
import java.io.S
public class Person implements Serializable{
private static final long serialVersionUID = -030507L;
public Person(){
public Person(int id,String name,int sex){
this.name =
this.sex =
public int getId() {
public void setId(int id) {
public String getName() {
public void setName(String name) {
this.name =
public int getSex() {
public void setSex(int sex) {
this.sex =
二:DAO接口类
&&& IAction.java
import java.util.L
import po.P
public interface IAction {
public boolean insertPerson(Person person);
public boolean deleteById(int id);
public boolean updatePerson(Person person);
public Person queryById(int id);
//根据ID查询
public List&Person& queryAllPerson();
//查询全部
三:DAO实现类
&&&ActionImpl.java&此类继承SqlMapClientSupport 实现IAction接口
package dao.
import java.io.IOE
import java.io.R
import java.sql.SQLE
import java.util.L
import org.springframework.orm.ibatis.support.SqlMapClientDaoS
import mon.resources.R
import com.ibatis.sqlmap.client.SqlMapC
import com.ibatis.sqlmap.client.SqlMapClientB
import com.ibatis.sqlmap.client.SqlMapS
import po.P
import dao.IA
public class ActionImpl extends SqlMapClientDaoSupport implements IAction {
//添加操作
public boolean insertPerson(Person person) {
// TODO Auto-generated method stub
getSqlMapClientTemplate().insert(&insertPerson&,person);
//删除操作
public boolean deleteById(int id) {
// TODO Auto-generated method stub
getSqlMapClientTemplate().delete(&deleteById&, id);
//查询全部
public List&Person& queryAllPerson() {
// TODO Auto-generated method stub
List&Person& persons = getSqlMapClientTemplate().queryForList(&queryAllPerson&);
public Person queryById(int id) {
// TODO Auto-generated method stub
//自己添加实现代码
public boolean updatePerson(Person person) {
// TODO Auto-generated method stub
//自己添加实现代码
四:既然是ibatis spring整合 那就必须要有ibatis的配置文件
& SqlMapConfig.xml
&?xml version=&1.0& encoding=&UTF-8& ?&
&!DOCTYPE sqlMapConfig
PUBLIC &-////DTD SQL Map Config 2.0//EN&
&/dtd/sql-map-config-2.dtd&&
&sqlMapConfig&
&!--此处一定不能有&settings/& 标签--&
cacheModelsEnabled=&true&
enhancementEnabled=&true&
lazyLoadingEnabled=&true&
errorTracingEnabled=&true&
maxRequests=&32&
maxSessions=&10&
maxTransactions=&5&
useStatementNamespaces=&false& /&
&sqlMap resource=&po/Person.xml& /&
&/sqlMapConfig&
SqlMapClient.xml里本应该有数据源的配置的 使用spring之后数据源的配置移植到了spring上
五:Person.xml
&& 里面配置了一下对数据的增删改查操作
&?xml version=&1.0& encoding=&UTF-8&?&
&!DOCTYPE sqlMap
PUBLIC &-////DTD SQL Map 2.0//EN&
&/dtd/sql-map-2.dtd&&
&typeAlias alias=&person& type=&po.Person& /&
&insert id=&insertPerson& parameterClass=&po.Person&&
insert into person values (#id#,#name#,#sex#)
&delete id=&deleteById& parameterClass=&int&&
delete from person where id=#id#
&update id=&updatePerson& parameterClass=&po.Person&&
update person set name=#name#,sex=#sex# where id=#id#
&select id=&queryById& parameterClass=&int& resultClass=&po.Person&&
select * from person where id=#id#
&select id=&queryAllPerson& cacheModel=&personCache& resultClass=&po.Person&&
select * from person
六:下面最重要的也就是配置applicationContext.xml了
&?xml version=&1.0& encoding=&UTF-8&?&
&beans xmlns=&http://www.springframework.org/schema/beans&
xmlns:xsi=&http://www.w3.org/2001/XMLSchema-instance& xmlns:p=&http://www.springframework.org/schema/p&
xsi:schemaLocation=&http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd&&
&bean id=&dataSource& class=&mons.dbcp.BasicDataSource&&
&property name=&driverClassName& value=&com.mysql.jdbc.Driver& /&
&property name=&url& value=&jdbc:mysql://localhost:3306/ibatis& /&
&property name=&username& value=&root& /&
&property name=&password& value=&1& /&
&bean id=&sqlMapClient& class=&org.springframework.orm.ibatis.SqlMapClientFactoryBean&&
&property name=&configLocation&&
&!-- name 为configLocation或s
不可为其他 --&
&value&SqlMapConfig.xml&/value& &!-- 不区分大小写,路径前可加'/' --&
&/property&
&!-- dataSource不是必需 --&
&property name=&dataSource&&
&ref local=&dataSource& /&
&/property&
&bean id=&personDAO& class=&dao.impl.ActionImpl&&
&!-- dataSource不是必需 --&
&property name=&dataSource&&
&ref local=&dataSource& /&
&/property&
&!-- sqlMapClient必需 --&
&property name=&sqlMapClient&&
&ref local=&sqlMapClient&/&
&/property&
&!-- transactionManager不是必需
&bean id=&transactionManager& class=&org.springframework.jdbc.datasource.DataSourceTransactionManager&&
&property name=&dataSource&&
&ref local=&dataSource& /&
&/property&
注释里面的必需或不是必需都是本人多次试验的,至于为什么是必需不必需 其中的原理我也不是能太讲清楚,在此先是这些写罢了。
里面的每一个节点,属性,如果不太理解,可以上网查一些其他资料。
七:编写测试类
&& 此类利用junit进行测试。只测试了部分功能。
package dao.
import java.util.I
import java.util.L
import org.junit.T
import org.springframework.context.ApplicationC
import org.springframework.context.support.ClassPathXmlApplicationC
import po.P
public class ActionImplTest {
private static ApplicationContext applicationContext =
//提供静态ApplicationContext
applicationContext = new ClassPathXmlApplicationContext(&applicationContext.xml&); //实例化
//添加操作
public void testInsertPerson(){
ActionImpl s = (ActionImpl)applicationContext.getBean(&personDAO&);
s.insertPerson(new Person(1,&zhangsan&,2));
//删除操作
public void testDeletePerson(){
ActionImpl s = (ActionImpl)applicationContext.getBean(&personDAO&);
s.deleteById(1);
//查询全部
public void testQueryAllPerson(){
ActionImpl s = (ActionImpl)applicationContext.getBean(&personDAO&);
List&Person& persons = s.queryAllPerson();
//System.out.println(persons.size());
Iterator&Person& ite = persons.iterator();
while(ite.hasNext()){
Person person = ite.next();
System.out.print(&ID: &+person.getId());
System.out.print(& Name: &+person.getName());
System.out.print(& Sex: &+person.getSex());
System.out.println();
八:如需记录日志 则要log4j.properties
#log4j.rootLogger=DEBUG, stdout
#log4j.appender.stdout=org.apache.log4j.ConsoleAppender
#log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
#log4j.appender.stdout.layout.ConversionPattern=%c{1} - %m%n
#log4j.logger.java.sql.PreparedStatement=DEBUG
log4j.rootLogger=DEBUG, stdout, fileout
#log4j.logger.test=info
#log4j.logger.org.apache.jasper = DEBUG
#log4j.logger.org.apache.catalina.startup.TldConfig = DEBUG
#log4j.logger.org.apache.catalina.session.ManagerBase = DEBUG
.fiscal = DEBUG
.system = DEBUG
.ibatis = DEBUG
.mon.jdbc.SimpleDataSource = DEBUG
.mon.jdbc.ScriptRunner = DEBUG
.ibatis.sqlmap.engine.impl.SqlMapClientDelegate = DEBUG
log4j.logger.java.sql.Connection = DEBUG
log4j.logger.java.sql.Statement = DEBUG
log4j.logger.java.sql.PreparedStatement = DEBUG, fileout
log4j.logger.java.sql.ResultSet = DEBUG
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.fileout=org.apache.log4j.RollingFileAppender
log4j.appender.fileout.File=C\:\\ibatis.log
log4j.appender.fileout.MaxFileSize=10000KB
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=[%-5p] %d{yyyy-MM-dd HH\:mm\:ss} \:%m%n
log4j.appender.fileout.layout=org.apache.log4j.PatternLayout
log4j.appender.fileout.layout.ConversionPattern=[%-5p]_%d{yyyy-MM-dd HH\:mm\:ss} \:%m%n
#log4j.appender.stdout.layout=org.apache.log4j.SimpleLayout
# log4j.logger.org=info
九:已经到最后了,我觉得这最后的才是最最重要的,就是一下jar包问题
&&&我调试了很长时间& 一大部分时间是jar问题
&& 在此列出一下我认为能够跑起来这个小程序所需的一下jar包
&& 如没有,可网上下载。
ibaits-2.3.4.jar&&
spring.jar&&
mysql-connector-java-bin.jar&&
commons-dbcp-1.4.jar&&
commons-pool-1.5.6.jar&&
spring-orm-2.5.6.jar&//已集成到spring.jar里&&
//记录日志所需&&
&log4j-1.2.15.jar&&
commons-logging.jar&
下面是本人的目录结构图
暂无贡献等级
最近登录: 11:12:54
最近从网上收集的java开发常用的工具类,分享给大家。基本满足开发需求.推荐给热爱最代码以及java的牛牛们. & 每个类都有注释的,欢迎大家可以下载使用. &&字符编码:CharTools,base64:Base64 *.javaMd5加密: &MD5*.j...
最近从网上收集的java开发常用的工具类,分享给大家。基本满足开发需求.推荐给热爱最代码以及java的牛牛们. & 每个类都有注释的,欢迎大家可以下载使用. &&
字符编码:CharTools,
base64:Base64 *.java
Md5加密: &MD5*.java
上传:*Uploader*
生成缩略图类:ThumbnailGenerator
时间和日期的工具:DateUtil
字符串:StringHelper,DealString
dom4j工具类:Dom4jHelper.java
Excel导出:ExecHelper
文件操作:File*.java
IP:IPDeal.java
正则表达式应用类:RegExUtil
..........还有好多开发中用到的类,可根据个人需要加入
由编辑于 12:14:44
由编辑于 17:43:35
暂无贡献等级
最近登录: 11:12:54
由最代码官方编辑于 16:53:45...
由编辑于 16:53:45
暂无贡献等级
最近登录: 11:12:54
由最代码官方编辑于 10:06:16...
由编辑于 10:06:16
暂无贡献等级
最近登录: 11:12:54
{代码...}该项目基于:年末最代码部分源码大出血分享-freemarker,bootstrap,springdata jpa分页代码现在各种持久层框架如:hibernate,springdata jpa,mybatis对使用了占位符的sql输出是没有显示完整的sql语句的,所以如果想获取到完整的sql,除了在d...
该项目基于:
现在各种持久层框架如:hibernate,springdata jpa,mybatis对使用了占位符的sql输出是没有显示完整的sql语句的,所以如果想获取到完整的sql,除了在db层做配置外,也有单独在jdbc层做此功能的项目如:。
mysql开启sql的方式:
windows环境下my.ini
log = &c:\log.txt& #所有查询语句
#log-slow-queries=&c:\slow-log.txt& #慢查询
#long_query_time=3 #(3秒) #慢查询时间
linux下的修改my.cnf
general_log_file
= /usr/local/mysql/mysql.log
general_log
修改后必须重启mysql服务才会生效。
log4jdbc执行的截图:
可以看到hibernate的输出占位符为?,而log4jdbc则输出了完整的sql查询
mysql的sql查询输出截图:
log4j需要单独配置log的输出:
log4j.logger.jdbc.sqlonly=OFF
log4j.logger.jdbc.sqltiming=INFO
log4j.logger.jdbc.audit=OFF
log4j.logger.jdbc.resultset=OFF
log4j.logger.jdbc.connection=OFF
jdbc的连接配置如下:
datasource.connection.url=jdbc:log4jdbc:mysql://localhost:3306/zuidaima_log4jdbc?useUnicode=true&characterEncoding=utf-8
需要单独把log4jdbc的jar包加到lib中或者把该jar包加到自己本地的maven仓库中然后添加maven依赖也可以。
参考:/p/log4jdbc/
暂无贡献等级
最近登录: 11:12:54
扫描二维码关注最代码为好友"/>扫描二维码关注最代码为好友}

我要回帖

更多关于 同步会话启动失败 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信