Struts2学习(二)---一个struts2的登陆例子

struts2作为一个很成熟的mvc框架,经久不衰,虽然有下降趋势,但是很多公司还是沿用struts2作为mvc框架,接下来就先来完成一个struts2的一个登陆例子,只为了解流程和基本原理。

一:概述
Struts2的核心是一个Filter,Action可以脱离web容器,那么是什么让http请求和action关联在一起的,下面我们深入源码来分析下Struts2是如何工作的。

FilterDispatcher API 写道

Deprecated. Since Struts 2.1.3, use StrutsPrepareAndExecuteFilter
instead or StrutsPrepareFilter and StrutsExecuteFilter if needing
using the ActionContextCleanUp filter in addition to this one

ps:这里附上我看的博文http://www.iteye.com/topic/829843,博主也是写的贼好,在我的理解,StrutsPrepareAndExecuteFilter,struts2核心就是Filter,StrutsPrepareAndExecuteFilter的主要作用就是完成基础的filter参数配置(如我们在web.xml中配置的filter的初始化参数),解析struts相关的default.properties,struts-default.xml,struts-plugin.xml,struts.xml等等,解析完就会有ActionMapping之类的,匹配请求,重点是创建上下文ActionContext(存session,request,application之类的),之后有请求来的时候就会把请求匹配ActionMapping。

二:实现
这里我就不传github了,给个项目结构图吧
这里写图片描述

1.鉴于常规情况官方推荐使用StrutsPrepareAndExecuteFilter替代FilterDispatcher,我们此文将剖析StrutsPrepareAndExecuteFilter,其在工程中作为一个Filter配置在web.xml中,配置如下:

<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>*.action</url-pattern>
    </filter-mapping>

2.我用的是maven,先导入一个jar包
(想自己创建骨架请看我另一篇博文http://blog.csdn.net/shouldnotappearcalm/article/details/52353712

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.nsu</groupId>
    <artifactId>ClassStudyStruts2_01</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>


    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.apache.struts/struts2-core -->
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-core</artifactId>
            <version>2.3.28</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.2</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.freemarker/freemarker -->
        <dependency>
            <groupId>org.freemarker</groupId>
            <artifactId>freemarker</artifactId>
            <version>2.3.23</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.struts.xwork/xwork-core -->
        <dependency>
            <groupId>org.apache.struts.xwork</groupId>
            <artifactId>xwork-core</artifactId>
            <version>2.3.28</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/ognl/ognl -->
        <dependency>
            <groupId>ognl</groupId>
            <artifactId>ognl</artifactId>
            <version>3.1.14</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/commons-lang/commons-lang -->
        <dependency>
            <groupId>commons-lang</groupId>
            <artifactId>commons-lang</artifactId>
            <version>2.5</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/jstl/jstl -->
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>


    </dependencies>

</project>

3.login.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
    <c:if test="${errorMessage !=null}">${errorMessage }</c:if>
    <form action="login.action">
        <input type="text" name="username" value="" />
        <input type="password" name="password" value="" />
        <button type="submit">登陆</button>
    </form>
</body>
</html>

4.index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
    index
    ${username }
</body>
</html>

4.struts.xml(前面懒得找那个dtd规范文件头的,在项目的Libirary里面Maven Dependencies里面struts-core-2.3.28.jar里面,有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="gzr" namespace="/" extends="struts-default">
        <action name="login" class="com.nsu.edu.action.UserAction">
            <result name="ok">/index.jsp</result>
            <result name="fail">/login.jsp</result>
        </action>
    </package>

</struts>

这里的配置还是挺简单的,name随便起,namespace你如果写个/xx,那你的login请求就应该是/xx/login.action才能请求过来,这里的action标签可以指定Action类的method,还有种写法 action name=”user_*”,method=”{1}”,有兴趣的可以去查查如果是user_registPage.action 调用方法为 registPage。

5.最后就是UserAction,不难,自己看吧。

package com.nsu.edu.action;

public class UserAction {

    String username;
    String password;
    String errorMessage;

    /**
     * 业务组件
     * @return
     */
    public String execute(){
        System.out.println(username+","+password);
        if("gzr".equals(username.trim())){
            if("Aa11223344".equals(password.trim())){
                return "ok";
            }
            else{
                errorMessage="用户名对了,密码错了";
                return "fail";
            }
        }
        else{
            errorMessage="用户名就错了";
            return "fail";
        }
    }

    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 getErrorMessage() {
        return errorMessage;
    }

    public void setErrorMessage(String errorMessage) {
        this.errorMessage = errorMessage;
    }


}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值