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文件夹中 系统要能够进行登入错误次 ...
随机推荐
- Drools等规则引擎技术对比分析
项目中需要设计开发一个规则引擎服务,于是调研了业界常用的规则引擎. 常见的规则引擎如下: Ilog JRules 是最有名的商用BRMS: Drools 是最活跃的开源规则引擎: Jess 是Clip ...
- A Taxonomy for Performance
A Taxonomy for Performance In this section, we introduce some basic performance metrics. These provi ...
- Html5 移动游戏开发
有非常多游戏採用H5技术开发.比方三国来了.巴哈姆特之怒.切绳子等. 我们公司也有多款游戏用H5开发.H5开发成本低.效率高,方便做自己主动更新,可移植性好. 受益于H5技术,我们公司的非常多产品都非 ...
- Android CountDownTimer的使用
官方提供的用法如下: new CountDownTimer(30000, 1000) { public void onTick(long millisUntilFinished) { mTextFie ...
- 让ListView回来原来的位置
让ListView回到原来的位置 当从ListView中的某一个Item跳转到其他的Activity,进行操作之后,ListView可能需要刷新(重新加载数据源),这个时候ListView就会回到原始 ...
- 【线程安全】—— 单例类双重检查加锁(double-checked locking)
1. 三个版本单例类的实现 版本1:经典版 public class Singleton { public static Singleton getInstance() { if (instance ...
- CF732 F Tourist Reform——边双连通分量
题目:http://codeforces.com/contest/732/problem/F 首先把边双缩点,边双内部 dfs 一个顺序一定是可以从每个点走到边双内部所有点的,因为它是以环为基本单位: ...
- RDA EQ&频响曲线
相关数据: FAC->Audio->EQ Setting EQ Band - Gain Frequency Q Factor 1.5 FAC->Audio->PEQ // En ...
- bzoj1407
扩展欧几里得 我们发现其实就是两个野人在自己的寿命内不会相遇,或者永远不会相遇,那么我们枚举m,然后枚举两个人,看是否符合条件 扩展欧几里得ax+by=c,这里c不能取模,a能取模,具体不想了 #in ...
- Timer A UP mode 中断
Timer_A, Toggle P1.0, CCR0 Up Mode ISR, DCO SMCLK // Description: Toggle P1.0 using software and TA ...