一、下载Struts开发包
1.可以在百度上搜素“struts”关键词,找到官网,点击download,亦可以直接输入
http://struts.apache.org/download.cgi进入下载页面,
在该页面可以下载最新版的struts。
2.将下载的struts-2.3.20-all.zip文件解压,其中lib目录下的jar包就是struts开发所需要的jar包。
这里的jar包比较多,可以根据需要来选择,并不是所有的都需要。
几个核心的jar包如下:
将这几个jar包放到WEB-INF目录的lib目录下就行了。
二、创建一个简单的Demo
1.在Eclipse中创建StrutsDemo工程
新建一个Dynamic Web Project,命名为StrutsDemo。
这个不多说了。。。
2.在Eclipse中部署Struts开发包
将上面所说的几个核心jar包拷贝到工程的WEB-INF目录的lib目录下。
3.在web.xml文件中配置Struts
新建一个或者从其他项目中拷贝一个web.xml文件都行,放到WEB-INF目录下。一个最简单的Struts项目的web.xml文件配置如下:
<?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_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>HdfsOperation</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>
<!-- Struts2过滤器(拦截器)配置 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<!-- 这个配置的是struts2拦截器的拦截规则
/* 表示拦截所有请求,如果页面中使用了struts标签,这里的拦截器一定要设置成/*
-->
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
4.编写Struts处理程序Action
package com.strutsdemo.action;
import com.opensymphony.xwork2.ActionSupport;
public class TestAction extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = 1L;
// Action属性
private String msg;
// getter方法
public String getMsg() {
return msg;
}
// setter方法
public void setMsg(String msg) {
this.msg = msg;
}
// Action默认的执行方法
@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
return SUCCESS;
}
}
说明:
(1)通常我们编写Action会继承ActionSupport类,ActionSupport类中定义了一些常量和一个默认的方法execute()。
(2)一个简单的Action通常由一些属性、属性对应的setter和getter方法和一些执行方法组成。setter和getter方法是让Struts调用的,Struts拦截到请求后,会从请求中解析出对应的参数并赋值给Action的属性。这里就涉及到一个对应关系,即请求中的参数是如何和Action中的属性对应的?Struts是根据参数的名称来和Action的属性对应的,确切的说是和setter方法对应,即把setter方法的方法名(如setMsg)去掉set之后的部分首字母小写(如msg),如果请求中正好有个参数名叫msg,struts就会调用setMsg方法,将参数值赋值给msg属性。
(3)每个Action都有一个默认的执行方法,即execute方法,该方法不需要显示地在struts.xml文件中配置。如果我们想要让其他的方法来处理请求,就需要显示地在struts.xml文件中配置了,这个接下来会说。
5.编写Struts配置文件struts.xml
在src目录下新建struts.xml文件,我们的Action要想起作用,需要在这里面配置。
<?xml version="1.0" encoding="GBK"?>
<!-- 指定Struts2配置文件的DTD信息 -->
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
"http://struts.apache.org/dtds/struts-2.1.7.dtd">
<!-- Struts2配置文件的根元素 -->
<struts>
<package name="default" extends="struts-default">
<!-- 配置Action
name 请求的action的名称
class Action的处理类
result 返回结果的页面
-->
<action name="showMsg" class="com.strutsdemo.action.TestAction">
<!-- 处理成功时返回的页面 -->
<result name="success">/seccess.jsp</result>
<!-- 出错时返回的页面 -->
<result name="error">/error.jsp</result>
</action>
</package>
</struts>
说明:struts.xml文件相当于一个映射文件,如上所示,struts拦截器拦截到请求后,会根据action的name属性匹配到对应的Action类,默认情况下一个action的name对应到一个Action类的execute方法。如果我们不想使用默认的execute方法,则可以在struts.xml文件中做显示的配置,指明method属性,如下代码:
<action name="showMsg2" class="com.strutsdemo.action.TestAction" method="showMsg">
<!-- 处理成功时返回的页面 -->
<result name="success">/success.jsp</result>
<!-- 出错时返回的页面 -->
<result name="error">/error.jsp</result>
</action>
如果这样配置了,那么Action类中就要有相应的showMsg方法:
package com.strutsdemo.action;
import com.opensymphony.xwork2.ActionSupport;
public class TestAction extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = 1L;
// Action属性
private String msg;
// getter方法
public String getMsg() {
return msg;
}
// setter方法
public void setMsg(String msg) {
this.msg = msg;
}
// Action默认的执行方法
@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
return SUCCESS;
}
// 这个是自定义的方法,需要在struts.xml文件中配置
public String showMsg(){
return SUCCESS;
}
}
6.编写测试页面
在WebContent目录下新建index.jsp、success.jsp、error.jsp。
在index.jsp中输入如下代码:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!-- 引入struts标签库 -->
<%@ taglib prefix="s" uri="/struts-tags"%>
<!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>Struts测试页面</title>
</head>
<body>
<!-- struts标签 -->
<s:form action="showMsg">
<s:textfield name="msg" value="" label="MSG"></s:textfield>
<s:submit></s:submit>
</s:form>
</body>
</html>
说明:上面代码是用struts标签构建的表单,其中action属性指定表单提交到哪个action去处理。我们前面配置了两个action,一个是showMsg,另一个是showMsg2,这两个action分别对应TestAction类中的execute方法和showMsg方法,执行的效果都是一样的。
在success.jsp中输入如下代码:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!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>Success Page</title>
</head>
<body>
<!-- 输出Action中的msg属性 -->
<s:property value="msg"/>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!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>Error Page</title>
</head>
<body>
<%
out.print("ERROR!");
%>
</body>
</html>
7.运行查看效果
在浏览器中输入如下网址:
http://localhost:8080/StrutsDemo/index.jsp
在打开的网页中,输入文本,点击submit,就可以看到页面跳转情况。如果中间没有出错的话,页面会跳转到success.jsp页面,并输出之前填的文本;如果中间环节有错,会跳转到error.jsp页面。