Struts2中的Action类实现ModelDriven接口的问题!

J2EE 码拜 9年前 (2015-05-10) 850次浏览 0个评论
 

刚使用Struts2做项目,就遇到了一系列的问题,最蛋疼的主要是以下几个问题,求各位大侠帮忙看看,先谢谢了!
1、每次执行Action类中的某个方法都执行两次getModel方法;
2、做更新功能时,第一次跳转到更新页面的是数据全是空白的,第二次跳转就是显示之前那条数据的信息,上网查了以下,说将“ModelDrivenInterceptor提供了一个配置参数:refreshModelBeforeResult,只要将它定义为true,上述问题就被解决了!”请问是在Struts.xml中配置吗?
我的struts.xml配置如下,但是不管用,求解!

<package name=”news” extends=”struts-default”>
<interceptors>
<interceptor-stack name=”myDefaultStack”>
<interceptor-ref name=”defaultStack”>
<param
name=”refreshModelBeforeResult”>
true
</param>
</interceptor-ref>
</interceptor-stack>
</interceptors>
<default-interceptor-ref name=”myDefaultStack” />

<!–获取所有新闻信息–>
<action name=”showAllNews” class=”NewsAction”
method=”showAllNewInfo”>
<param name=”refreshModelBeforeResult”>true</param>
<result name=”success”>
./back/news/showAllNewsInfo.jsp
</result>
</action>

高手都去哪了啊,自己顶一个
40分
水平有限,看不太懂你的问题!能把代码弄来看看吗
引用 3 楼  的回复:

水平有限,看不太懂你的问题!能把代码弄来看看吗

可以的
Action类的代码:

public class NewsAction extends BaseAction implements ModelDriven<NewsDTO> {

private NewsServiceImpl newsService; // 新闻业务层接口实现类

private NewsDTO newsInfo = new NewsDTO(); // 实例化新闻信息类

/**
 * 获取所有新闻信息
 * 
 * @return
 * @throws Exception
 */
public String showAllNewInfo() throws Exception {
if (null != newsInfo) {
String title = newsInfo.getTitle();
String newType = newsInfo.getNewtype();
if (null != title)
newsInfo.setTitle(title);
if (newType != null)
newsInfo.setNewtype(newType);
}

pageInfo.setPageNo(pageNo); // 设置页码
newsService.getAllNewsByPage(newsInfo, pageInfo);

// 将所有新闻信息赋给“newsList”变量
newsList = pageInfo.getList();

pageMap.put(“pageNo”, pageInfo.getPageNo());
pageMap.put(“pageSize”, pageInfo.getPageSize());
pageMap.put(“pageTotal”, pageInfo.getPageTotal());
pageMap.put(“recTotal”, pageInfo.getRecTotal());

return this.SUCCESS;
}

/**
 * 添加新闻信息
 * 
 * @return
 * @throws Exception
 */
public String doInsertNewInfo() throws Exception {
String newCode = randomNum.getTimeStamp(“N_”);
long readNum = 1;
newsInfo.setNewcode(newCode);
newsInfo.setPublishtime(new Date());
newsInfo.setReadnumber(readNum);
boolean flag = newsService.doInsertNews(newsInfo);
if (flag) {
newsInfo.setTitle(null);
newsInfo.setNewtype(null);
messages = “信息添加成功!”;
return this.showAllNewInfo();
} else {
messages = “抱歉,信息添加失败!请确认必填项是否已经填写!”;
return this.INPUT;
}
}

/**
 * 根据新闻编号删除新闻信息
 * 
 * @return
 * @throws Exception
 */
public String doDelNewInfo() throws Exception {
// 获取新闻编号
String newsCode = request.getParameter(“code”);
// 调用删除方法
boolean flag = newsService.doDelNewsByCode(newsCode);
// 根据“flag”的返回值给出相应的提示信息
if (flag) {
messages = “信息已成功删除!”;
} else {
messages = “抱歉,信息删除失败!”;
}
return this.showAllNewInfo();
}

/**
 * 更新新闻信息方法
 * 
 * @return
 * @throws Exception
 */
public String doModifyNewInfo() throws Exception {

return this.SUCCESS;
}

        /**
 * 根据新闻编号查询新闻信息方法
 * 
 * @return
 * @throws Exception
 */
public String showNewInfo() throws Exception {
// 获取新闻编号
String newsCode = request.getParameter(“code”);
// 调用获取新闻实体信息方法
newsInfo = newsService.getNewsInfoByCode(newsCode);
// 跳转到更新页面
return “to_updateNewsInfo”;
}

public NewsDTO getModel() {
if (newsInfo != null) {
newsInfo = new NewsDTO();
}
return newsInfo;
}

public NewsDTO getNewsInfo() {
return newsInfo;
}

public void setNewsInfo(NewsDTO newsInfo) {
this.newsInfo = newsInfo;
}

public void setNewsService(NewsServiceImpl newsService) {
this.newsService = newsService;
}
}

struts.xml的代码

<package name=”news” extends=”struts-default”>

<!–获取所有新闻信息–>
<action name=”showAllNews” class=”NewsAction”
method=”showAllNewInfo”>
<result name=”success”>
./back/news/showAllNewsInfo.jsp
</result>
</action>

<!–查询新闻信息–>
<action name=”getNewInfo” class=”NewsAction”
method=”showNewInfo”>
<result name=”to_selNewsInfo”>
/back/news/showNewsInfo.jsp
</result>
<result name=”to_updateNewsInfo”>
/back/news/doModifyNews.jsp
</result>
</action>

<!–添加新闻信息–>
<action name=”addNews” class=”NewsAction”
method=”doInsertNewInfo”>
<result name=”success”>
../../back/news/showAllNewsInfo.jsp
</result>
<result name=”input”>/back/news/doAddNews.jsp</result>
</action>

<!–删除新闻信息–>
<action name=”delNews” class=”NewsAction”
method=”doDelNewInfo”>
<result name=”success”>
./back/news/showAllNewsInfo.jsp
</result>
</action>

<!–更新新闻信息–>
<action name=”updateNews” class=”NewsAction”
method=”doModifyNewInfo”>
<param name=”refreshModelBeforeResult”>true</param>
                        <result name=”success”>
../../back/news/showAllNewsInfo.jsp
</result>
<result name=”input”>/back/news/doModifyNews.jsp</result>
</action>

</package>
</struts>

上面代码中getModel()方法中代码有误:
if(newsinfo != null){
   ….
}
应改为:
if(newsinfo == null){
   ….
}
估计楼主早就解决了问题了,但是这儿却没有答案。。。。
我也遇到了同样的问题,查了下,好像是需要楼主在struts.xml中的refreshModelBeforeResult改为modelDriven.refreshModelBeforeResult。形如:

<interceptor-ref name="defaultStack">
					<param name="modelDriven.refreshModelBeforeResult">true</param>
				</interceptor-ref>

应该就可以了

如果还没看懂的童鞋,可以看下这里:
http://unmi.cc/struts2-how-to-override-interceptor-parameters

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明Struts2中的Action类实现ModelDriven接口的问题!
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!