Struts2关于Action的系统训练1

来源:互联网 发布:微信美化软件 编辑:程序博客网 时间:2024/05/01 02:19

这篇教程主要讲解了Struts中Action的一些知识点,通过对一些知识的了解从而掌握Action的使用和一些原理。废话不多说,下面就直接看我的训练步骤:

1安装myeclipse,tomcat。

2下载Struts文件压缩包,并解压到本地硬盘,

3使用myeclipse建立一个web project,使用javaee5.0,给项目配置jdk和tomcat服务器

4在src文件夹里添加Struts.xml配置文件,配置内容如下

<span style="font-size:18px;"><?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><span style="color:#ff0000;"><constant name="struts.devMode" value="true" /></span>   <package name="default" namespace="/" extends="struts-default">                <action name="hello">            <result>                /Hello.jsp            </result>        </action>    </package></struts></span>

其中红色部分是打开开发模式,这样改变配置文件就不用重启tomcat加载项目,默认是关闭。action名字为hello,结果是hello.jsp,

5,在WEB-INF文件夹下lib文件中添加Struts的jar包,包括

1)xwork-core-2.1.62)struts2-core-2.1.83)ognl-2.7.34)freemarker-2.3.155)commons-io-1.3.26)commons-fileupload-1.2.1

6下面是hello.jsp页面内容

<span style="font-size:18px;"><%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%><span style="color:#ff0000;"><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%</span>><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>   <span style="color:#ff0000;"> <base href="<%=basePath%>"></span>        <title> Hello Struts2</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0">    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">-->  </head>    <body>    Hello Struts2! <br>  </body></html></span>

7web.xml文件的配置文件为:

<span style="font-size:18px;"><?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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">  <display-name>Struts2_0100_Introduction</display-name>  <welcome-file-list>    <welcome-file>index.jsp</welcome-file>  </welcome-file-list> <span style="color:#ff0000;"> <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></span></web-app></span>

8在地址栏输入http://localhost:8080/项目名/hello.action就会打开hello.jsp页面,

9红色部分是核心代码。下面我就解释一下流程:

首先看http://localhost:8080/项目名/hello.action。客户端发送一个url到tomcat,tomcat根据项目配置文件web.xml找filter,首先查看<url-pattern>/*</url-pattern>,映射到

<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>这个类中,然后根据Struts.xml配置文件中的action名称为hello,就运行Action的方法,返回内容指引到hello.jsp页面。返回给用户。

10这是最简单的流程。慢慢理解一下,然后接下来的训练会逐渐添加要点。




0 0
原创粉丝点击