Struts2入门

--------------------siwuxie095

   

   

   

   

   

   

   

   

Struts2 环境搭建

   

   

1、先下载相关库文件,下载链接:

   

1http://struts.apache.org/download

   

2https://struts.apache.org/download.cgi

   

3http://archive.apache.org/dist/struts/

   

   

注意:这里选择下载 Struts 2.3 版本,下载后,

将 struts-2.3.33-all.zip 解压一览:

   

   

   

   

2、导入 Struts2 中的 jar 包

   

1)打开 apps 文件夹,其中是官方提供的 Struts2 的示例程序

   

   

   

   

2)选择 struts2-blank.war,它是 Struts2 的一个的工程,

将之解压后一览:

   

   

   

   

3)将 WEB-INF/lib 文件夹中的包(共 13 个包)导入

   

   

   

   

   

   

   

   

创建部署描述文件

   

   

部署描述文件中配置 Struts2 的核心过滤器

   

   

1、创建 XML 格式的部署描述文件

   

部署描述文件的名称和位置是固定的

   

位置:必须在 WEB-INF

   

名称:必须是 web.xml

   

   

   

2、快捷创建部署描述文件 web.xml

   

选择工程名,右键->Java EE Tools->Generate Deployment Descriptor Stub

   

   

提示:示例程序 struts2-blank web.xmlWEB-INF 文件夹下

   

   

   

3、配置 Struts2 的核心过滤器

   

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>

   

   

   

4、最后

   

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" version="3.1">

<display-name>TestStruts2</display-name>

<welcome-file-list>

<welcome-file>index.html</welcome-file>

<welcome-file>index.htm</welcome-file>

<welcome-file>index.jsp</welcome-file>

<welcome-file>default.html</welcome-file>

<welcome-file>default.htm</welcome-file>

<welcome-file>default.jsp</welcome-file>

</welcome-file-list>

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

   

   

   

   

   

   

   

创建 Action 类

   

   

包名:com.siwuxie095.action

类名:HelloAction.java

   

HelloAction.java:

   

package com.siwuxie095.action;

   

public class HelloAction {

/*

* (1) 每次访问 Servlet,都会执行 service() 方法

*

* 手动创建 Servlet:写一个类,继承自 HttpServlet

* 并重写方法。到 web.xml 中配置 Servlet 访问路径

*

*

* (2) 每次访问 Action,默认都会执行 execute() 方法

*

* 在核心配置文件 struts.xml 配置 Action 的访问路径

*/

public String execute() {

return "ok";

}

}

   

   

   

   

   

   

   

创建 Struts2 的核心配置文件

   

   

核心配置文件中对 Action 进行配置

   

   

1、创建 XML 格式的配置文件

   

核心配置文件的名称和位置是固定的

   

位置:必须在 src

   

名称:必须是 struts.xml

   

   

   

2、在配置文件中引入 XML 约束

   

struts-2.3.dtd

   

「XML约束有 DTD 和 Schema 两种,在 Struts2 的配置文件

中引入的是 DTD 约束(目前)」

   

该文件在 srccoresrcmain esources 文件夹下:

   

   

   

struts.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">

   

   

提示:示例程序 struts2-blank struts.xmlWEB-INFsrcjava

WEB-INFclasses 文件夹下均有

   

   

   

3、配置 Action

   

struts.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">

<!--

http://struts.apache.org/dtds/struts-2.3.dtd

dtd 文件是用于提示的文件,联网即有提示信息,也可

手动添加:Window->Preferences->XML->XML Catalog

点击 Add 添加即可,Location 即路径,Key 即上面的链接,

Key type URI

-->

   

<!-- 根标签 -->

<struts>

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

<!-- name:访问名称 -->

<action name="hello" class="com.siwuxie095.action.HelloAction">

<!--

配置 execute() 方法的返回值对应的返回页面,

配置结果页面的跳转

-->

<result name="ok">/hello.jsp</result>

</action>

</package>

   

</struts>

   

   

   

4、在浏览器中进行访问,访问路径

   

1)http://localhost:8080/TestStruts2/hello

   

2)http://localhost:8080/TestStruts2/hello.action

   

3)http://localhost:8080/TestStruts2/hello.jsp

   

   

「最好加上 .action.jsp,以防某些浏览器无法访问」

   

   

   

   

   

   

   

工程结构目录如下:

   

   

   

   

hello.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">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>Hello</title>

</head>

<body>

<h1>Hello Struts2 ...</h1>

</body>

</html>

   

   

   

   

   

   

   

注:

   

   

   

   

   

   

   

   

   

   

   

【made by siwuxie095】

原文地址:https://www.cnblogs.com/siwuxie095/p/7308569.html