Struts框架汲取了Struts的优点,以WebWork为核心,拦截器,可变和可重用的标签。

第一步:加载Struts2 类库:

第二步:配置web.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app version="2.5"
  3. xmlns="http://java.sun.com/xml/ns/javaee"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
  6. http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  7. <welcome-file-list>
  8. <welcome-file>index.jsp</welcome-file>
  9. </welcome-file-list>
  10.  
  11. <!-- 配置Struts2过滤器 :拦截请求 -->
  12. <filter>
  13. <filter-name>Struts2</filter-name>
  14. <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  15. </filter>
  16. <filter-mapping>
  17. <filter-name>Struts2</filter-name>
  18. <url-pattern>/*</url-pattern><!--'/*' 过滤所有请求 -->
  19. </filter-mapping>
  20.  
  21. </web-app>

第三步:开发视图层页面(提交表单页)

  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
  2. <%
  3. String path = request.getContextPath();
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  5. %>
  6.  
  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  8. <html>
  9. <head>
  10. <base href="<%=basePath%>">
  11.  
  12. <title>Struts2Demo</title>
  13.  
  14. </head>
  15.  
  16. <body>
  17. <h1>HelloWorld Struts2</h1>
  18. <hr>
  19. <form action="LonginAction" method="post">
  20. <p>用户名:<input type="text" name="username" /></p>
  21. <p>密 码:<input type="password" name="password" /></p>
  22. <p><input type="submit" /></p>
  23. </form>
  24. </body>
  25. </html>
四:三层架构:
  1. package dao;
  2. /**
  3. * 用户操作接口
  4. * @author Administrator
  5. *
  6. */
  7. public interface UserDao {
  8.  
  9. public String login(String username,String password);
  10.  
  11. }
  1. package biz;
  2. /**
  3. * 用户业务接口
  4. * @author Administrator
  5. *
  6. */
  7. public interface UserBiz {
  8.  
  9. public String login(String username,String password);
  10.  
  11. }
  1. package biz.impl;
  2.  
  3. import biz.UserBiz;
  4. import dao.UserDao;
  5. import dao.UserDaoImpl;
  6.  
  7. public class UserBizImpl implements UserBiz {
  8.  
  9. //创建dao层对象
  10. UserDao userdao = new UserDaoImpl();
  11.  
  12. /**
  13. * 调用dao层方法
  14. */
  15.  
  16. public String login(String username, String password) {
  17.  
  18. return userdao.login(username, password);
  19. }
  20.  
  21. }
  1. package dao;
  2.  
  3. public class UserDaoImpl implements UserDao {
  4.  
  5. public String login(String username, String password) {
  6. String str = "";
  7. if(username.equals("msit") && password.equals("123456")){
  8. str = "success";
  9. }else{
  10. str = "error";
  11. }
  12.  
  13. // TODO Auto-generated method stub
  14. return str;
  15. }
  16.  
  17. }

第五步:开发控制层Action

  1. package Action;
  2.  
  3. import java.util.Map;
  4.  
  5. import javax.servlet.http.HttpServletRequest;
  6. import javax.servlet.http.HttpServletResponse;
  7. import javax.servlet.http.HttpSession;
  8.  
  9. import org.apache.struts2.ServletActionContext;
  10.  
  11. import biz.UserBiz;
  12. import biz.impl.UserBizImpl;
  13.  
  14. import com.opensymphony.xwork2.ActionContext;
  15. import com.opensymphony.xwork2.ActionSupport;
  16.  
  17. /**
  18. * 登录控制器
  19. * @author Administrator
  20. *
  21. */
  22. public class LoginAction extends ActionSupport{
  23.  
  24. /*值栈:页面可以直接获取*/
  25. String username;//form表单name;进行封装
  26. String password;//form表单name;进行封装
  27.  
  28. //创建Biz层对象
  29. UserBiz userbiz = new UserBizImpl();
  30.  
  31. /* (non-Javadoc)
  32. * @see com.opensymphony.xwork2.ActionSupport#execute()
  33. */
  34. @Override
  35. public String execute() throws Exception {
  36. // TODO Auto-generated method stub
  37. System.out.println(username+"==="+password);
  38.  
  39. /*解耦(降低依赖)方式;基于Map集合*/
  40. ActionContext ac = ActionContext.getContext();
  41. /*得到request*/
  42. Map request = (Map) ac.get("request");
  43. //request.put("username", username);
  44. /*得到session*/
  45. Map session = ac.getSession();
  46. /*得到application*/
  47. Map application = ac.getApplication();
  48. //===================================================
  49. /*耦合(依懒性)方式*/
  50. HttpServletRequest request2 = ServletActionContext.getRequest();
  51. HttpServletResponse response = ServletActionContext.getResponse();
  52. HttpSession session2 = request2.getSession();
  53.  
  54. //request2.setAttribute("username", username);
  55.  
  56. /**
  57. * struts默认提供了五个状态字符串
  58. */
  59. return userbiz.login(username, password);
  60. }
  61.  
  62. /**
  63. * @return the username
  64. */
  65. public String getUsername() {
  66. return username;
  67. }
  68.  
  69. /**
  70. * @param username the username to set
  71. */
  72. public void setUsername(String username) {
  73. this.username = username;
  74. }
  75.  
  76. /**
  77. * @return the password
  78. */
  79. public String getPassword() {
  80. return password;
  81. }
  82.  
  83. /**
  84. * @param password the password to set
  85. */
  86. public void setPassword(String password) {
  87. this.password = password;
  88. }
  89. }

