Struts2学习总结(二):第一个Struts2程序

1.创建Web项目,将Struts2支持的类型库文件添加到WEB-INF目录的lib文件夹下(只需要添加Struts的核心类库)


  • 文件上传支持类库
  • 处理IO操作的工具类库
  • Log4j日志支持类库
  • Freemarker模板语言支持类库
  • Ognl表达式语言类库
  • Struts2的核心类库

2.在web.xml文件中声明Struts2的过滤器,类名为:org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter

代码:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>StrutsTest</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>
	<!-- Struts2过滤器映射 -->
	<filter-mapping>
		<!-- 过滤器名称 -->
		<filter-name>struts2</filter-name>
		<!-- 过滤器映射 -->
		<url-pattern>/*</url-pattern>
	</filter-mapping>
</web-app>

3.在Web项目的源码文件夹下,创建名为struts.xml的配置文件,在文件中定义Struts2的Action对象。

<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
	"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
	<!-- 声明包 -->
	<package name="myPackage" extends="struts-default">
		<!-- 定义action -->
		<action name="first">
			<!-- 定义处理成功后的映射页面 -->
			<result>/first.jsp</result>
		</action>
	</package>
</struts>

<package>标签:声明一个包,通过name属性指定包的名称,并通过extends属性指定此包继承与struts-default包

<action>标签:用于定义Action对象,name属性用于指定访问此Action的URL(Action的配置要放在包空间)

<result>标签:用于定于处理结果和资源之间的映射关系

4.创建程序的主界面index.jsp。编写一个超链接,用于访问指向的Action对象

<%@ 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>主页</title>
</head>
<body>
	<a href="first.action">请求Struts2</a>
</body>

</html>

5.创建一个页面,是Action对象处理成功后的返回界面

<%@ 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>第一个Struts2程序</title>
</head>
<body>
	第一个Struts2程序!
	<br>
</body>
</html>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值