对于技术原理不作解析, 我的目标是让需要的朋友能一次性成功完成自己的第一个Struts 2.1项目.
1. 添加Apache Struts 2.1.6库
commons-fileupload-1.2.1.jar
commons-logging-api-1.1.jar
freemarker-2.3.13.jar
ognl-2.6.11.jar
struts2-core-2.1.6.jar
xwork-2.1.2.jar
与Struts 2.0相比, 多了commons fileupload.
下载地址: http://struts.apache.org/download.cgi#struts216
2. 在web.xml中配置FilterDispatcher
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <filter> <filter-name>struts2.1</filter-name> <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> </filter> <filter-mapping> <filter-name>struts2.1</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
3. 编写HiAction
package mp.likeming.action;
public class HiAction {
private String name;
public String execute() {
return "success";
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
}
4. 编写页面index.jsp及hi.jsp
index.jsp:
<%@page contentType="text/html; charset=UTF-8" %>
<%@taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>《开始Struts 2.1》 之 Struts 2.1 简单示例</title>
<style type="text/css">
<!--
* {
font-family:"Microsoft Yahie";
font-size:16px;
}
body {
text-align:center;
}
-->
</style>
</head>
<body>
<s:form action="hiAction">
称呼: <s:textfield name="name" /> <s:submit value="提交" />
</s:form>
</body>
</html>
hi.jsp:
<%@page contentType="text/html; charset=UTF-8" %>
<%@taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>《开始Struts 2.1》 之 Struts 2.1 简单示例</title>
<style type="text/css">
<!--
* {
font-family:"Microsoft Yahie";
font-size:16px;
}
body {
text-align:center;
}
.name {
font-weight:600;
}
-->
</style>
</head>
<body>
<p>Hi, <span class="name"><s:property value="name" /></span></p>
<p>感谢您关注Struts 2.1</p>
</body>
</html>
5. 编写struts.xml
<?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> <!-- 主题设置 --> <constant name="struts.ui.theme" value="simple" /> <package name="stuts2" extends="struts-default"> <action name="hiAction" class="mp.likeming.action.HiAction"> <result>hi.jsp</result> </action> </package> </struts>
附件为项目编译版, 可部署至Tomcat运行, 以查看运行效果.
如有不到之处, 请一定批评指正. 非常感谢!!!