第六步:配置struts.xml(核心文件);可以参照struts-default.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE struts PUBLIC
  3. "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
  4. "http://struts.apache.org/dtds/struts-2.3.dtd">
  5. <struts>
  6. <!-- 配置包信息 -->
  7. <package name="default" namespace="/" extends="struts-default">
  8. <!-- 配置Action:关联Action JavaBean -->
  9. <action name="LonginAction" class="Action.LoginAction">
  10. <!-- 指定返回的视图 ;默认使用转发-->
  11. <result name="success">success.jsp</result>
  12. <result name="error">error.jsp</result>
  13. </action>
  14. </package>
  15. </struts>

第七步:处理完逻辑之后返回相应的字符串success、error跳转到相应的页面

  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
  2. <%
  3. String path = request.getContextPath();
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  5. %>
  6.  
  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  8. <html>
  9. <head>
  10. <base href="<%=basePath%>">
  11.  
  12. <title>My JSP 'success.jsp' starting page</title>
  13.  
  14. <meta http-equiv="pragma" content="no-cache">
  15. <meta http-equiv="cache-control" content="no-cache">
  16. <meta http-equiv="expires" content="0">
  17. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  18. <meta http-equiv="description" content="This is my page">
  19. <!--
  20. <link rel="stylesheet" type="text/css" href="styles.css">
  21. -->
  22.  
  23. </head>
  24.  
  25. <body>
  26. ${username } 登陆成功!!! <br>
  27. </body>
  28. </html>
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
  2. <%
  3. String path = request.getContextPath();
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  5. %>
  6.  
  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  8. <html>
  9. <head>
  10. <base href="<%=basePath%>">
  11.  
  12. <title>My JSP 'error.jsp' starting page</title>
  13.  
  14. <meta http-equiv="pragma" content="no-cache">
  15. <meta http-equiv="cache-control" content="no-cache">
  16. <meta http-equiv="expires" content="0">
  17. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  18. <meta http-equiv="description" content="This is my page">
  19. <!--
  20. <link rel="stylesheet" type="text/css" href="styles.css">
  21. -->
  22.  
  23. </head>
  24.  
  25. <body>
  26. 登录失败 <br>
  27. </body>
  28. </html>

