1. 环境搭建
按照之前的文章配置好myeclipse的jdk和tomcat,并新建一个web项目后,可开始动手配置与struts2相关的地方了。首先去struts的官网下载好最新的struts2代码,然后解压,可得到一个名为struts-x.x.x的文件夹。进入apps,解压struts2-blank.war(若不能直接解压,可修改后缀名后解压),得到文件夹struts2-blank。进入struts-x.x.x\apps\struts2-blank\WEB-INF\lib,拷贝所有jar文件,复制到web项目下的webRoot->WEB-INF->lib下。然后拷贝struts-x.x.x\apps\struts2-blank\WEB-INF\web.xml,复制到web项目下的webRoot->WEB-INF。最后拷贝struts-x.x.x\apps\struts2-blank\WEB-INF\classes\struts.xml,复制到web项目下的src文件夹中。
2. struts.xml代码
structs.xml代码复制到项目中,请注释掉<struts>到</struts>之间的所有内容。修改后的代码如下:
<?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>
<!--
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="true" />
<!-- package的name可以随便取 -->
<package name="default" namespace="/" extends="struts-default">
<default-action-ref name="index" />
<global-results>
<result name="error">/error.jsp</result>
</global-results>
<global-exception-mappings>
<exception-mapping exception="java.lang.Exception" result="error"/>
</global-exception-mappings>
<!-- action的name与jsp中的提交处指定的action名应相同。比如:在jsp页面有个form,则form提交后的action应与struts.xml中的某action名相同 -->
<action name="index">
<result type="redirectAction">
<param name="actionName">HelloWorld</param>
<param name="namespace">/example</param>
</result>
</action>
</package>
<include file="example.xml"/>
-->
<!-- Add packages here -->
<constant name="struts.devMode" value="true" />
<constant name="struts.configuration.xml.reload" value="true"/>
<package name = "hello" namespace = "/hello" extends = "struts-default">
<action name = "hello" class = "com.sun.struts.HelloWorld">
<result>/hello_world.jsp</result>
</action>
</package>
</struts>
3. Action代码
我们还需要action来为我们返回一个代表成功的值,从而展现正确的结果页面。右键src,选择new个class,为package起名为com.sun.struts,为class起名为HelloWorld.java。类代码如下:
package com.sun.struts;
import com.opensymphony.xwork2.ActionSupport;
public class HelloWorld extends ActionSupport {
public String execute(){
return SUCCESS;
}
}
4. result代码
当action执行成功后,就会按照struts中所描述的,显示/hello_world.jsp页面了。页面代码如下:
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</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>
hello world! <br>
</body>
</html>
5. 访问
完成如上步骤后,就应该可以成功访问url: http://localhost:8080/web项目名/hello/hello 了,页面应该显示:hello world!