struts2 学习记录

1、在jsp中加上<s:debug/>会有意想不到的结果;

如果加入了struts2-dojo-plugin.***.jar在浏览器用get方式传debug=console时会弹出一个调试控制台,IE无效;

如果加入了struts2-config-browser-plugin后,在浏览器用get方式传debug=browser之后,也会出现一种调试的页面。

最简单的还是<s:debug/>。别忘了在struts的配置文件中开启调试模式

<constant name="struts.devMode" value="true" />
开发时最好开了下面这行,这样配置文件改了也不用一边一边的重启服务器。
<constant name="struts.configuration.xml.reload" value="true" />


2、自定义拦截器时,不要忘记加上默认的defaultStack,可以继承自AbstractInterceptor,这样就没有init和destory了

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
	<package name="auth" extends="struts-default">
		<interceptors>
			<interceptor name="timeIntercepter" class="interceptors.ExecuteTimeInterceptor"></interceptor>
		</interceptors>
		<action name="login" class="com.neusoft.erp.action.LoginAction"
			method="login">
			<interceptor-ref name="defaultStack"></interceptor-ref>
			<interceptor-ref name="timeIntercepter"></interceptor-ref>
			<result name="success">/customer/main.jsp</result>
			<result name="error">/login.jsp</result>
		</action>
		<action name="logout" class="com.neusoft.erp.action.LogoutAction"
			method="logout">
			<result name="success">/customer/main.jsp</result>
		</action>
	</package>
</struts>    

3、action中需要session,requesht,application时,实现SessionAware等对应的接口,不要使用servletActionContext之类的与servlet耦合的方式

4、jsp中最好不用jsp的EL ${},这个符号一般用在struts配置文件中(result中get传值之类的),例如,下面的username和password就不好

<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<title>客户管理</title>

		<meta http-equiv="pragma" content="no-cache">
		<meta http-equiv="cache-control" content="no-cache">
		<meta http-equiv="expires" content="0">


	</head>

	<body>

		客户管理:
		<span>${user.username}</span>-
		<span>${user.password }</span>-
		<span>在线人数:<s:property value="#application['login_numbers']" />
		</span>
		<br>
		<a href="<%=basePath%>customer/main.action?fun=add">增加</a>
		<a href="<%=basePath%>customer/main.action?fun=modify">修改</a>
		<a href="<%=basePath%>customer/main.action?fun=delete">删除</a>
		<a href="<%=basePath%>login.jsp">登陆</a>
		<a href="<%=basePath%>logout.action">注销</a>
		<div>
			<s:iterator value="#application['users']">
				<span><s:property value="username" /> </span>
				<br />
			</s:iterator>
		</div>
		<s:debug></s:debug>
	</body>
</html>

OGNL中,在值栈里面的数据不需要#,其他域的数据(session,request,paramator,application,attr)中需要,而且,如果给某个属性传值的时候传的是一个OGNL,如果这个属性本来就是string,需要加%,如下面第一行,value的值会是字面的‘#obj['attr']’,而不是取出里面代表的具体值

<s:url value="#obj['attr']"/>
<s:url value="%{#obj['attr']}"/>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值