注意:

  1. 配置struts2
  2. 1、加入类库(jar包)—从应用实例当中拷贝
  3. 2、在web.xml中配置过滤器
  4. <!-- 配置Struts2过滤器 -->
  5. <filter>
  6. <filter-name>Struts2</filter-name>
  7. <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  8. </filter>
  9. <filter-mapping>
  10. <filter-name>Struts2</filter-name>
  11. <url-pattern>/*</url-pattern><!--'/*' 过滤所有请求 -->
  12. </filter-mapping>
  13.  
  14. 3、编写页面index.jsp
  15. 4、配置控制器(action) LoginAction 继承 Actionsupper
  16. 重写父类的excute()方法
  17.  
  18. 5、配置struts.xml(核心文件);可以参照struts-default.xml
  19.  
  20. 6、处理完逻辑之后返回相应的字符串success、error跳转到相应的页面

Struts2框架实现简单的用户登入的更多相关文章

  1. Struts2+AJAX+JQuery 实现用户登入与注册功能。

    要求 必备知识 JAVA/Struts2,JS/JQuery,HTML/CSS基础语法. 开发环境 MyEclipse 10 演示地址 演示地址 预览截图(抬抬你的鼠标就可以看到演示地址哦): 关于U ...

  2. Struts2+AJAX+JQuery 实现用户登入与注册功能

    要求:必备知识:JAVA/Struts2,JS/JQuery,HTML/CSS基础语法:开发环境:MyEclipse 10 关于UI部分请查看下列链接,有详细制作步骤: 利用:before和:afte ...

  3. [Django]登陆界面以及用户登入登出权限

    前言:简单的登陆界面展现,以及用户登陆登出,最后用户权限的问题 正文: 首先需要在settings.py设置ROOT_URLCONF,默认值为: ROOT_URLCONF  = 'www.urls'# ...

  4. Django,COOKIES,SESSION完成用户登入

    1.urls.py """Django_cookie_session URL Configuration The `urlpatterns` list routes UR ...

  5. python基础篇---实战---用户登入注册程序

    一.首先了解需求: 1.支持多个用户登入 2.登入成功后显示欢迎,并退出程序 3.登入三次失败后,退出程序,并在下次程序启动尝试登入时,该用户名依然是锁定状态 二.文件代码如下: f = open(& ...

  6. Oracle+struts2实现用户登入并显示访问次数

    实体类: package entity; public class userfo { private int id;//id private String name;//用户名 private Str ...

  7. 【转】vsftpd用户登入不进去问题

    实在是登陆不上... 我已经加了一个新的用户UID和GID都设置到1000以后 /etc/vsftpd.conf也加了local_enable=yes 以standalone模式运行. 重启服务器后, ...

  8. MonGoDB 常见操作, 设置管理员和用户登入

    [ 启动客户端 => ./bin/mongo --host 192.168.200.100 ] 1: 查看所有已经创建的数据库  =>  show dbs   2: 切换或者创建数据库   ...

  9. python编辑用户登入界面

    1.需求分析 登入界面需要达到以下要求: 系统要有登入和注册两个选项可供选择 系统要能够实现登入出错提示,比如账户密码错误等,用户信息保存在user_info.txt文件夹中 系统要能够进行登入错误次 ...

随机推荐

  1. Drools等规则引擎技术对比分析

    项目中需要设计开发一个规则引擎服务,于是调研了业界常用的规则引擎. 常见的规则引擎如下: Ilog JRules 是最有名的商用BRMS: Drools 是最活跃的开源规则引擎: Jess 是Clip ...

  2. A Taxonomy for Performance

    A Taxonomy for Performance In this section, we introduce some basic performance metrics. These provi ...

  3. Html5 移动游戏开发

    有非常多游戏採用H5技术开发.比方三国来了.巴哈姆特之怒.切绳子等. 我们公司也有多款游戏用H5开发.H5开发成本低.效率高,方便做自己主动更新,可移植性好. 受益于H5技术,我们公司的非常多产品都非 ...

  4. Android CountDownTimer的使用

    官方提供的用法如下: new CountDownTimer(30000, 1000) { public void onTick(long millisUntilFinished) { mTextFie ...

  5. 让ListView回来原来的位置

    让ListView回到原来的位置 当从ListView中的某一个Item跳转到其他的Activity,进行操作之后,ListView可能需要刷新(重新加载数据源),这个时候ListView就会回到原始 ...

  6. 【线程安全】—— 单例类双重检查加锁(double-checked locking)

    1. 三个版本单例类的实现 版本1:经典版 public class Singleton { public static Singleton getInstance() { if (instance ...

  7. CF732 F Tourist Reform——边双连通分量

    题目:http://codeforces.com/contest/732/problem/F 首先把边双缩点,边双内部 dfs 一个顺序一定是可以从每个点走到边双内部所有点的,因为它是以环为基本单位: ...

  8. RDA EQ&频响曲线

    相关数据: FAC->Audio->EQ Setting EQ Band - Gain Frequency Q Factor 1.5 FAC->Audio->PEQ // En ...

  9. bzoj1407

    扩展欧几里得 我们发现其实就是两个野人在自己的寿命内不会相遇,或者永远不会相遇,那么我们枚举m,然后枚举两个人,看是否符合条件 扩展欧几里得ax+by=c,这里c不能取模,a能取模,具体不想了 #in ...

  10. Timer A UP mode 中断

    Timer_A, Toggle P1.0, CCR0 Up Mode ISR, DCO SMCLK //  Description: Toggle P1.0 using software and TA ...