📄 day03_1.txt
字号:
用户登陆
a) 在exception 包中新建 LoginException
package exception;
public class LoginException extends Exception {
public LoginException() {
}
public LoginException(String message) {
super(message);
}
public LoginException(String message, Throwable cause) {
super(message, cause);
}
public LoginException(Throwable cause) {
super(cause);
}
}
----------------------------------------------------------
b) 在UserMgmSessionFacade中添加userLogin方法,用于用户登陆
右键点击UserMgmSessionFacade --> add --> method
修改:
name : userLogin
return type :boolean (成功返回true,失败返回false)
input parameter: dto.UserDTO user(用户输入的数据)
interface : local
在UserMgmSessionFacade中添加adminLogin方法,用于管理员登陆
右键点击UserMgmSessionFacade --> add --> method
修改:
name : adminLogin
return type :boolean (成功返回true,失败返回false)
input parameter: dto.UserDTO user(用户输入的数据)
interface : local
----------------------------------------------------------
c) 为userLogin添加实现体
--> view bean source
将userLogin的实现,修改如下:
public boolean userLogin(dto.UserDTO user)
throws LoginException {
try{
UserLocal local =
userLocalHome.findByPrimaryKey(user.getUserId());
if( user.getPassword().equals(
local.getPassword()) ){
return true;
}
return false;
}catch( FinderException e ){
e.printStackTrace();
throw new LoginException( e.getMessage() );
}
}
为adminLogin添加实现体
--> view bean source
将adminLogin的实现,修改如下:
public boolean adminLogin(dto.UserDTO user)
throws LoginException{
try{
AdminLocal local =
adminHome.findByPrimaryKey( user.getUserId() );
if( local.getPassword().equals(
user.getPassword() ) ){
return true;
}
return false;
}catch( FinderException fe ){
fe.printStackTrace();
throw new LoginException( fe.getMessage() );
}
}
----------------------------------------------------------
d) 修改UserMgmSessionFacadeLocal中的函数声, 使之与bean class 中的一致 :
为userLogin方法,添键throws LoginException
public boolean userLogin(UserDTO user)
throws LoginException;
为adminLogin方法,添键throws LoginException
public boolean adminLogin(UserDTO user)
throws LoginException;
----------------------------------------------------------
e) 编译UserMgmSessionFacad
==========================================================
为UserMgmDelegate 添加商业方法 userLogin 用于用户登陆
public boolean userLogin( UserDTO user )
throws LoginException{
return sessionLocal.userLogin( user );
}
为UserMgmDelegate 添加商业方法 adminLogin 用于管理员登陆
public boolean adminLogin( UserDTO user )
throws LoginException{
return sessionLocal.adminLogin( user );
}
==========================================================
编写用户登陆的页面 index.jsp
a) name : index.jsp --> next
tagliberary : struts1.1 --> bean , html
b) 在主页的合适位置添加login formj
<html:form action="login.do" method="post"name="loginForm" type="ec_port_web.LoginForm">
<table cellspacing=1 cellpadding=2 width=131
align=center height="79">
<tr>
<td>用户名</td>
<td><html:text property="name"/></td>
</tr>
<tr>
<td>密 码<div align=center></div></td>
<td> <html:text property="password"/></td>
</tr>
<tr>
<td colspan=2 height=2>
<input type="radio" name="USER_TYPE"
id="yh" value="yh" checked>
<label for="yh"> 用户</label>
<input type="radio" name="USER_TYPE" value="gly" id="gly">
<label for="gly"> 管理员</label>
</td>
</tr>
<tr>
<td colspan=2 height=2 align="center"> <html:submit>登陆</html:submit>
</td>
</tr>
</table>
</html:form>
==========================================================
为登陆 form 创建formBean
a) file --> new --> web --> actionForm
填写
package : ec_port_web
actionForm : ec_port_web.LoginForm
--> next --> add from jsp --> index.jsp --> next
formBean name : loginForm
(注意:
actionForm : 对应<html:form>中的type
formbean name : 对应<html:form>中的name
)
package ec_port_web;
import org.apache.struts.action.*;
import javax.servlet.http.*;
public class LoginForm extends ActionForm {
private String name;
private String password;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public ActionErrors validate(
ActionMapping actionMapping,
HttpServletRequest httpServletRequest) {
return null;
}
public void reset(
ActionMapping actionMapping,
HttpServletRequest httpServletRequest) {
name = null;
password = null;
}
}
b) 重写validate 和 reset 方法
c) 编译
d) 察看struts-config.xml原文件
==========================================================
创建LoginAction
a) file --> new --> web --> Action
填写 package : ec_port_web
Action : LoginAction
--> next
ActionPath : /login
form bean name: loginForm
scope : session
validate : true
input jsp : index.jsp
其中:
ActionPath : Action的侦听路径,与页面form中的Action相
对应,将.do去掉即可
form bean name : Action 需要用到的form bean
scope :form bean 放在什么区域
input jsp : 请求是由那个页面发送的
--> finish
b) 为 loginAction 配置forward:
双击struts-config.xml ,进入图形界面
在走下角展开Action Mapping 选中/login
在窗口右下方选中forward标签 --> add
1) path : /index.jsp
name : fail
2) path : /userCenter.jsp
name : success
3) path : /fail.jsp
name : systemError
c) 存盘 , 察看struts-config.xml 源代码
==========================================================
编写userCenter.jsp
==========================================================
实现LoginAction中的perform方法:
package ec_port_web;
import org.apache.struts.action.*;
import javax.servlet.http.*;
import exception.*;
import delegate.*;
import javax.ejb.CreateException;
import dto.UserDTO;
public class LoginAction extends Action {
public ActionForward perform(
ActionMapping actionMapping,
ActionForm actionForm,
HttpServletRequest request,
HttpServletResponse httpServletResponse) {
LoginForm form = (LoginForm)actionForm;
try{
UserMgmDelegate ud = new UserMgmDelegate();
//判断是否为管理员登陆
if( request.getParameter( "USER_TYPE" ).
equals("gly") ){
//执行管理员登陆
if( ud.adminLogin( new UserDTO(
form.getName(),form.getPassword()))){
//设置登陆标志和管理员身份标志
request.getSession().setAttribute(
"USER_ID", form.getName());
request.getSession().setAttribute(
"IS_ADMIN", "is admin");
return
actionMapping.findForward("loginSuccess");
}
else{
return actionMapping.findForward( "loginFail" );
}
}
//普通用户登陆
if( ud.userLogin( new UserDTO(
form.getName(),form.getPassword() ) ) ){
request.getSession().setAttribute( "USER_ID",form.getName());
return actionMapping.findForward( "loginSuccess" );
}
//设置错误类型
request.setAttribute( "LOGIN_ERROR" , "error");
return actionMapping.findForward( "loginFail" );
}catch( ServiceLocatorException se ){
return actionMapping.findForward( "systemError" );
}
catch( CreateException ce ){
return actionMapping.findForward( "systemError" );
}
catch( LoginException le ) {
return actionMapping.findForward( "loginFail" );
}
}
}
==========================================================
编译,部署,测试
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -