您的位置:首页 > 编程语言 > Java开发

Struts2 action中不要将方法以get开头!!!

2016-04-06 21:22 483 查看


Struts2 action中不要将方法以get开头


Struts2 action中不要将方法以get开头


Struts2 action中不要将方法以get开头

重要的事情说三遍!

今天调试jquery发送请求到action中我写的三个方法回一次执行。审查代码一点问题都没有

我写的代码如下:

/**
*
*/
package com.artpri.artist.action;

import java.util.List;

import com.opensymphony.xwork2.Action;

/**
*
* @author 王校兵
* @version 1.0, 2016年4月6日
*/
public class GetQueryAlbumPhotoAction extends BaseAction {

private List photoList;// 照片的查询结果集合
private String queryTime;// 要查询照片的拍摄时间条件
private String queryCategory;// 要查询的年代条件
private String queryTitle;// 要查询的标题条件

/**
* 通过相册分类查询相册照片action方法
* @return 页面跳转对应的逻辑代码
* */
public String getPhotoByCategory() {
System.out.println("getPhotoByCategory-------------------------------" + queryCategory);
photoList = getAlbumPhotoMangeService().getPhotoByCategory(queryCategory);
return Action.SUCCESS;
}

/**
* 通过拍摄时间查询相册照片action方法
* @return 页面跳转对应的逻辑代码
* */
public String getPhotoByTime() {
System.out.println("getPhotoByTime-------------------------------" + queryTime);
photoList = getAlbumPhotoMangeService().getPhotoByTime(queryTime);
return Action.SUCCESS;
}

/**
* 通过照片标题查询相册照片action方法
* @return 页面跳转对应的逻辑代码
* */
public String getPhotoByTitle() {
System.out.println("getPhotoByTitle-------------------------------" + queryTitle);
photoList = getAlbumPhotoMangeService().getPhotoByTitle(queryTitle);
return Action.SUCCESS;
}

public List getPhotoList() {
return photoList;
}

public String getQueryCategory() {
return queryCategory;
}

public String getQueryTime() {
return queryTime;
}

public String getQueryTitle() {
return queryTitle;
}

public void setPhotoList(List photoList) {
this.photoList = photoList;
}

public void setQueryCategory(String queryCategory) {
this.queryCategory = queryCategory;
}

public void setQueryTime(String queryTime) {
this.queryTime = queryTime;
}

public void setQueryTitle(String queryTitle) {
this.queryTitle = queryTitle;
}

}


他们会依次执行。因为action中 ajax模式下,调用的action方法不能为get*方式命名,内中机理未知。

于是就把get开头的方法修改了一下,程序就运行正常了,继续找了下原因,最后基本上明白。
个人认为,出现这个问题的原因是因为,action中属性都是以get set方式设置的,这样strut2才能根据反射进行设置,但是当action结束的时候,
就会调用相应的get方法取值,所以导致get开头的method执行了两次。

所以


Struts2 action中不要将方法以get开头

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  jquery struts2.0