一、登陆struts2官网下载struts压缩包,下载struts-xx.xx-all.zip,这个包是最全的
Struts2官网地址:http://struts.apache.org/
二、解压下载下来的压缩包(我下载的是struts-2.3.20.1-all.zip)
a) 找到struts-2.3.20.1/apps/struts2-blank.war,解压
b) 找到lib文件夹:struts-2.3.20.1\apps\struts2-blank\WEB-INF\lib
三、Struts2的搭建工作一共三步:
a) 拷贝struts2项目需要的jar包到lib文件夹下;
b) 配置web.xml文件
c) 配置struts.xml文件
四、打开eclipse开发工具,新建DynamicWeb Project 项目,名称是:Struts2_blank
a) 将\apps\struts2-blank\WEB-INF\lib文件夹中的jar包全部拷贝到新建的项目
a) 配置web.xml文件
i. 从struts-2.3.20.1\apps\struts2-blank\WEB-INF文件夹下找到web.xml配置文件
拷贝下面这两行到中
<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>
a) 配置struts.xml文件
i. 从文件夹struts-2.3.20.1\apps\struts2-blank\WEB-INF\src\java下找到struts.xml文件,拷贝到项目Struts2_blank的src下
i. 将多余的删除,仅保留下面这几行即可:
<?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>
</struts>
a) 至此,struts2项目的搭建任务已经完成。下面写一个最简单的hello world例子;
b) 在WebContent文件夹下新建一个Login.jsp文件
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@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=ISO-8859-1">
<title>Sign On</title>
</head>
<body>
<s:form action="Login">
<s:textfield key="username" />
<s:textfield key="password" />
<s:submit />
</s:form>
</body>
</html>
a) 创建一个Action,如下
package example;
import com.opensymphony.xwork2.ActionSupport;
@SuppressWarnings("serial")
public class Login extends ActionSupport {
@Override
public String execute() throws Exception {
return SUCCESS ;
}
private String username ;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
private String password ;
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
h) 新建一个成功跳转的jsp页面success.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@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=ISO-8859-1">
<title>hello world</title>
</head>
<body>
welcome ${username}
</body>
</html>
i) 使用tomcat服务器启动项目
j) 打开浏览器输入url:http://localhost:8082/Struts2_blank/,页面如图:
k) 点击Sbumit,跳转到页面:
一个简单的struts2搭建完成 !
转载请注明出处:http://blog.csdn.net/u011159417/article/details/71122711