一段代码,感觉很恶心,求优化!!

阿诺 2013-07-19 04:23:16
先说说使用技术:jsp(标签是jstl)+s2sh.
涉及到两个功能:发布信息,修改信息。
发布信息:
公告标题:
公告类型:(三个下拉框,没有级联关系 )
公告内容:
最主要是下拉框这里最繁琐。
下面是Action:
public class NoticeAction extends BaseAction{
private static final long serialVersionUID = 1L;
private TenderAnnounce tenderAnnounce;//模型对象po
@Resource
private INoticeService noticeServiceImpl;//service层
/**
* 转到发布公告页面
点击页面发布公告,转到一个发布公告的页面
*/
public String toPublishNoticePage (){
//获取公告类型初始值
//map里面我存了三个list。就是页面里面三个下拉框的值
Map<String,List> map= noticeServiceImpl.getTACategory();
super.getRequest().setAttribute("map", map);
return "publish_notice_page";
}
//到了页面后,就遍历出来map中三个list的值,初始化下拉框的值。

/**
* 提交公告.提交公告需要校验标题,下拉框,和内容的值是否为空
*
* @return
*/
public String surePublishNotice(){
//下拉框数据:
//获取公告类型初始值
Map<String,List> map= noticeServiceImpl.getTACategory();
super.getRequest().setAttribute("map", map);

//获取页面选择的下拉框的值
String listOne=super.getRequest().getParameter("listOne");
String listTwo=super.getRequest().getParameter("listTwo");
String listThree=super.getRequest().getParameter("listThree");
//分别放入作用域,当校验不通过时,传回页面做回显
super.getRequest().setAttribute("listOne", listOne);
super.getRequest().setAttribute("listTwo", listTwo);
super.getRequest().setAttribute("listThree", listThree);

//首先校验字段
if(null==tenderAnnounce.getTitle()||"".equals(Commons.isNull(tenderAnnounce.getTitle()))){//Commons.isNull是工具类,是空白字符的替换后是否为空
super.getRequest().setAttribute("noticeTitleIsNotNull", "标题不不能为空");
//如果标题为空,就回到发布页面,显示错误信息
return "publish_notice_page";
}

if("-1".equals(listOne) || "-1".equals(listTwo) || "-1".equals(listThree)){
super.getRequest().setAttribute("noticeTypeIsNotNull", "请选择类型");
//三个下拉框做一次校验。有一个没有选择都不通过
return "publish_notice_page";
}

if(null==tenderAnnounce.getContent()||"".equals(Commons.isNull(tenderAnnounce.getContent()))){
super.getRequest().setAttribute("noticeContentIsNotNull", "内容不能为空");
return "publish_notice_page";
}

//保存信息 如果校验通过,将信息设置到po中调用service方法,插入到数据库去
tenderAnnounce.setUserid(sysUser.getId());
tenderAnnounce.setTitle(tenderAnnounce.getTitle().trim());
tenderAnnounce.setContent(tenderAnnounce.getContent());
tenderAnnounce.setCompanyid(sysUser.getCompanyid());
tenderAnnounce.setUsername(sysUser.getUsername());
tenderAnnounce.setCatalogid(Commons.stringToInt(listOne));
tenderAnnounce.setChildid(Commons.stringToInt(listTwo));
tenderAnnounce.setGrandid(Commons.stringToInt(listThree));
noticeServiceImpl.savePblishNotice(tenderAnnounce);
}
页面部分如下:
<!--第一个下拉框-->
<select name="listOne">
<option value="-1">--请选择--</option>
<c:forEach items="${map['listOne']}" var="obj">
<!--将遍历出来的值,和选择的值(后台作用域中保存的选择值)做比较,做回显。-->
<c:if test="${requestScope.listOne eq obj.id}">
<option value="${obj.id}" selected="selected">${obj.catalogname }</option>
</c:if>
<c:if test="${requestScope.listOne ne obj.id}">
<option value="${obj.id}">${obj.catalogname }</option>
</c:if>
</c:forEach>
</select>
<!--第二个下拉框-->
<select name="listTwo">
<option value="-1">--请选择--</option>
<c:forEach items="${map['listTwo']}" var="obj">
<c:if test="${requestScope.listTwo eq obj.id}">
<option value="${obj.id}" selected="selected">${obj.catalogname }</option>
</c:if>
<c:if test="${requestScope.listTwo ne obj.id}">
<option value="${obj.id}">${obj.catalogname }</option>
</c:if>
</c:forEach>
</select>

<!--第三个下拉框-->
<select name="listThree">
<option value="-1">--请选择--</option>
<c:forEach items="${map['listThree']}" var="obj">
<c:if test="${requestScope.listThree eq obj.id}">
<option value="${obj.id}" selected="selected">${obj.catalogname }</option>
</c:if>
<c:if test="${requestScope.listThree ne obj.id}">
<option value="${obj.id}">${obj.catalogname }</option>
</c:if>
</c:forEach>
</select>

这是保存,当保存后,我们可以对其进行修改,修改的页面都一样。当进入到修改页面时,自然要显示出来该信息的标题,类型,内容这些。
修改的action代码如下(保存和修改在同一个Action中):
/**
* 转到修改公告信息页面
* @return
*/
public String toupdate(){
//获取公告类型初始值 当到修改页面是填充下拉框值
Map<String,List> map= noticeServiceImpl.getTACategory();
super.getRequest().setAttribute("map", map);
//根据id查询信息对象信息,显示到修改页面上去
tenderAnnounce=noticeServiceImpl.getObjFromId(tenderAnnounce.getId());
//将三个下拉框的id值(数据库中存的是对应的id.)设置到作用域。以便于,在修改页面显示
//这个信息之前保存时,所选择的下拉框的值。因为三个下拉框的name分别叫:listOne,
//listTwo,listThree.
super.getRequest().setAttribute("listOne", tenderAnnounce.getCatalogid());
super.getRequest().setAttribute("listTwo", tenderAnnounce.getChildid());
super.getRequest().setAttribute("listThree", tenderAnnounce.getGrandid());
return "update_notice_page";
}
//用户在修改页面修改后,点击确认修改,同样要校验字段。
/**
* 确认修改
* @return
*/
public String updateNotice(){
//下拉框数据:
//获取公告类型初始值
Map<String,List> map= noticeServiceImpl.getTACategory();
super.getRequest().setAttribute("map", map);
//获取用户选择的三个下拉框的值
String listOne=super.getRequest().getParameter("listOne");
String listTwo=super.getRequest().getParameter("listTwo");
String listThree=super.getRequest().getParameter("listThree");
//放入作用域,便于校验未通过时传回页面做回显示
super.getRequest().setAttribute("listOne", listOne);
super.getRequest().setAttribute("listTwo", listTwo);
super.getRequest().setAttribute("listThree", listThree);

//首先校验字段
if(null==tenderAnnounce.getTitle()||"".equals(Commons.isNull(tenderAnnounce.getTitle()))){
super.getRequest().setAttribute("noticeTitleIsNotNull", "标题不可为空");
return "publish_notice_page";
}



if("-1".equals(listOne) || "-1".equals(listTwo) || "-1".equals(listThree)){
super.getRequest().setAttribute("noticeTypeIsNotNull", "请选择类型");
return "publish_notice_page";
}

if(null==tenderAnnounce.getContent()||"".equals(Commons.isNull(tenderAnnounce.getContent()))){
super.getRequest().setAttribute("noticeContentIsNotNull", "内容不能为空");
return "publish_notice_page";
}
//校验听过执行修改。
noticeServiceImpl.update(tenderAnnounce);
return "toNoticeManage";
}

代码看上去有点多,其实思路不复杂,就是要保存数据,有三个下拉框,然后要校验字段。当校验没通过时,之前填写的和选择的数据要回显到页面。(一般开发都是这个思路吧...)
然后就是保存和修改有许多代码重复的地方,我实在是不知道怎么抽取,因为这是struts2...不好弄...
最后我贴一下service:
//获取下拉框的值,并且将数据分为三类。(三个下拉框使用。)
public Map<String, List> getTACategory() {
Map<String, List> map= new HashMap<String, List>();
List list=tenderAnnounceDao.getTACategory();
Iterator<TenderAnnounceCategory> iterator=list.iterator();
List<TenderAnnounceCategory> listOne=new ArrayList<TenderAnnounceCategory>();
List<TenderAnnounceCategory> listTwo=new ArrayList<TenderAnnounceCategory>();
List<TenderAnnounceCategory> listThree=new ArrayList<TenderAnnounceCategory>();
while(iterator.hasNext()){
TenderAnnounceCategory announceCategory=iterator.next();
switch (announceCategory.getLayer()) {
case 1:
listOne.add(announceCategory);
break;
case 2:
listTwo.add(announceCategory);
break;
case 3:
listThree.add(announceCategory);
break;

}
}
map.put("listOne", listOne);
map.put("listTwo", listTwo);
map.put("listThree", listThree);
return map;
}
数据库类型的数据格式是这样的:
id layer
1 1
2 1
3 1
4 2
5 2
6 3
7 3
我上面的service就是在根据 layer分类(1,2,3各一类)
求高手,大哥大姐大叔大婶,叔叔阿姨弟弟妹妹。帮忙优化下action的代码啊....



...全文
626 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
caiselangren 2013-07-24
  • 打赏
  • 举报
回复
好长啊
ZZZ5512536 2013-07-23
  • 打赏
  • 举报
回复
额,再怎么也要用专门贴代码的文本贴出来啊。。这样看着太乱,就没心情了
Bumpking 2013-07-23
  • 打赏
  • 举报
回复
ctrl+shift+f 嗯 。。。只能帮你到这啦 。。。
Ciffer 2013-07-23
  • 打赏
  • 举报
回复
恶心的代码,谁都不想看的 去看一下“重构”吧
清风v明月 2013-07-22
  • 打赏
  • 举报
回复
zqf3940 2013-07-22
  • 打赏
  • 举报
回复
太长看不下去
阿诺 2013-07-22
  • 打赏
  • 举报
回复
引用 4 楼 海龙 的回复:
你觉得哪看着不舒服?
保存和修改的地方那么多重复的代码,我试着把他们抽取出放到一个方法中,但是又达不到相要的效果。
阿诺 2013-07-22
  • 打赏
  • 举报
回复
55555555 我看着怎么那么多重复的代码呀!由于是struts2又不知道怎么把他们抽取出来。我是不是写错地方了,那些重复的代码是不是该在service层哦 我怀疑...嘿嘿
安小菜 2013-07-21
  • 打赏
  • 举报
回复
优化就是你自己看哪里需要的留下不需要的删掉。哪里代码相似部分多哪里遍改改。或者说你需要怎么优化,怎么做。
AlexChowKey 2013-07-19
  • 打赏
  • 举报
回复
代码优化还是得自己来, 看哪里不爽,删哪里,哪里不需要就干掉, 在代码正常运行的情况下,把它优化到最精简。
oh_Maxy 2013-07-19
  • 打赏
  • 举报
回复
注释还算多,可以在if等判断、循环处,增加注释,说明含义。
我的波塞冬 2013-07-19
  • 打赏
  • 举报
回复
你觉得哪看着不舒服?
jiafeng_lee 2013-07-19
  • 打赏
  • 举报
回复
代码优化还是自己弄吧
阿诺 2013-07-19
  • 打赏
  • 举报
回复
来了就说几句呀!怎么优化一下。。。静下心,,,等.........
小丑哥_V5 2013-07-19
  • 打赏
  • 举报
回复

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