在这里,有三个名词需要区分:项目首页、系统首页、子系统首页、登录页。
项目首页是指在web.xml文件中配置的页面。
<welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list>
当一个系统比较大时,可能包含多个子系统。系统有自己的主页,称为系统首页;而子系统也有自己的主页,称为子系统首页。
登录页,则是用户进行登录的页面。
1、系统首页
(1)HomeAction.java
package com.rk.home.action; import com.opensymphony.xwork2.ActionSupport; public class HomeAction extends ActionSupport { //跳转到首页 @Override public String execute() throws Exception { return "home"; } }
(2)struts-home.xml ,注意:这里并没有将系统首页注入到Spring的IOC容器。因为在后面的子系统首页,也叫做HomeAction,如果注册两个HomeAction会发生报错。
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <package name="home_package" namespace="/sys" extends="base-default"> <action name="home_*" class="com.rk.home.action.HomeAction" method="{1}"> <result name="home">/WEB-INF/jsp/home/home.jsp</result> <result name="{1}">/WEB-INF/jsp/home/{1}.jsp</result> </action> </package> </struts>
注意:需要将struts-home.xml加入到struts.xml文件中。
(3)JSP页面:省略
2、子系统首页
(1)HomeAction.java 注意:虽然系统首页和子系统首页都叫HomeAction,但它们在不同的package下。
package com.rk.tax.action; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Controller; import com.opensymphony.xwork2.ActionSupport; @Controller("homeAction") @Scope("prototype") public class HomeAction extends ActionSupport { //跳转到纳税访问系统首页 public String frame(){ return "frame"; } //跳转到纳税访问系统首页-顶部 public String top(){ return "top"; } //跳转到纳税访问系统首页-左边菜单 public String left(){ return "left"; } }
(2)struts-home.xml ,这个文件与之前的文件不在同一个文件夹下。
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <package name="tax_home_package" namespace="/tax" extends="base-default"> <action name="home_*" class="homeAction" method="{1}"> <result name="{1}">/WEB-INF/jsp/tax/{1}.jsp</result> </action> </package> </struts>
注意:最后要将struts-home包含到struts.xml文件中
(3)JSP页面:省略
3、登录页
(1)LoginAction.java
package com.rk.home.action; import java.util.List; import javax.annotation.Resource; import org.apache.commons.lang3.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.struts2.ServletActionContext; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Controller; import com.opensymphony.xwork2.ActionSupport; import com.rk.core.constant.ProjectStatics; import com.rk.tax.entity.User; import com.rk.tax.service.UserService; @Controller("loginAction") @Scope("prototype") public class LoginAction extends ActionSupport { /*****1、业务数据 *****/ private User user; private String loginResult; /*****2、业务实现类 *****/ @Resource private UserService userService; /*****3、响应JSP页面的操作 *****/ //登录 public String login(){ if(user != null){ if(StringUtils.isNotBlank(user.getAccount()) && StringUtils.isNotBlank(user.getPassword())){ //根据用户的帐号和密码查询用户列表 List<User> list = userService.findUserByAccountAndPassword(user.getAccount(),user.getPassword()); if(list != null && list.size()>0){//说明登录成功 //2.1、登录成功 User user = list.get(0); //2.1.1、根据用户id查询该用户的所有角色 user.setUserRoles(userService.findUserRolesByUserId(user.getId())); //2.1.2、将用户信息保存到session中 ServletActionContext.getRequest().getSession().setAttribute(ProjectStatics.USER, user); //2.1.3、将用户登录记录到日志文件 Log log = LogFactory.getLog(getClass()); log.info("用户名称为:" + user.getName() + " 的用户登录了系统。"); //2.1.4、重定向跳转到首页 return "home"; } else{ loginResult = "帐号或密码不正确!"; } } else{ loginResult = "帐号或密码不能为空!"; } } else{ loginResult = "请输入帐号和密码!"; } return toLoginUI(); } //退出,注销 public String logout(){ //清除session中保存的用户信息 ServletActionContext.getRequest().getSession().removeAttribute(ProjectStatics.USER); return "toLoginUI"; } //跳转到登录页面 public String toLoginUI(){ return "loginUI"; } //跳转到没有权限提示页面 public String toNoPermissionUI(){ return "noPermissionUI"; } // {{ public User getUser() { return user; } public void setUser(User user) { this.user = user; } public String getLoginResult() { return loginResult; } public void setLoginResult(String loginResult) { this.loginResult = loginResult; } // }} }
(2)struts-home.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <package name="home_package" namespace="/sys" extends="base-default"> <action name="home_*" class="com.rk.home.action.HomeAction" method="{1}"> <result name="home">/WEB-INF/jsp/home/home.jsp</result> <result name="{1}">/WEB-INF/jsp/home/{1}.jsp</result> </action> <action name="login_*" class="loginAction" method="{1}"> <result name="{1}">/WEB-INF/jsp/{1}.jsp</result> <result name="loginUI">/WEB-INF/jsp/loginUI.jsp</result> <result name="noPermissionUI">/WEB-INF/jsp/noPermissionUI.jsp</result> <result name="home" type="redirectAction"> <param name="actionName">home</param> </result> <result name="toLoginUI" type="redirectAction"> <param name="actionName">toLoginUI</param> </result> </action> </package> </struts>
这里的struts-home.xml要结合LoginAction代码一起看,有两点需要注意的:
第一点注意,在login()方法中,最后返回toLoginUI()方法,它的目的是为了实现信息的回显
return toLoginUI();
第二点注意,在logout()方法中,返回"toLoginUI",是进行了重定向,使用浏览器的地址发生了改变。
(3)UserService.java 添加方法
List<User> findUserByAccountAndPassword(String account, String password);
(4)UserServiceImple.java 添加方法
public List<User> findUserByAccountAndPassword(String account,String password) { return userDao.findUserByAccountAndPassword(account,password); }
(5)UserDao.java添加方法
List<User> findUserByAccountAndPassword(String account, String password);
(6)UserDaoImpl.java添加方法
public List<User> findUserByAccountAndPassword(String account,String password) { Query query = getSession().createQuery("from User where account=? and password=? and state=?"); query.setParameter(0, account); query.setParameter(1, password); query.setParameter(2, User.USER_STATE_VALID);//有效用户才能登录系统 return query.list(); }
(7)LoginUI.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@ taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <%@include file="/common/header.jsp" %> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>登录</title> <link href="${basePath}/css/login.css" type="text/css" rel="stylesheet"> <script type="text/javascript"> function logins(){ document.forms[0].submit(); } function setClean(){ document.getElementById("account").value = ""; document.getElementById("password").value = ""; } //解决子框架嵌套的问题 if(window != window.parent){ window.parent.location.reload(true); } </script> <style type="text/css"> html { overflow-y: hidden; } .password{ background-color:#f1f3f6; border:1px solid #f1f3f6; font-color:#ccc; } #Layer1 { position:absolute; left:224px; top:479px; padding-top:5px; width:99px; height:21px; background-color:#fff; z-index:1; } .password1 { background-color:#f1f3f6; border:1px solid #f1f3f6; font-color:#ccc; } .youbian input{ border:0px none; background-color:transparent; color:#555;padding-left:10px;font-size:16px;width:100%;overflow: hidden;} </style> <!--[if IE 6]> <script type="text/javascript" src="${basePath}/ehome/js/DD_belatedPNG.js" ></script> <script type="text/javascript"> DD_belatedPNG.fix(‘b, s, img, span, .prev, .next, a, input, .youbian, td‘); </script> <![endif]--> </head> <body scroll="no"> <s:form name="form1" namespace="/sys" action="login_login"> <div id="lo_tf"> <div class="outside"> <div class="head"> <table width="1000" height="60" border="0" align="center" cellpadding="0" cellspacing="0"> <tr> <td width="840" align="left"><img src="${basePath}/images/login/form_03.png" width="332" height="47"/></td> <td align="center"> <a href="#"></a></td> </tr> </table> </div> <div class="main2"> <div class="content"> <div class="youbian"> <table width="251" border="0" cellpadding="0" cellspacing="0"> <tr> <td height="12"> </td> </tr> <tr> <td height="45" align="left"></td> </tr> <tr> <td height="13"> <span><div height=20 valign="middle" style="padding-left: 18px"> <font color="red" id="errMsg"> <s:property value="loginResult"/> </font> </div></span> </td> </tr> <tr> <td height="40"> <table width="100%" border="0" cellpadding="0" cellspacing="0"> <tr> <td height="32" align="left"><span style="color:#767676;font-size:14px;">帐号:</span></td> </tr> </table> <table width="100%" height="39" border="0" cellpadding="0" cellspacing="0"> <tr> <td background="${basePath}/images/login/shuru_03.png" width=""><table width="100%" border="0" cellpadding="0" cellspacing="0"> <tr> <td align="left"> <s:textfield id="account" name="user.account" cssClass="password1" cssStyle="color: #767676" size="31"/> </td> </tr> </table></td> </tr> </table></td> </tr> <tr> <td height="10"><table width="100%" border="0" cellpadding="0" cellspacing="0"> <tr> <td height="32" align="left"><span style="color:#767676;font-size:14px;">密 码:</span></td> </tr> </table></td> </tr> <tr> <td height="40"><table width="100%" height="39" border="0" cellpadding="0" cellspacing="0"> <tr> <td background="${basePath}/images/login/shuru_03.png"><table width="100%" border="0" cellpadding="0" cellspacing="0"> <tr> <td align="left"> <s:password id="password" name="user.password" cssClass="password" cssStyle="color: #767676" size="31"/> </td> </tr> </table></td> </tr> </table></td> </tr> <tr> <td height="10"> </td> </tr> <tr> <td height="40"><table width="100%" border="0" cellpadding="0" cellspacing="0"> <tr> <td align="right"><a href="#" onclick="javascript:logins();"><img src="${basePath}/images/login/form_15.png" width="95" height="37"/></a></td> <td width="18"> </td> <td align="left"><img src="${basePath}/images/login/form_17.png" width="95" height="37" | 国税局 2014年</div> </div> </div> </s:form> </body> </html>
4、项目主页
在web.xml文件中指定项目首页
<welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list>
index.jsp ,它的作用是跳转到登录页面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <c:redirect url="/sys/login_toLoginUI.action"></c:redirect>