struts实战--登录功能实现

栏目: Struts · 发布时间: 8年前

内容简介: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约束

<!DOCTYPE validators PUBLIC
                "-//Apache Struts//XWork Validator 1.0.3//EN"
                "http://struts.apache.org/dtds/xwork-validator-1.0.3.dtd">

 3.对属性进行校验

<field name="logonName">
                    <field-validator type="requiredstring">
                        <message>用户名不能为空</message>
                    </field-validator>

                    <field-validator type="stringlength">
                        <param name="maxLength">12</param>
                        <param name="minLength">3</param>
                        <message>用户名长度必须在${minLength}到${maxLength}之间</message>
                    </field-validator>
                </field>
                <field name="logonPwd">
                    <field-validator type="requiredstring">
                        <message>密码不能为空</message>
                    </field-validator>
                </field>
                在页面上通过<s:fielderror>

 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";
		}
	}

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Practical Algorithms for Programmers

Practical Algorithms for Programmers

Andrew Binstock、John Rex / Addison-Wesley Professional / 1995-06-29 / USD 39.99

Most algorithm books today are either academic textbooks or rehashes of the same tired set of algorithms. Practical Algorithms for Programmers is the first book to give complete code implementations o......一起来看看 《Practical Algorithms for Programmers》 这本书的介绍吧!

SHA 加密
SHA 加密

SHA 加密工具

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具