1、建立一个Web项目
打开MyEclipse(我使用的是MyEclipse 10版本),建立一个Web Project。步骤:File/New/Project/
Web Project/Next,进入New Web Project界面,输入Project Name:StrutsTest。J2EE Specification Level选择Java EE 6.0(或者其他版本),截图如下:![]()
点击Finish,进入下一步。2、添加Struts2基本类库
添加Struts2的6个主要类库(足以完成本文需要实现的功能。在这之前需要下载struts-2.1.6-all这个包,网址为http://struts.apache.org/点击打开链接.下载完成后可以从\struts-2.1.6-all\struts-2.1.6\lib中找到需要的jar包):commons-fileupload-1.2.1.jar、commons-logging-1.0.4.jar、freemarker-2.3.13.jar、ognl-2.6.11.jar、struts2-core-2.1.6.jar、xwork-2.1.2.jar六个包。
点击项目名,单击右键,选择Build Path/Configure Build Path菜单项,出现下图所示界面,单击Add External JARs按钮,进入\struts-2.1.6-all\struts-2.1.6\lib中找到需要的jar包,单击OK按钮完成类库的添加。
打开项目中的WebRoot/WEB-INF/wen.xml,修改其代码如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
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_3_0.xsd">
<filter>
<filter-name>Struts 2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>Struts 2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
4、创建hello.jsp文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
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_3_0.xsd">
<filter>
<filter-name>Struts 2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>Struts 2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
选择WebRoot/New/jsp菜单项,在File Name中输入文件名“hello.jsp”,修改后的代码如下:
<%@ page language="java" pageEncoding="UTF-8"%>
<html>
<head>
<title>struts2简单应用</title>
</head>
<body>
<form action="struts.action" method="post">
请输入姓名:<input type="text" name="name"/><br>
<input type="submit" value="提交">
</form>
</body>
</html>
当用户在输入框中输入姓名后,单击【提交】按钮就会交给struts.action处理,Struts 2的拦截器就会起作用,将用户请求转发给对应的Action类。第5步将编写Action类。
5、创建Action实现类
先建立包。右击src文件夹,选择New/Package,在name中输入包名:org.action,右击改包,选择New/class,建立class,命名为:StrutsAction,修改后代码如下:
package org.action;
import java.util.Map;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class StrutsAction extends ActionSupport {
private String name;
public String getName(){
return name;
}
public void setName(String name){
this.name=name;
}
public String execute()throws Exception{
if(!name.equals("HelloWorld")){
Map request=(Map)ActionContext.getContext().get("request");
request.put("name", getName());
return "success";
}else{
return "error";
}
}
}
package org.action;
import java.util.Map;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class StrutsAction extends ActionSupport {
private String name;
public String getName(){
return name;
}
public void setName(String name){
this.name=name;
}
public String execute()throws Exception{
if(!name.equals("HelloWorld")){
Map request=(Map)ActionContext.getContext().get("request");
request.put("name", getName());
return "success";
}else{
return "error";
}
}
}
该Java类中有一个属性name,生成了getter和setter方法,这个属性名要和JSP文件name的属性值对应,当执行该Action类时,就会通过该变量的setter和getter方法为该变量赋值和取值,然后再调用execute()方法。
6、创建并配置struts.xml文件
右击src文件夹,选择New/File菜单项,在File name框中输入: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>
<package name="default" extends="struts-default">
<action name="struts" class="org.action.StrutsAction">
<result name="success">/welcome.jsp</result>
<result name="error">/error.jsp</result>
</action>
</package>
</struts>
7、创建welcome.jsp文件(成功跳转界面)
选择WebRoot/New/jsp菜单项,在File Name中输入文件名“welcome.jsp”,修改后的代码如下:
<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<html>
<head>
<title>struts2简单应用</title>
</head>
<body>
<s:property value="#request.name"/>你好,祝你学习愉快!
</body>
</html>
<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<html>
<head>
<title>struts2简单应用</title>
</head>
<body>
<s:property value="#request.name"/>你好,祝你学习愉快!
</body>
</html>
8、创建error.jsp文件(出错界面)
选择WebRoot/New/jsp菜单项,在File Name中输入文件名“error.jsp”,修改后的代码如下:
<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<html>
<head>
<title>struts2简单应用</title>
</head>
<body>
你好,出错了!
</body>
</html>
右击项目名称,选择Run As/3 MyEclipse server application菜单项,出现Server Selection对话框,选择容器,笔者使用的是Tomcat 5.x版本。等容器启动完毕后,输入:http://localhost:8080/StrutsTest/hello.jsp,即可看到成果。