j2ee(SSH)项目开发中的代码小结(四)

 一)在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类的代码如下:

package  com.bsh.ieaa.web.struts.base;

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);
        }

    }


}
BeanLocator类的代码片断:
public   class  BeanLocator
{
    
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判断,是否需要登陆验证。代码片断:

    @Override
    
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) <= 0return true; }
        
return false;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值