一)在JSP页面中使用相对路径时是针对当前action的,如果在开发中加入像下面这样的语句:
<link href="../../../../../../images/skin1/css/ieaa/basestyle.css" rel="stylesheet" type="text/css">
document.location = '../../xxx.do';
需要在head中加入struts标签<html:base />,这句话表示当前页面的相对路径都是以当前页面的地址为准的。如果没有这句话,如果项目中有不同路径下的多个action跳转到此页面,就会发生页面不存在的错误提示!
二)有时候在使用spring时,不想通过依赖注入来创建对象。可以这样,首先创建一个plug in类,继承自struts的PlugIn类,然后在struts-config.xml中加入插件配置
<plug-in className="com.qk.ApplicationLoaderPlugIn"/>由自定义的插件类ApplicationLoaderPlugIn来接管。ApplicationLoaderPlugIn类的代码如下:
import javax.servlet.ServletException;
import org.apache.struts.action.ActionServlet;
import org.apache.struts.action.PlugIn;
import org.apache.struts.config.ModuleConfig;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import com.qk;
public class ApplicationLoaderPlugIn implements PlugIn
... {
private WebApplicationContext wac = null;
public void destroy()
...{
wac = null;
}
public void init(ActionServlet arg0, ModuleConfig arg1) throws ServletException
...{
if (wac == null)
...{
wac = WebApplicationContextUtils.getRequiredWebApplicationContext(arg0.getServletContext());
BeanLocator.init(wac);
}
}
}
... {
private static WebApplicationContext webApplicationContext = null;
public synchronized static void init(WebApplicationContext wac)
...{
if (webApplicationContext == null) webApplicationContext = wac;
}
public static Object getBean(String beanName)
...{
return webApplicationContext.getBean(beanName);
}
}
这样就可以以这种方式获取你需要的Bean:
BeanLocator.getBean("xxxFacade");
三)登陆时,有些action是不需要进行登陆验证的,比如登陆action。此时首先将struts-config.xml中的控制器controller对应的spring controller类换成自己的controller---com.qk.AppRequestProcessor,并且此controller继承自spring controller类---DelegatingRequestProcessor。覆写方法processActionCreate,也就是在创建action之前进行action判断,是否需要登陆验证。代码片断:
protected Action processActionCreate(HttpServletRequest arg0, HttpServletResponse arg1, ActionMapping arg2)
throws IOException
... {
// 如果不是登录Action,则验证当前用户是否登录,没有登录则跳转到错误页面
String[] strLogin = StringUtil.delimitedListToStringArray(AppConstants.LOGIN_ACTION_LIST, ",");
String[] strRegister = StringUtil.delimitedListToStringArray(AppConstants.REGISTER_ACTION_LIST, ",");
String[] newArray = StringUtil.concatenateStringArrays(strLogin, strRegister);
String actionBeanName = determineActionBeanName(arg2);
boolean isContain = false;
for (int i = 0; i < newArray.length; i++)
...{
if (actionBeanName.equalsIgnoreCase(newArray[i]))
...{
isContain = true;
break;
}
}
if (!isContain)
...{
HttpSession session = arg0.getSession(false);
if (session == null || StringUtil.isNull(session.getAttribute(AppConstants.LOGIN_USER)))
...{
arg1.sendRedirect(arg0.getContextPath() + moduleConfig.findForwardConfig("timeout").getPath());
return null;
}
}
return super.processActionCreate(arg0, arg1, arg2);
}
}
其中determineActionBeanName方法是spring提供的方法,根据当前传入的actionMap获取对应的actionName,然后判断此actionName是否需要进行登陆验证,如果需要则判断session中是否已经登陆了,不需要则创建对应的action类。
四)一个有用的日期时间判断函数
* 一个时分秒的时间段是否符合一个日期时间段的情况(包含时间交叉的情况)
*/
public static boolean isInTimestamp(Date startTime, Date endTime, Date sourStartDate, Date sourEndDate)
... {
if (sourStartDate.compareTo(startTime) <= 0 && sourEndDate.compareTo(startTime) > 0
|| sourStartDate.compareTo(endTime) < 0 && sourEndDate.compareTo(endTime) >= 0
|| sourStartDate.compareTo(startTime) >= 0 && sourEndDate.compareTo(endTime) <= 0) ...{ return true; }
return false;
}