一种方法你的每一个jsp都对应一个action类,访问路径在struts-config里配置好,在界面上不要让用户看到任何jsp文件的链接,都应该是.do的,你的用户验证就在action里做。
但是为了防止有些“非法用户”直接找到jsp文件的路径进行访问,我们不得不在jsp里再做一次验证,这个方法struts的例子里有,使用自定义标记。
另一种是把所有的jsp文件都移到WEB-INF目录下,这样用户是无法通过在浏览器地址栏输入url进行访问的,必须要通过struts的return mapping.findForward("success");success在配置文件里绑定一个jsp页面,进行访问,这样就可以不在jsp页面进行权限验证,只在action里验证就可以了。
这种访问方式目前只有tomcat4或5,jboss,weblogic8提供。像weblogic7就不能用struts来访问WEB-INF下的内容。
但是为了防止有些“非法用户”直接找到jsp文件的路径进行访问,我们不得不在jsp里再做一次验证,这个方法struts的例子里有,使用自定义标记。
另一种是把所有的jsp文件都移到WEB-INF目录下,这样用户是无法通过在浏览器地址栏输入url进行访问的,必须要通过struts的return mapping.findForward("success");success在配置文件里绑定一个jsp页面,进行访问,这样就可以不在jsp页面进行权限验证,只在action里验证就可以了。
这种访问方式目前只有tomcat4或5,jboss,weblogic8提供。像weblogic7就不能用struts来访问WEB-INF下的内容。
public abstract class ActionBase extends Action {
public final String WEB_INF = "/WEB-INF/";
public ActionBase() {
} public ActionForward execute(ActionMapping mapping,ActionForm form,
HttpServletRequest request,HttpServletResponse response)
throws Exception
{
String strforward = CommonStants.ACTION_FORWARD_SUCCESS;
HttpSession session = request.getSession();
try{
if (null == StaticStants.getProp()) {
//查找web-inf的路径
ServletContext context = servlet.getServletContext();
String defaultroot = context.getRealPath("/");
String configroot = defaultroot + WEB_INF + CommonStants.SYSTEM_CONFIG_FILE; ParseXML xml = new ParseXML();
//解析xml文件开始
//parse(xml的文件路径)
xml.parse(configroot);
//解析完成 //设置配置信息到Properties
prop = xml.getProps();
//设置到全局变量
StaticStants.setProp(prop);
if (prop != null && !prop.isEmpty())
{
//如果有数据库信息,设置
if(prop.getProperty(CommonStants.DRIVER_NAME) != null &&
prop.getProperty(CommonStants.HOST_URL) != null &&
prop.getProperty(CommonStants.USER_NAME) != null &&
prop.getProperty(CommonStants.USER_PWD) != null) { //设置数据库连接信息
DataBaseManager.setConnection(prop.getProperty(CommonStants.DRIVER_NAME),
prop.getProperty(CommonStants.HOST_URL),
prop.getProperty(CommonStants.USER_NAME),
prop.getProperty(CommonStants.USER_PWD));
}
if (prop.getProperty(CommonStants.AUTO_COMMIT) != null) {
DataBaseManager.setAutoCommit(prop.getProperty(CommonStants.AUTO_COMMIT));
}
//init count
if (prop.getProperty(CommonStants.CONNECT_INITCOUNT) != null) {
DataBaseManager.setInitCount(prop.getProperty(CommonStants.CONNECT_INITCOUNT));
}
//max count
if (prop.getProperty(CommonStants.CONNECT_MAXCOUNT) != null) {
DataBaseManager.setMaxCount(prop.getProperty(CommonStants.CONNECT_MAXCOUNT));
}
//timeout
if (prop.getProperty(CommonStants.CONNECT_TIMEOUT) != null) {
DataBaseManager.setTimeOut(prop.getProperty(CommonStants.CONNECT_TIMEOUT));
}
if (prop.getProperty(CommonStants.ERR_MSG_FILEURL) != null) {
//设置错误文件路径
String errmsgroot = defaultroot + WEB_INF + prop.getProperty(CommonStants.ERR_MSG_FILEURL);
StaticStants.setErrMsgUrl(errmsgroot);
}
}
}
Vector validevec_= doValidate(mapping,form,request,response);
//clear err msg
if (session.getAttribute(LsConstants.ACTION_ERROR) != null) {
session.removeAttribute(LsConstants.ACTION_ERROR);
}
//clear end
if ( validevec_.size() > 0 ) {
session.setAttribute(LsConstants.ACTION_ERROR,validevec_);
strforward = CommonStants.ACTION_FORWARD_ERROR;
} else {
ActionForward forward = doMainAction(mapping,form,request,response);
strforward = forward.getName();
}
doPostAction(mapping,form,request,response);
} catch(Exception e){
strforward = CommonStants.ACTION_FORWARD_ERROR;
}
return (mapping.findForward(strforward));
}
/**
*数据验证
*/
public Vector doValidate(ActionMapping mapping,ActionForm form,
HttpServletRequest request,HttpServletResponse response){
return (new Vector());
}
/**
*主操作
*/
public abstract ActionForward doMainAction(ActionMapping mapping,ActionForm form,
HttpServletRequest request,HttpServletResponse response)
throws IOException, ServletException; /**
*必须操作
*/
public abstract void doPostAction(ActionMapping mapping,ActionForm form,
HttpServletRequest request,HttpServletResponse response)
throws IOException, ServletException;
}
以后每个action再继承ActionBase,如果你有一个验证是所有action都要使用的,就在doValidate(..)操作,以后每个action还可以自己做验证,先super(),然后写自己的验证代码
例如
public class FittingsUseShowAction extends ActionBase { public FittingsUseShowAction() {
}
public Vector doValidate(ActionMapping mapping,ActionForm form,
HttpServletRequest request,HttpServletResponse response)
{
……
super(mapping,form,request,response);
}
看看吧,呵呵,不知道行不行
http://community.csdn.net/Expert/topic/3472/3472337.xml?temp=.2961847
我也在考虑考虑
在filter里面查询 cookie 放入session
然后进行 session 判断 是不是上线了
基本上可以了吧!