Struts2笔记1--快速入门

http://struts.apache.org/下载最新的Struts2 all,解压文件目录如下:

image

解压apps中的struts2-blank.war文件,这是一个简单的Struts实例。我们可以依照这个实例,来开始struts2的入门。

1、用jee版的Eclipse,创建一个Dynamic Web Project,命名为StrutsDemo

2、把struts2-blank的WEB-INFlib下的jar包,copy到StrutsDemo的WEB-INFlib下,这是支持Struts2项目的jar包。

image

3、在解压的struts2-blank的WEB-INF下找到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>/*</url-pattern>
    </filter-mapping>

代码copy到StrutsDemo的web.xml中,这是Struts2的过滤器,所有请求之前进行预处理。

4、把struts2-blank的WEB-INFclasses目录下struts.xml,copy到StrutsDemo中src目录下,并修改成

<?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>
    <constant name="struts.devMode" value="true" />
    
    <package name="default" namespace="/" extends="struts-default">
        <action name="hello">
            <result>
               /Hello.jsp
            </result>
        </action>
    </package>

</struts>

把hello这个action用Hello.jsp作为结果。

5、创建Hello.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>Hello</title>
</head>
<body>
    Hello Struts2!
</body>
</html>

6、把项目部署到Tomcat中,在浏览器中输入:http://localhost:8080/StrutsDemo/hello,即可以看到页面显示如下:

image

则struts项目搭建成功~可以折腾了~微笑

原文地址:https://www.cnblogs.com/zsp0817/p/3519820.html