一个简单登录的Struts2的示例

一. 框架图

这里写图片描述

二.步骤

1./WebRoot/WEB-INF/lib下添加7个Struts2必须的jar包;
下载地址
2.web.xml中配置关于Struts2的filter;
3.建立Login.jsp, LoginFailure.jsp以及LoginSuccess.jsp文件;
4.建立UserAction.java文件;
4.src下建立struts.xml文件;

三.源代码

3.1web.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">
  <display-name></display-name> 
  <welcome-file-list>
    <welcome-file>Login.jsp</welcome-file>
  </welcome-file-list>

  <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-app>
3.2 Login.jsp
<%@ 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>用户登录</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">
    -->
    <style>
        #mainContent{
             display:block;

             width:40%;
             height:70%;
             padding-top:5px;

        }
    </style>
  </head>
  <body>
    <center>
        <h2>用户登录</h2>
        <hr>
        <!-- action的配置很重要,一定要记着写全“login.action”,login是在struts.xml中的action,是需要配置的 -->
        <form action ="login.action" method="post" >
        <div id="mainContent">
        <table>
           <tr>
               <td>用户名:</td>
               <td><input type="text" name="uname"></td>
           </tr>
           <tr>
               <td>密码:</td>
               <td><input type="text" name="upassword"></td>
           </tr>
           <tr>
                <td colspan="2">
                    <input type="submit" value="登录">
                    <input type="reset"  value="重置">
                </td>
           </tr>
        </table>
        </div>
        </form>
    </center>
  </body>
</html>
3.3 LoginSuccess.jsp
<%@ 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>登录成功</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>
    <center>  
     <h2>登录成功!</h2>     
    </center>
  </body>
</html>
3.4 LoginFailure.jsp
<%@ 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>登录失败</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>
     <center>
    <h2>登录失败 !</h2>
    </center>
  </body>
</html>
3.5UserAction.java
package com.action;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class UserAction extends ActionSupport{
    private String uname;
    private String upassword;
    public String getUname() {
        return uname;
    }
    public void setUname(String uname) {
        this.uname = uname;
    }
    public String getUpassword() {
        return upassword;
    }
    public void setUpassword(String upassword) {
        this.upassword = upassword;
    }

    public String execute() throws Exception{

        System.out.println("姓名:"+uname);
        System.out.println("密码:"+upassword);            
        if("James".equals(getUname())&&"123456".equals(getUpassword())){
            return "success";

        }else{
            return "failure";
        }       
    }
}
3.6 struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
         "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
         "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
    <package name="default"  extends="struts-default" namespace="/">
         <action name="login" class="com.action.UserAction" method="execute">
             <result name="success">/LoginSuccess.jsp</result>
             <result name="failure">/LoginFailure.jsp</result>
         </action>
    </package>
</struts>

四.注意事项

1.struts.xml中的package的namespace一定要写,name不重复已有的package就好,extends表示继承的package

<package name="default"  extends="struts-default" namespace="/">

2.struts.xml中的action的method一定要写
name的“login”与Login.jsp中的action=“login.action”是对应的,
class是已经src下的java处理文件;
method是默认执行execute的,但是建议写着。

<action name="login" class="com.action.UserAction" method="execute">
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值