您的位置:首页 > 编程语言 > Java开发

struts2+hibernate3+spring3(ssh2)框架下的web应用-SBPMS系统

2014-01-13 00:00 363 查看
摘要: 完善登陆,初步了解session和url

当用户成功登陆了之后,应该跳转到他们的主页,然后在他的主界面进行它能够进行的业务操作。而且应该将他的们的页面分开,因为我如果不是通过 object-j或者使用javascript动态生成页面的话,用户可以通过查看页面源代码来看到页面所有操作。所以将主页建成统一主页是好的,这样可 以减少建设网站的时间和页面地址数量。但是缺点同样明显,除了上面说的之外,还增加了额外的服务器解析负担,页面响应速度慢,代码稳定性弱。总之,我决定 主页使用分开的页面,或许会写很多重复代码,调用重复方法,但是是值得的。

那么先修改登录界面~修改ManageUserAction.java里面的login()函数,并将用户登录信息存储到session里面

/*
* This method is used to the login JSP, include the operations of call
* database and do validation.
*/
public String login() throws Exception {
// Put all information of this user saved in the database to
// session by key user_login
ActionContext.getContext().getSession().put("user_login",
this.user);
EnumSet<Role> rolesSet = EnumSet.allOf(Role.class);
for (Role role : rolesSet) {
if(this.user.getRole().equals(role.toString())){
return role.toString().toUpperCase();
}
}
return INPUT;
}

顺便修改了validate方法,把对空值的判定加在了前面,这样避免了nullpointexception。算是bug修复。

public void validateLogin() {

this.clearActionErrors();

if (null == this.user) {
this.addActionError("User name must not be empty");
return;
}

// Judge the the name input by whether it is empty.
if (null == this.user.getName() || this.user.getName() == "") {
this.addActionError("User name must not be empty");
} else {
// Judge the pre_password input by whether it it empty.
if (null == this.user.getPre_password() || this.user.getPre_password().isEmpty()) {
this.addActionError("Password must not be empty");
} else {
// Change pre_password into password
String md5Password;
try {md5Password = MD5EncrypterUtil.getMD5(
this.user.getPre_password());

this.user.setPassword(md5Password);
// Judge the input name and password by whether they are accord
// with the database.
this.user = this.service.login(this.user);
if (null == this.user) {
this.addActionError("User name or Password error");
this.user = null;
} else {
// Do Nothing
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}

我返回的是角色信息,是一个String类型。因为ACtion的返回值必须是String类型,我调用了一个toUpperCase方法,防止用户输入错误的角色大小写。

在用户登录的时候,我们现实下welcome:***就可以去session取值,我们做拦截器,拦截用户行为的时候,就通过判断session里面存储 的用户信息。比如session里面判断用户为SMS,那么它想访问SUP的界面的话,我们就让他返回登录。这就是拦截器的一般使用的小例子。我们当用户 登录10分钟不动作时候,让session超时(在配置web.xml里面写的),清空session,那么用户再进行操作,我们拦截它,发现没 session,就让他返回登录。等等。。。session可以理解为存储在内存里面的一个临时的数据块。相当于一个记事本。嗯名可以这么理解。

然后进入struts.xml,修改LoginAction的响应:

<action name="login" class="ManageUserAction" method="login">
<result name="input">/WEB-PAGE/Login.jsp</result>
<result name="SUP">/WEB-PAGE/SUP-PAGES/Homepage_SUP.jsp</result>
<result name="SMM">/WEB-PAGE/SMM-PAGES/Homepage_SMM.jsp</result>
<result name="SMS">/WEB-PAGE/SMS-PAGES/Homepage_SMS.jsp</result>
<result name="MS">/WEB-PAGE/MS-PAGES/Homepage_MS.jsp</result>
<result name="SS">/WEB-PAGE/SS-PAGES/Homepage_SS.jsp</result>
</action>

效果如图



这样,得到不同角色后就进入不同页面。当然需要在制定的目录建立jsp页面,我们先暂时建立一个几个空白的页面,在最后才会去修改和完善。实际上我们这样 跳转,会存在一个url地址混乱的问题,因为我们login.jsp是在根目录,进入action后,会继续在根目录。但是我们跳转到具体home目录 后,会将url制定到其所在目录,那么,我们的css样式和javascript函数就会不好加载。当然我会在后面写页面的时候给出解决方案。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: