可以的
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>