登录操作

欢迎界面
登录的时候不同用户看到不同的菜单,页面在webinf下面,需要一个转发Action;
拦截器,如果没有登录就转到登录界面;
--
登录查找用户Action得到User;
如果为null就跳到登录页面,如果有值,将其放入session中;
一个工具类:将user放进session;
public class OAUtils {
public static void putUser2Session(User user){
ServletActionContext.getRequest().getSession().setAttribute("user", user);
}

public static User getUserFromSession(){
return (User)ServletActionContext.getRequest().getSession().getAttribute("user");
}
}
----
专门一个LoginAction:
@Controller("loginAction")
@Scope("prototype")
public class LoginAction extends BaseAction<User>{
public String login(){
/**
* 1、验证用户名和密码是否正确
* 2、如果正确,把user放入到session中
* 3、跳转到框架页面
*/
User user = this.userService.login(this.getModel().getUsername(), this.getModel().getPassword());
if(user==null){//用户名或者密码不正确
//跳转到登录页面,重新进行登录
this.addActionError("用户名或者密码错误");
return "login";
}else{//用户名和密码正确
OAUtils.putUser2Session(user);
return "index";
}
}
}
//this.addActionError("用户名或者密码错误"); 需要对应到页面上的显示错误信息;
<s:actionerror/> 这个标签是现实错误信息的;
--
struts-login.xml
<struts>
<package name="login" namespace="/" extends="struts-global">
<action name="loginAction_*" method="{1}" class="loginAction">
<result name="login">login.jsp</result>
<result name="index">WEB-INF/jsp/frame/index.jsp</result>
</action>
</package>
</struts>
--

index.jsp是需要几个页面的frameset组合,这里就写一个forwardAction获取页面;
<%@ include file="/WEB-INF/jsp/common/common.jsp"%>
<html>
<head>
<title>ItcastOA</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<frameset rows="100,*,25" framespacing="0" border="0" frameborder="0">
<frame src="forwardAction_forward.action?method=top" name="TopMenu" scrolling="no" noresize />
<frameset cols="180,*" id="resize">
<frame noresize name="menu" src="forwardAction_forward.action?method=left" scrolling="yes" />
<frame noresize name="right" src="forwardAction_forward.action?method=right" scrolling="yes" />
</frameset>
<frame noresize name="status_bar" scrolling="no" src="forwardAction_forward.action?method=bottom" />
</frameset>
<noframes>
<body>
</body>
</noframes>
</html>


----
public class ForwardAction extends ActionSupport{
private String method;

public String getMethod() {
return method;
}

public void setMethod(String method) {
this.method = method;
}

public String forward(){
return this.method;
}
}


--
进行页面之间的跳转;
<struts>
<package name="forward" namespace="/" extends="struts-default">
<action name="forwardAction_*" method="{1}" class="com.itcast.oa.struts2.action.ForwardAction">
<result name="top">WEB-INF/jsp/frame/top.jsp</result>
<result name="bottom">WEB-INF/jsp/frame/bottom.jsp</result>
<result name="left">WEB-INF/jsp/frame/left.jsp</result>
<result name="right">WEB-INF/jsp/frame/right.jsp</result>
<result name="kynamic">WEB-INF/jsp/kynamic/kynamic.jsp</result>
</action>
</package>
</struts>
--
加载树的时候,从session中取出用户,session中的User在放的时候,里面是"from User where username = ? and password = ?"
也就是这里并没有将权限树取出来,所以这里需要单独使用一个方法:根据用户id取出权限;
而不能直接user.getMenuitems(); y因为这里是懒加载的;
但是这个不是原因,原因是即使将其取出来,在index.jsp中这个user所在的操作session已将关闭,left.jsp发送新的请求的session跟之前的session已经不是一个session了;而这里是HttpSession;因此需要和数据库从新关联取出菜单项;

posted @ 2014-03-05 22:33  教程学习  阅读(318)  评论(0)    收藏  举报