Struts2框架实现简单的用户登入
Struts框架汲取了Struts的优点,以WebWork为核心,拦截器,可变和可重用的标签。
第一步:加载Struts2 类库:

第二步:配置web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list> <!-- 配置Struts2过滤器 :拦截请求 -->
<filter>
<filter-name>Struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>Struts2</filter-name>
<url-pattern>/*</url-pattern><!--'/*' 过滤所有请求 -->
</filter-mapping> </web-app>
第三步:开发视图层页面(提交表单页)
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>Struts2Demo</title> </head> <body>
<h1>HelloWorld Struts2</h1>
<hr>
<form action="LonginAction" method="post">
<p>用户名:<input type="text" name="username" /></p>
<p>密 码:<input type="password" name="password" /></p>
<p><input type="submit" /></p>
</form>
</body>
</html>
package dao;
/**
* 用户操作接口
* @author Administrator
*
*/
public interface UserDao { public String login(String username,String password); }
package biz;
/**
* 用户业务接口
* @author Administrator
*
*/
public interface UserBiz { public String login(String username,String password); }
package biz.impl; import biz.UserBiz;
import dao.UserDao;
import dao.UserDaoImpl; public class UserBizImpl implements UserBiz { //创建dao层对象
UserDao userdao = new UserDaoImpl(); /**
* 调用dao层方法
*/ public String login(String username, String password) { return userdao.login(username, password);
} }
package dao;
public class UserDaoImpl implements UserDao {
public String login(String username, String password) {
String str = "";
if(username.equals("msit") && password.equals("123456")){
str = "success";
}else{
str = "error";
}
// TODO Auto-generated method stub
return str;
}
}
第五步:开发控制层Action
package Action; import java.util.Map; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession; import org.apache.struts2.ServletActionContext; import biz.UserBiz;
import biz.impl.UserBizImpl; import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport; /**
* 登录控制器
* @author Administrator
*
*/
public class LoginAction extends ActionSupport{ /*值栈:页面可以直接获取*/
String username;//form表单name;进行封装
String password;//form表单name;进行封装 //创建Biz层对象
UserBiz userbiz = new UserBizImpl(); /* (non-Javadoc)
* @see com.opensymphony.xwork2.ActionSupport#execute()
*/
@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
System.out.println(username+"==="+password); /*解耦(降低依赖)方式;基于Map集合*/
ActionContext ac = ActionContext.getContext();
/*得到request*/
Map request = (Map) ac.get("request");
//request.put("username", username);
/*得到session*/
Map session = ac.getSession();
/*得到application*/
Map application = ac.getApplication();
//===================================================
/*耦合(依懒性)方式*/
HttpServletRequest request2 = ServletActionContext.getRequest();
HttpServletResponse response = ServletActionContext.getResponse();
HttpSession session2 = request2.getSession(); //request2.setAttribute("username", username); /**
* struts默认提供了五个状态字符串
*/
return userbiz.login(username, password);
} /**
* @return the username
*/
public String getUsername() {
return username;
} /**
* @param username the username to set
*/
public void setUsername(String username) {
this.username = username;
} /**
* @return the password
*/
public String getPassword() {
return password;
} /**
* @param password the password to set
*/
public void setPassword(String password) {
this.password = password;
}
}
第六步:配置struts.xml(核心文件);可以参照struts-default.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>
<!-- 配置包信息 -->
<package name="default" namespace="/" extends="struts-default">
<!-- 配置Action:关联Action JavaBean -->
<action name="LonginAction" class="Action.LoginAction">
<!-- 指定返回的视图 ;默认使用转发-->
<result name="success">success.jsp</result>
<result name="error">error.jsp</result>
</action>
</package>
</struts>
第七步:处理完逻辑之后返回相应的字符串success、error跳转到相应的页面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'success.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
--> </head> <body>
${username } 登陆成功!!! <br>
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'error.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
--> </head> <body>
登录失败 <br>
</body>
</html>
注意:
配置struts2
1、加入类库(jar包)—从应用实例当中拷贝
2、在web.xml中配置过滤器
<!-- 配置Struts2过滤器 -->
<filter>
<filter-name>Struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>Struts2</filter-name>
<url-pattern>/*</url-pattern><!--'/*' 过滤所有请求 -->
</filter-mapping> 3、编写页面index.jsp
4、配置控制器(action) LoginAction 继承 Actionsupper
重写父类的excute()方法 5、配置struts.xml(核心文件);可以参照struts-default.xml 6、处理完逻辑之后返回相应的字符串success、error跳转到相应的页面
Struts2框架实现简单的用户登入的更多相关文章
- Struts2+AJAX+JQuery 实现用户登入与注册功能。
要求 必备知识 JAVA/Struts2,JS/JQuery,HTML/CSS基础语法. 开发环境 MyEclipse 10 演示地址 演示地址 预览截图(抬抬你的鼠标就可以看到演示地址哦): 关于U ...
- Struts2+AJAX+JQuery 实现用户登入与注册功能
要求:必备知识:JAVA/Struts2,JS/JQuery,HTML/CSS基础语法:开发环境:MyEclipse 10 关于UI部分请查看下列链接,有详细制作步骤: 利用:before和:afte ...
- [Django]登陆界面以及用户登入登出权限
前言:简单的登陆界面展现,以及用户登陆登出,最后用户权限的问题 正文: 首先需要在settings.py设置ROOT_URLCONF,默认值为: ROOT_URLCONF = 'www.urls'# ...
- Django,COOKIES,SESSION完成用户登入
1.urls.py """Django_cookie_session URL Configuration The `urlpatterns` list routes UR ...
- python基础篇---实战---用户登入注册程序
一.首先了解需求: 1.支持多个用户登入 2.登入成功后显示欢迎,并退出程序 3.登入三次失败后,退出程序,并在下次程序启动尝试登入时,该用户名依然是锁定状态 二.文件代码如下: f = open(& ...
- Oracle+struts2实现用户登入并显示访问次数
实体类: package entity; public class userfo { private int id;//id private String name;//用户名 private Str ...
- 【转】vsftpd用户登入不进去问题
实在是登陆不上... 我已经加了一个新的用户UID和GID都设置到1000以后 /etc/vsftpd.conf也加了local_enable=yes 以standalone模式运行. 重启服务器后, ...
- MonGoDB 常见操作, 设置管理员和用户登入
[ 启动客户端 => ./bin/mongo --host 192.168.200.100 ] 1: 查看所有已经创建的数据库 => show dbs 2: 切换或者创建数据库 ...
- python编辑用户登入界面
1.需求分析 登入界面需要达到以下要求: 系统要有登入和注册两个选项可供选择 系统要能够实现登入出错提示,比如账户密码错误等,用户信息保存在user_info.txt文件夹中 系统要能够进行登入错误次 ...
随机推荐
- MS SQL SERVER 书BOOK
http://www.cnblogs.com/lyhabc/p/4833248.html
- C#: 旋转图片到正确位置
当从iPhone等手机上传图片到服务器后,通常需要进行旋转处理,否则在进行图片压缩.缩放处理后会丢失正确的位置信息,导致显示的图片不处于正确的位置上. 处理的做法就是读取照片的Exif信息,并旋转到正 ...
- FTPClientUtil FTPclient工具
package com.ctl.util; //须要commons-net-3.0.1.jar import java.io.*; import java.net.*; import java.uti ...
- ubuntu拨号上网以及路由设置
1.宽带拨号 配置宽带连接信息:pppoeconf 拨号:pon dsl-provider 断开连接:poff dsl-provider 查看拨号日志:plog 2.路由设置 以下为rc.local文 ...
- Linux ALSA声卡驱动之七:ASoC架构中的Codec
1. Codec简介(ad/da) 在移动设备中,Codec的作用可以归结为4种,分别是: 对PCM等信号进行D/A转换,把数字的音频信号转换为模拟信号 对Mic.Linein或者其他输入源的模拟信 ...
- 在C#中实现listbox的项上下移动(winform) 标准
在C#中实现listbox的项上下移动(winform) 收藏人:梅毛子360 2013-10-02 | 阅:1 转:2 | 分享 | 来源 usi ...
- Codeforces--622A--Infinite Sequence(数学)
Infinite Sequence Crawling in process... Crawling failed Time Limit:1000MS Memory Limit:26214 ...
- cf2.25
T1 题意:判断给出的数中有多少不同的大于的数. content:傻逼题,5min手速 T2 题意:给出p.y,输出y~p+1中最大一个不是2-p的倍数的数. content:答案很简单,但是很难想到 ...
- 杂项-Java:JMX
ylbtech-杂项-Java:JMX 1.返回顶部 1. JMX(Java Management Extensions,即Java管理扩展)是一个为应用程序.设备.系统等植入管理功能的框架.JMX可 ...
- [Swift通天遁地]九、拔剑吧-(17)创建一个三维折叠样式的页面展开效果
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...