zoukankan      html  css  js  c++  java
  • 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跳转到相应的页面
  • 相关阅读:
    vision transformer
    亮剑,gacutil.exe
    MOSS自定义登陆页面
    查看应用程序的进程ID
    基于AD的表单认证 Moss2010
    js 实现拖动
    获取指定数据库表, 以及指定表的数据信息
    二分法
    JZ053表示数值的字符串
    JZ054字符流中第一个不重复的字符
  • 原文地址:https://www.cnblogs.com/wlx520/p/4581877.html
Copyright © 2011-2022 走看看