用struts2实现简单的登录

首先先写一个简单的的登录界面,代码如下:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!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>
      <s:form action="login">
          <s:textfield name="username" key="用户名"/>
          <s:textfield name="password" key="密码"/>
          <s:submit key="登录dd"/>
      </s:form>
  </body>
</html>

然后,写一个Action,命名为LoginAction,代码如下

package com.dhy.action;

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

public class LoginAction extends ActionSupport{
    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;
    }
    @Override
    public String execute() throws Exception {
        System.out.println(getUsername()+getPassword());
        // TODO Auto-generated method stub
        ActionContext ctx=ActionContext.getContext();
        //通过ActionContext访问application范围的属性值
        Integer counter=(Integer)ctx.getApplication().get("counter");
        if(counter==null){
            counter=1;
        }else{
            counter=counter+1;
        }
        ctx.getApplication().put("counter", counter);
        //通过ActionContext设置session范围的属性
        ctx.getSession().put("user", username);
        if(getUsername().equals("jocean")&&getPassword().equals("jocean")){
            //通过ActionContext设置request范围的属性
            System.out.println("in");
            ctx.put("tip","服务器提示:您已经登录成功");
            return  SUCCESS;
        }
      //通过ActionContext设置request范围的属性
        ctx.put("tip", "服务器提示:登录失败!");
        return  ERROR;
    }
}

配置struts2.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>
    <!-- 指定全局国际化资源文件 -->
    <!-- add -->
  <!--   <constant name="struts.custom.i18n.resources" value="mess"/> -->
    <!-- End -->
    <!-- <constant name="struts.enable.DynamicMethodInvocation" value="false" />
    <constant name="struts.devMode" value="true" /> -->

    <package name="crazyit" namespace="/" extends="struts-default">
          <action name="login" class="com.dhy.action.LoginAction">
              <result name="error">/WEB-INF/content/error.jsp</result>
              <result name="success">/WEB-INF/content/success.jsp</result>
          </action>
    </package>


 <!--    <include file="example.xml"/> -->

    <!-- Add packages here -->

</struts>

然后,写success.jsp和error.jsp,其中success.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>
       访问次数:${applicationScope.counter}<br/>
       ${sessionScope.user},您已经登录!<br/>
       ${requestScope.tip} 
  </body>
</html>

error.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>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>
    ${requestScope.tip}
  </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">
   <!-- 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>
  <!-- End -->

</web-app>

目录结构如下图所示:
这里写图片描述
运行结果如下:
这里写图片描述
这里写图片描述

  • 0
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值