20170918_chr_newlogin Struts2实现登陆功能(简单validate验证)
2017-12-13 13:23
721 查看
Struts2实现登陆功能
/20170918_chr_newlogin/src/nuc/sw/action/LoginAction.javapackage nuc.sw.action; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; public class LoginAction extends ActionSupport { private String username; private String password; private String type; public String getType() { return type; } public void setType(String type) { this.type = type; } 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; } @Override public String execute() throws Exception { // TODO Auto-generated method stub //接收登陆表单页提交的数据 if(username.equals("程浩然")&&password.equals("123")) { ActionContext.getContext().getSession().put("user",username); ActionContext.getContext().getSession().put("type",type); return SUCCESS; } else { ActionContext.getContext().getSession().put("error","用户名或密码错误"); return ERROR; } } public void validate() { if(username==null||username.trim().equals("")) { this.addFieldError("usernameError", "用户名不能为空"); } if(password==null||password.trim().equals("")) { this.addFieldError("passwordError", "密码不能为空"); } } }
/20170918_chr_newlogin/src/struts.xml
<struts> <!-- Add packages here --> <package name="user" namespace="/" extends="struts-default"> <action name="LoginAction" class="nuc.sw.action.LoginAction"> <result name="success"> /welcome.jsp </result> <result name="error"> /login.jsp </result> <result name="input"> /login.jsp </result> </action> </package> </struts>
/20170918_chr_newlogin/WebContent/login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>登录页</title> </head> <body> <font color="red">${sessionScope.error}</font> <font color="red"><s:fielderror></s:fielderror></font> <form action="LoginAction" method="post"> 用户名:<input type="text" name="username"><br> 密 码:<input type="text" name="password"><br> 用户类型:<select name="type"> <option>普通用户</option> <option>管理员</option> </select><br> <input type="submit" value="登陆"> </form> </body> </html>
/20170918_chr_newlogin/WebContent/welcome.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>欢迎页</title> </head> <body> 欢迎${session.type}${session.user}登陆! </body> </html>
相关文章推荐
- 20170913_chr_loginchr Struts2实现简单登陆功能
- 【PythonDjango后台实例 第六章】Django实现简单的登陆验证功能
- django开发项目实例3--用session是实现简单的登陆、验证登陆和注销功能
- 用户登陆与验证的简单实现
- 用JS实现简单的登录验证功能
- 简单的登陆注册的实现+验证码和表单验证
- jQuery Validate插件实现表单强大的验证功能
- jQueryEasyUI ValidateBox 验证框轻松实现验证功能
- ThinkPHP实现简单登陆功能
- 【IOS】利用ASIHTTPRequest 实现一个简单的登陆验证
- 1_python小程序之实现用户的注册登陆验证功能
- SpringBoot + thymeleaf 实现简单的登陆验证
- Vue+Vue Router+Axios+Webpack+Flask+MySQL实现简单的注册、登录验证功能
- JAVA简单链接Oracle数据库,实现注册和登陆的功能
- php与ajax实现简单登陆功能
- Java Web中简单实现自动登陆功能。
- Tomcat__实现简单的登陆功能
- ASP.NET jQuery 实例12 通过使用jQuery validation插件简单实现用户注册页面验证功能
- python实现简单的登陆验证
- JSP的登陆验证功能实现