一、搭建Struts2开发环境
1、开发所依赖的最基本的jar
struts2-core-2.x.x.jar :Struts 2框架的核心类库
xwork-core-2.x.x.jar :XWork类库,Struts 2在其上构建
ognl-2.6.x.jar :对象图导航语言(Object Graph Navigation Language),struts2框架通过其读写对象的属性
freemarker-2.3.x.jar :Struts 2的UI标签的模板使用FreeMarker编写
commons-logging-1.x.x.jar :ASF出品的日志包,Struts 2框架使用这个日志包来支持Log4J和JDK 1.4+的日志记录
commons-fileupload-1.2.1.jar 文件上传组件,2.1.6版本后必须加入此文件
2、Struts2的应用配置文件
Struts2的默认配置文件为struts.xml,该文件在开发时默认在src文件下,编译后在WEB-INF/class目录下。若是官方下载的压缩包中,则struts-2.3.4\src\core\src\test\resources目录下。该配置文件的模版如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="" namespace="" extends="struts-default">
<action name="" class="com.action.FirstAction" method="execute" >
<result name="">/xxx.jsp</result>
</action>
</package>
</struts>
3、Struts在Web中启动的配置
在Struts2中是通过Filter启动的。在web.xml配置如下:
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
<init-param>
<param-name>config</param-name>
<param-value>struts-default.xml,struts-plugin.xml,../struts.xml</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
其中,在StrutsPrepareAndExecuteFilter的init()方法中将会读取类路径下默认的配置文件struts.xml完成初始化操作。
若不想配置文件struts.xml设置为默认路径下,在非默认路径下,如上所示,设置路径在WEB-INF下。
注意:struts2读取到struts.xml的内容后,以javabean形式存放在内存中,以后struts2对用户的每次请求处理将使用内存中的数据,而不是每次都读取struts.xml文件。
二、编写第一个基于Struts2的web程序
1、编写Action类
import com.opensymphony.xwork2.ActionSupport;
public class FirstAction extends ActionSupport {
private int operand1;
private int operand2;
public String execute() throws Exception {
if(getSum()>=0){
//代数和为非负整数
return "positive";
}else{
//代数和为负整数
return "negative";
}
}
public int getOperand1() {
return operand1;
}
public void setOperand1(int operand1) {
this.operand1 = operand1;
}
public int getOperand2() {
return operand2;
}
public void setOperand2(int operand2) {
this.operand2 = operand2;
}
public int getSum(){
return operand1+operand2;
}
}
2、sum.jsp主页面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>输入操作数</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>
求代数和<br>
<s:form action="mystruts/sum.action" >
<s:textfield name="operand1" label="操作数1"/><br>
<s:textfield name="operand2" label="操作数2"/><br>
<s:submit value="代数和"/>
</s:form>
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>显示代数和</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> 代数和为非负整数:<h1><s:property value="sum"/></h1> </body> </html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>显示代数和</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>
代数和为负整数:<h1><s:property value="sum"/></h1>
</body>
</html>
URL访问路径:http://localhost:8080/struts2/sum.jsp
输入数字,点提交后,URL访问路径:http://localhost:8080/struts2/mystruts/sum.action
注:struts2中action的URL路径由两部份组成:包的命名空间(namespace)+action的名称