struts实战--登录功能实现
struts實現登錄功能
一.使用struts2提供的表單標簽來改造頁面。
?? ??? ??? ?WebRoot/login/login.jsp
?? ??? ?
?? ??? ??? ?<form>-------------------<s:form>
?? ??? ??? ?<input type="text">------<s:textfield>
?? ??? ??? ?<input type="password">---<s:password>
?? ??? ??? ?<input type="submit">-----<s:submit>
?? ??? ??? ?<input type="reset">------<s:reset>
?? ??? ????
?? ??? ??? ?
?? ??? ??? ?1.改造form
?? ??? ??? ??? ?<s:form id="loginAction_home" name="form1" action="user_login" namespace="/" target="_parent" method="post">
?? ??? ??? ?2.改造登錄名
?? ??? ??? ??? ?<s:textfield name="logonName" value="" id="logonName" cssClass="text" cssStyle="width: 160px;"/>
?? ??? ??? ?3.改造登錄密碼
?? ??? ??? ??? ?<s:password? name="logonPwd" id="logonPwd" cssClass="text" cssStyle="width: 160px;"/>
?? ??? ??? ??? ??? ?密碼框默認不回顯示.需要設置屬性showPassword="true"
?? ??? ??? ?4.<s:submit name="submit" value="登錄" cssClass="buttoninput"/>
?? ??? ? 5.<s:reset name="reset" value="取消" cssClass="buttoninput"/>
?? ??? ??? ?
?? ??? ??? ?注意:struts2中的表單標簽,有默認的主題xhtml.如果不想要添加任何修飾,只需要將主題修改為simple.
?? ??? ??? ???? 1.全局
?? ??? ??? ??? ??? ?在struts.xml文件中配置一個常量
?? ??? ??? ??? ??? ?<constant name="struts.ui.theme" value="simple"></constant>
?? ??? ??? ??? ?2.局部
?? ??? ??? ??? ??? ?針對于某一個form.
?? ??? ??? ??? ??? ?<s:form theme="simple">
?? ??? ??? ??? ?3.局部
?? ??? ??? ??? ??? ?可以給任意的表單組件去指定theme屬性值。
login.jsp:
<%@ page language="java" pageEncoding="UTF-8"%> <%@taglib uri="/struts-tags" prefix="s"%> <script type="text/javascript"> function ini(){document.form1.logonName.focus(); } </script><html><head><meta http-equiv="Content-Language" content="zh-cn"><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title></title><link href="${pageContext.request.contextPath}/css/Style.css" rel="stylesheet" type="text/css"></head><body onload="ini()"><table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0"><tr><td align="center"><table width="452" height="290" border="0" cellpadding="0" cellspacing="0"><tr><td bgcolor="#FFFFFF"><table width="452" height="290" border="0" cellpadding="0" cellspacing="0"><tr><td height="74"><img src="${pageContext.request.contextPath}/images/logintitle.gif"></td></tr><tr><td align="center" valign="bottom" background="${pageContext.request.contextPath}/images/loginbg.gif"><s:form action="user_login" method="post" theme="simple" namespace="/" id="loginAction_home" name="form1" target="_top"><table border="0" align="center" cellpadding="2" cellspacing="0"><tr align="center"><td height="30" colspan="2" style="border-bottom: 1px dotted #cccccc"><strong style="font-size: 14px;">請登錄</strong><!-- 錯誤信息回顯 --><s:fielderror /> <s:actionerror/></td></tr><tr><td height="30" nowrap><font color="000F60"><strong>用戶名:</strong> </font></td><td><s:textfield name="logonName" id="logonName" cssClass="text" cssStyle="width: 160px;"/></td></tr><tr><td height="30" nowrap><strong><font color="000F60">密碼: </font> </strong></td><td><s:password name="logonPwd" id="logonPwd" cssClass="text" cssStyle="width: 160px;" /></td></tr><tr><td height="30" nowrap colspan="2"><strong><font color="red"></font> </strong></td></tr><tr><td height="30"></td><td><input type="submit" name="submit" value="登陸" class="buttoninput"/><input type="reset" name="reset" value="取消" class="buttoninput"/></td></tr></table></s:form><table width="100%" border="0" cellspacing="0" cellpadding="0"><tr><td height="30" align="center"></td></tr><tr><td height="23" align="center"></td></tr></table></td></tr></table></td></tr></table></td></tr></table></body> </html>?? ??? ??? ?
?二.需要使用xml配置方式對數據進行校驗。
?? ??? ??? ?用戶名 非空,3-12位
?? ??? ??? ?密碼? 非空
?? ??? ??? ?
?? ??? ??? ?1.在UserAction所在包下創建一個UserAction-validation.xml
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE validators PUBLIC"-//Apache Struts//XWork Validator 1.0.3//EN""http://struts.apache.org/dtds/xwork-validator-1.0.3.dtd"> <validators><field name="logonName"><field-validator type="requiredstring"><message key="logonName.required"></message></field-validator><field-validator type="stringlength"><param name="minLength">3</param><param name="maxLength">12</param><message key="logonName.length"></message></field-validator></field><field name="logonPwd"><field-validator type="requiredstring"><message key="logonPwd.required"></message></field-validator></field> </validators>?? ??? ??? ?2.在xml文件中添加dtd約束
?? ??? ??? ???
?? ??? ??? ?3.對屬性進行校驗
?? ??? ??? ??
?? ??? ??? ?
?? ??? ?
?? ??? ?3.登錄成功,將用戶存儲到session,在頁面上顯示用戶。
?? ??? ??? ?top.jsp?? ${user.userName } 顯示當前登陸用戶
/*** 員工登陸* * @return*/@InputConfig(resultName = "loginINPUT")// 修改workflow攔截器跳轉視圖public String login() {// 登陸數據 已經在 user中,傳遞業務層,查詢UserService userService = new UserService();User logonUser = userService.login(user);// 判斷是否登陸成功if (logonUser == null) {// 登陸失敗this.addActionError(this.getText("loginfail"));return "loginINPUT";} else {// 登陸成功ServletActionContext.getRequest().getSession().setAttribute("user", logonUser);return "loginSUCCESS";}}總結
以上是生活随笔為你收集整理的struts实战--登录功能实现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: struts2教程--标签库详解
- 下一篇: struts实战--添加功能(重点文件上