Struts2.1.8使用心得

这次的项目用到最新的Struts2.1.8,在用的过程中遇到了很多问题,在此一一记录。
[color=blue][b]一、jsonplugin0.34的问题[/b][/color]
jsonplugin0.34在struts2.1.6后就有问题了,其核心原因是xwork在2.1.2版本后将com.opensymphony.xwork2.util.TextUtils去掉了,解决办法将这个类重新考到jsonplugin包里。

[color=blue][b]二、Struts2标签问题[/b][/color]
个人对Struts标签实在不敢恭维,实在难用。在使用了simple主题后情况有所改观。不过,Struts2的flt模板也存在问题。比如checkboxlist:

The java code is :
<table border="0" cellspacing="1" width="100%" cellpadding="0" class="contextRow">
<s:checkboxlist cssClass="contextFieldData" name="delValueList" list="descList" listKey="tag_value_id" listValue="valueName" />
</table>

The rendered HTML code is:

<table border="0" cellspacing="1" width="100%" cellpadding="0" class="contextRow">
<tr>
<td class="tdLabel"></td>
<td>
<input type="checkbox" name="delValueList" value="1311" id="delValueList-1"/>
<label for="delValueList-1" class="checkboxLabel">16AP</label>
<input type="checkbox" name="delValueList" value="294" id="delValueList-2"/>
<label for="delValueList-2" class="checkboxLabel">ADRR</label>
<input type="checkbox" name="delValueList" value="1312" id="delValueList-3"/>
<label for="delValueList-3" class="checkboxLabel">AP16</label>
<input type="checkbox" name="delValueList" value="296" id="delValueList-4"/>
<label for="delValueList-4" class="checkboxLabel">BAT</label>
<input type="checkbox" name="delValueList" value="295" id="delValueList-5"/>
<label for="delValueList-5" class="checkboxLabel">CARP</label>
<input type="checkbox" name="delValueList" value="248" id="delValueList-6"/>
<label for="delValueList-6" class="checkboxLabel">EXER</label>
</td>
</tr>
</table>

看见没,cssClass根本没显示,看了checkboxlist.ftl发现就是如此,在文件中添加如下部分就可以了

<#if parameters.cssClass??>
class="${parameters.cssClass?html}"<#rt/>
</#if>


[color=blue][b]三、JFreeChart应用[/b][/color]
由于这次项目需要有各种JFreeChart图输出,综合考虑采用如下方式[img]http://dl.iteye.com/upload/attachment/180135/baa05626-7fb5-3f06-a0a1-c41849464f7d.jpg[/img]
1.BaseAction是个基类,里面有常用的getSession()等的方法
2.JFreeChartChart是抽象基类,实现了JFreeChart各种图形的实现方式,提供getData()抽象方法用于子类实现
3.ExitChartAction其实是一个出口基类,通过传递图类型参数确定转向哪些子类
4.众多JFreeChartChart子类,实现getData()方法提供JFreeChart图形数据。

[color=blue][b]四、拦截器--日志管理[/b][/color]
借鉴了网上一位仁兄的方法,具体在哪儿看到的记不清楚了,不过还是非常感谢这位仁兄。

<interceptors>
<interceptor name="myInterceptor"
class="com.chiyu.action.admin.log.LoggerInterceptor">
</interceptor>
<interceptor-stack name="myStack">
<interceptor-ref name="myInterceptor"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="myStack">
</default-interceptor-ref>



...导入包略
public class LoggerInterceptor extends AbstractInterceptor {
private IAdminOperationLogService adminOperationLogService;
private IAdminService adminService;
private static final long serialVersionUID = 1358600090729208361L;

@SuppressWarnings("unchecked")
@Override
public String intercept(ActionInvocation invocation) throws Exception {
System.out.println("日志拦截器已经开始启动..");
String actionName = invocation.getAction().getClass().toString();
invocation.invoke();
final Map<String, Object> parameters = invocation
.getInvocationContext().getParameters();
boolean result = invocation.getProxy().getExecuteResult();
String method = invocation.getProxy().getMethod();
// 取得请求相关的ActionContext实例
ActionContext ctx = invocation.getInvocationContext();

HttpServletRequest request = ServletActionContext.getRequest();
String ip = request.getRemoteAddr();
Map session = ctx.getSession();
// 取出名为user的session属性
String name = (String) session.get(Admin.ADMIN_KEY);
Admin admin = adminService.findAdminByAdminName(name);
System.out.println(admin);
if (admin != null) {
Adminoperationlog adminLog = new Adminoperationlog();
adminLog.setAdminId(admin.getAdminId());
adminLog.setAdminName(name);
adminLog.setDealTime(new java.sql.Date(System.currentTimeMillis()));
adminLog.setOperationIp(ip);
adminLog.setOperationSource(actionName + "---" + method + "---"
+ parameters + "---" + result);
adminOperationLogService.save(adminLog);
}
return Action.SUCCESS;
}

setter和getter略
}


[color=blue][b]五、OGNL语言[/b][/color]
关于OGNL的概念可以google一下,我主要记录一些常用的用法
1.action中的某属性,有setter和getter,这种属性是最好表示的。其实就是request.getAttribute("xxx")或者request.getParameter("xxx");
表达方式${xxx},有人说javascript可以引用吗?答案是可以的

var a = ${xxx};//可以获得值

那如果在struts2标签里呢?也好说,这里举一个页面跳转的例子

<s:action name="xxx" namespace="/xxx" executeResult="true">
<s:param name="pictureType" value="%{xxx}"></s:param>
</s:action>

2.<s:if test="" />中的判断条件,假设有个属性叫xxx
在test里可以写成xxx == 1或者%{#xxx == 1},但有一点要注意,如果是和字符串比较
xxx == 'a'这样是不行的一定要双引号xxx == "a"
3.其实还有很多应用,我在这就不一一列举了,大家可以一块商讨,下面附上[color=red]OGNL参考手册[/color]奉献给大家。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值