初识 Struts2 - 先来生成一个登录页面体验一下

1.首先下载Struts2 必要的包(大家可以到我的云盘直接下载 struts2 必要包 如果失效了,大家也可以自行下载,我会在下面放一张所需包的图片)

这里写图片描述

2.然后在 eclipse 中创建一个 动态web工程

3.然后大家把之前的下载的包全部拷贝到 WEBContent/WEB-INF/lib/ 这个目录下面。(注意:如果你是在我云盘中下载的,其中有两个文件web.xml 和 struts.xml 不用拷贝到 lib目录下面,这两个文件后面会用到

4.然后在 WEB-INF 目录下面生成一个 web.xml 文件(当然你也可以直接将我的文件拷贝到该目录下),如果你是自己生成的,那么你就将下面的代码拷贝到这个文件中


<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <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>

当启动一个web项目时,web容器(这里指tomcat)会去读取他的配置文件 web.xml,上面的代码表示可以拦截到所有的 url 请求。

5.在 java Resources/src/ 目录下生成一个 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="han.LoginAction">
            <result name="success">success.jsp</result>
            <result name="error">error.jsp</result>
        </action>
    </package>
    
</struts>

上面代码的意思就是 拦截到有一个 login.action 的请求,因为我们在登录页面里面的 action 中写的 login,所以拦截器会拦截到这个请求,然后执行 han 包下面的 LoginAction 类中的 execute方法,根据这个方法返回的字符串来决定跳转到哪一个页面(现在我们还没有创建 han包,它会在后面创建)

6.这时基本把struts2 的环境给配置好了。

7.在 WebContent/ 目录下生成三个 jsp 文件(注意:不要把jsp文件放到 META-INF 和 WEB-INF 下面了),以下是三个jsp文件:

loginForm.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title><s:text name="loginPage"/></title>
</head>
<body>
    <s:form action="login">
        <s:textfield name="username" key="user" />
        <s:textfield name="password" key="pass" />
        <s:submit key="login" value="submit" />
    </s:form>
</body>
</html>

不用管 <s:textfield.../>这个标签是什么意思,为什么与 html 标签不同,因为这时 struts2 特有的标签

error.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>
    wow something wrong
</body>
</html>
success.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>
    this is successful page
</body>
</html>

8.在 src 目录下生成一个 han包(这个包名随便,只是我这个例子中使用的是这个包而已,大家可以自行取名,但是相应在 struts 中 action 的class 也要改变),在这个包下面新建一个 LoginAction 类,这个类继承自 ActionSupport,并且重写 execute方法,代码如下;

package han;

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;
    }

    public LoginAction() {
        
    }


    //定义处理用户请求的 execute方法,这个方法会被程序自动调用
    public String execute() throws Exception {
        //当用户名与密码都为 123 的时候,返回成功字符串,否则返回 错误字符串
        if(getUsername().equals("123") && getPassword().equals("123")){
            System.out.println(5656);
            ActionContext.getContext().getSession().put("user", getUsername());
            System.out.println(3344);
            return SUCCESS;
        }
        return ERROR;
    }

}

9.这时基本上已经完成了,然后右键 loginForm.jsp,运行run as命令,启动服务器,进入 登录页面,如果用户名和密码都输入 123,那么进入 success.jsp 页面,否则进入error.jsp页面。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 本文包括: 1、Struts 2 概述2、Struts 2 快速入门3、Struts 2 的执行流程4、配置 st...
    廖少少阅读 8,192评论 3 13
  • 概述 什么是Struts2的框架Struts2是Struts1的下一代产品,是在 struts1和WebWork的...
    inke阅读 6,720评论 0 50
  • Apache Struts 2是一种流行的 Java模型 - 视图 - 控制器(MVC)框架,成功地结合了 Web...
    wyude阅读 2,875评论 0 0
  • 目录 1. 什么是Struts2 2. Struts2下载 3. Struts2的目录结构 4. Struts2中...
    深海鱼Q阅读 4,551评论 0 16
  • 1983年,黄家驹通过乐器行的老板认识了同为音乐人的鼓手叶世荣,志同道合的年轻人从那天起走向了人生最为辉煌一段旅程...
    6c7d8c349f45阅读 3,375评论 2 2

友情链接更多精彩内容