首页 > 代码库 > Struts2+DAO层实现实例01——搭建Struts2基本框架
Struts2+DAO层实现实例01——搭建Struts2基本框架
实例内容
利用Strust2实现一个登陆+注册功能的登陆系统。
实现基础流程:
实现代码
JSP页面部分:
<%-- 登陆页面 Created by IntelliJ IDEA. User: Richard Date: 2017/6/21 Time: 12:36 To change this template use File | Settings | File Templates. --%><%@ page contentType="text/html;charset=UTF-8" language="java" %><%@taglib prefix="s" uri="/struts-tags" %><html><head> <title>Login</title></head><style> body{ padding: 0; margin:0; } h1{ background-color: black; color: white; } .subform{ width:200px; border-style:solid; border-color: black; border-radius: 2px; padding: 10px; margin-left: 40px; margin-top: 70px; }</style><body><h1>Login For My Struts</h1><hr><div class="subform"> <form method="post" action="loginuser.action" name="pform" > <p>用户名:</p><br> <input type="text" name="username" id="name"><br> <br> <p>密码:</p><br> <input type="password" name="password" id="key"><br> <br> <s:actionmessage></s:actionmessage> <input type="submit" value="登陆" onclick="return check(this);"/> <a href="register.jsp">点我注册</a> </form></div></body><script> function check(form) { if(document.forms.pform.username.value==""){ alert("用户名不能为空!") document.forms.pform.username.focus(); return false; } if(document.forms.pform.password.value==""){ alert("密码不能为空!") document.forms.pform.password.focus(); return false; } }</script></html>
<%-- 注册页面 Created by IntelliJ IDEA. User: Richard Date: 2017/6/21 Time: 13:04 To change this template use File | Settings | File Templates.--%><%@ page contentType="text/html;charset=UTF-8" language="java" %><%@taglib prefix="s" uri="/struts-tags" %><html><head> <title>Register</title></head><style> body{ padding:0; margin:0; } h1{ background-color: black; color:white; } .form{ width: 400px; border-radius: 2px; padding:10px; border-style: solid; border-color: black; margin:0 auto; }</style><body><H1>Register up for My Struts</H1><hr><form method="post" action="*reguser.action" name="rform" class="form"> <p>请输入用户名:</p><br> <input type="text" name="username" id="name"><br> <br> <p>请输入密码:</p><br> <input type="password" name="password" id="key"><br> <p>请再次输入密码:</p><br> <input type="password" name="compassword" id="comkey"><br> <br> <s:actionmessage></s:actionmessage> <input type="submit" value="注册" onclick="return recheck(this);" /> <a href="login.jsp">登陆</a></form></body><script> function recheck(form) { if(document.forms.rform.username.value==""){ alert("用户名不能为空!"); document.forms.rform.username.focus(); return false; } if(document.forms.rform.password.value==""||document.forms.rform.compassword.value==""){ alert("密码不能为空!"); document.forms.rform.password.focus(); return false; } if(document.forms.rform.password.value!=document.forms.rform.compassword.value){ alert("两次输入的密码不相同!") document.forms.rform.password.focus(); return false; } }</script></html>
<%-- 欢迎页面 Created by IntelliJ IDEA. User: Richard Date: 2017/6/16 Time: 21:38 To change this template use File | Settings | File Templates.--%><%@ page contentType="text/html;charset=UTF-8" language="java" %><html><head> <title>Welcome</title></head><body><h1>WelCome to My Struts Page</h1><hr>当前登录的用户:${param.username}<br></body></html>
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> <constant name="struts.devMode" value="true"/> <constant name="struts.enable.DynamicMethodInvocation" value="true"></constant> <!--利用通配符配置Action需要调用的方法--> <package name="user" namespace="/" extends="struts-default"> <action name="*user" class="UserBlock.UserAction" method="{1}"> <result name="index">in.jsp</result> <result name="login">login.jsp</result> <result name="register">register.jsp</result> </action> </package></struts>
Action
package UserBlock;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionSupport;import java.util.ArrayList;import java.util.Map;import java.util.logging.LogManager;/** * Created by Richard on 2017/6/16. * 继承ActionSupport实现一个Action * 登陆界面通过loginuser.action调用login()方法来处理数据 * login()方法中通过ActionContext调用Session对象,存入输入错误的数据 * 通过addActionMessage()方法向页面输出相关信息 * 注册页面通过reguser.action调用reg()方法来处理数据 */public class UserAction extends ActionSupport { private String INDEX="index"; private String LOGIN="login"; private String REG="register"; private String username; private String password; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String login(){ try{ if(username.equals("rekent")){ if(password.equals("root")){ /*登陆成功*/ return INDEX; }else{ ActionContext applicton=ActionContext.getContext(); Map session=applicton.getSession(); int count; if(session.get("count")==null){ count=0; }else{ count= (int) session.get("count"); } if(count>=3){ addActionMessage("错误次数过多"); count=0; session.put("count",count); return LOGIN; }else{ count++; addActionMessage("您输入的用户名或密码错误"+count); session.put("count",count); return LOGIN; } } }else{ addActionMessage("该用户不存在,已经跳转到注册页面"); return REG; } }catch (Exception e){ addActionError(e.getMessage()); } return LOGIN; } public String reg(){ try{ if(username.equals("rekent")) { addActionMessage("该用户已经存在"); return REG; } else{ return INDEX; } }catch (Exception e){ addActionError(e.getMessage()); } return REG; }}
实例代码
示例代码,持续更新
Struts2+DAO层实现实例01——搭建Struts2基本框架
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。