zoukankan      html  css  js  c++  java
  • SSH框架(三) struts2的登陆示例

     

     

    因为最近在学习使用SSH框架(struts2+spring+hibernate),下面来介绍表现层struts2的使用方法。

    (一)添加struts2的包

      见上一篇博客,先看一下添加的各个文件的目录结构

    (二)介绍各个文件的作用

      login.jsp:登陆界面,里面使用的标签是struts2的标签。

      index.jsp:主界面,登陆成功后会转向这个界面

      web.xml:web工程的主配置文件,在这个配置文件中指定了web欢迎界面(就是login.jsp)。添加struts2的过滤器,指定所有的表现层都有struts2来负责。这里要使用org.apache.struts2.dispatcher.FilterDispatcher过滤器,才能即支持struts2标签又支持html标签。如果使用org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter过滤器,则只支持html标签,不支持struts2标签。

      struts.xml:struts2的配置文件,指定每一个action的跳转关系。

      LoginAction.java:用来处理action的java文件。

      

    (三)各个文件的调用关系如下图

      

    (四)各个文件的具体内容

      login.jsp:

    复制代码
    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <%@ taglib prefix="s" uri="/struts-tags" %>
     
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
      </head>
      
      <body>
    
            <s:form action="Login" method="post">  
                <s:textfield key="username" label="用户名" />  
                <s:password key="password" label="密码" />  
                <s:submit value="登陆" />  
            </s:form>
                
      </body>
    </html>
    复制代码

      index.jsp: 

    复制代码
    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
      </head>
      
      <body>
        This is main page. <br>
      </body>
    </html>
    复制代码

      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">
      
      <!-- 配置FilterDispatcher过滤器,以便加Spring容器 -->
      <filter>
          <filter-name>struts2</filter-name>
          <filter-class>
              org.apache.struts2.dispatcher.FilterDispatcher
          </filter-class>
      </filter>
      
      <filter-mapping>
          <filter-name>struts2</filter-name>
          <url-pattern>/*</url-pattern>
      </filter-mapping>
    
      <!-- web欢迎界面 -->
      <welcome-file-list>
        <welcome-file>login.jsp</welcome-file>
      </welcome-file-list>
      
    </web-app>
    
      
    复制代码

      struts.xml: 

    复制代码
    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
    <struts>
        <!-- 需要Spring时添加 -->
        <!-- <constant name="struts.objectFactory" value="spring" />  -->
    
        <package name="default" extends="struts-default">
            <action name="Login" class="com.HYOpticalComm.action.LoginAction" method="execute">
                <result name='success'>/index.jsp</result>
                <result name='error'>/login.jsp</result>
            </action>
        </package>
        
    </struts>
    复制代码

      LoginAction.java: 

    复制代码
    package com.HYOpticalComm.action;
    
    import com.opensymphony.xwork2.Action;
    
    public class LoginAction implements Action {
        private String username;
        private String password;
    
        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;
        }
    
        public String execute() throws Exception {
        if (this.getUsername().equals("admin")
            && this.getPassword().equals("123")) {
            return SUCCESS;
        } else {
            return ERROR;
        }
        }
    }
    复制代码

    (五)运行结果

    PS:本人新学习SSH框架,有不合理的地方请批评指正,本博客发布的所有代码都是经过我验证通过的。

     

     
     
  • 相关阅读:
    VBE2019、VBE2014、VBE2014_VB6三合一的下载、安装和使用(最新版2020.09.09)
    调用其他VBA工程中的过程和函数以及API函数
    QQ消息群发助手SendQQMessage的下载和使用
    VBA/VB6/VBS/VB.NET/C#/Python/PowerShell都能调用的API封装库
    Excel-DNA项目只用1个文件实现Ribbon CustomUI和CustomTaskpane定制【C#版】
    Excel-DNA项目只用1个文件实现Ribbon CustomUI和CustomTaskpane定制【VB.Net版】
    VSTO外接程序项目只用1个文件实现Ribbon CustomUI和CustomTaskpane定制【C#版】
    VSTO外接程序项目只用1个文件实现Ribbon CustomUI和CustomTaskpane定制【VB.Net版】
    Outlook邮件的右键菜单中添加自定义按钮
    VBA自动点击IE的浏览按钮、自动选择路径、自动关闭打开文件对话框
  • 原文地址:https://www.cnblogs.com/u0mo5/p/4168506.html
Copyright © 2011-2022 走看看