1. 使用的工具

eclipse-jee-luna-SR2-win32

struts-2.3.24

apache-tomcat-6.0.44\

2.   编码

2.1  创建web工程

      在eclipse中选择file->New->Dynamic web Project

      这里使用项目名称为sshtest,创建的工程目录如下:其中build存放class文件;src存放java源文件; WebContent 存放structs2的lib库、jsp文件及xml配置文件

     

    

structs2基本程序创建_java

2.2  Jar包

  开发Struts 2程序最少需要的JAR如下:

struts2-core-2.3.24.jar :          Struts 2框架的核心类库
      xwork-core-2.3.24.jar :             XWork类库,Struts 2在其上构建
      ognl-3.0.6.jar :                          对象图导航语言(Object Graph Navigation Language),struts2框架通过其读写对象的属性
      freemarker-2.3.22.jar :            Struts 2的UI标签的模板使用FreeMarker编写
      commons-logging-1.1.3.jar :ASF出品的日志包,Struts 2框架使用这个日志包来支持Log4J和JDK 1.4+的日志记录。
    commons-fileupload-1.3.1.jar

    commons-lang3-3.2.jar:  

   讲上述jar拷贝到sshtest/WebContent/WEB-INF/lib目录下面,最后的结果如图所示:

 

structs2基本程序创建_xml_02

 

        图 2-1  提取structs 2中的jar

2.3 编写JAVA 与JSP

2.3.1  java

   在如图所示的位置创建java源文件并创建类及执行函数:

 

structs2基本程序创建_java_03

   其中HelloWorld.java的源代码如下:

/* 包名,后面配置文件会使用 */
package com.xhj.ssh.action;

/**
* <code>Set welcome message.</code>
*/
/* <span style="color:#FF0000;">注意下面的execute方法,后面的配置文件中会用到</span> */
public class HelloWorld {

public String execute() throws Exception {

return "success";
}

/**
* Provide default valuie for Message property.
*/
public static final String MESSAGE = "HelloWorld.message";

/**
* Field for Message property.
*/
private String message;

/**
* Return Message property.
*
* @return Message property
*/
public String getMessage() {
return message;
}

/**
* Set Message property.
*
* @param message Text to display on HelloWorld page.
*/
public void setMessage(String message) {
this.message = message;
}


 

2.3.2  jsp--请求

编写两个jsp文件,存放在WebContent根目录下:

 其中index.jsp会被配置成默认的主页显示;HelloWorld.jsp配置为响应处理后对客户端的显示。

 index.jsp


在后面的配置文件中会使用form中的action配置,此处action配置为HelloWorld。配置文件中会将此action和前述中的java类中的接口函数建立关联,即前台发生此action时,struct的dispatch进程检测到后,将此action交由与其关联的处理函数进行处理。


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Hello World</title>
</head>
<body>
<h1>Hello World From Struts2</h1>
<span style="color:#FF0000;"><form action="HelloWorld"></span>
<label for="message">Please enter your name</label><br/>
<input type="text" name="message"/>
<input type="submit" value="Say Hello"/>
</form>
</body>
</html>

2.3.3  jsp--回显



HelloWorld.jsp


下面代码中的message和上面代码中的input名字的message是对应的。此脚本在java执行execute并返回success时调用


<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>HelloWorld</title>
</head>

<body>
<h2><s:property value="message"/></h2>

</body>
</html>


2.4 struct配置文件

struct的配置文件在webConent/WEB-INF/classes目录下面

structs.xml

xml文件中的此句建立了index.jsp文件和java的对应关系

<action name="HelloWorld" class="com.xhj.ssh.action.HelloWorld"
method="execute">
此句建立了java和HelloWorl.jsp的对应关系,即:execute函数的返回值为success,则执行HelloWorld.jsp此脚本
<result name="success">/HelloWorld.jsp</result>


<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="hello world" extends="struts-default">
<action name="HelloWorld" class="com.xhj.ssh.action.HelloWorld"
method="execute">
<param name="message">param</param>
<result name="success">/HelloWorld.jsp</result>

</action>
</package>

<constant name="truts.devMode" value="true"></constant></struts>


2.5 web配置文件



web.xml文件是用来初始化配置信息:比如Welcome页面、servlet、servlet-mapping、filter、listener、启动加载级别等。其在WEB-INF目录下

此处主要配置了默认的主页面为index.jsp,详见下面的welcome-file 配置项

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

<display-name>Struts Blank</display-name>

<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>

<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

</web-app>


3. 运行与调试

3.1  基本运行

在tomcat的webapps目录中建立test目录。

右击项目名称sshtest,选择export->war,将生成的war文件部署到webapps/test 目录下。而后用7zip解压。解压后的效果为:


然后运行:http://localhost:8080/test/

3.2  tomcat日志

首先要保证tomcat可以正常运行上述代码,比如开始时,在tomcat中出现下面蓝色信息,则tomcat并没有讲HelloWorld运行成功,此时在前台进行访问必然是失败的。

严重: Exception starting filter struts2
java.lang.NoClassDefFoundError: org/apache/commons/lang3/StringUtils
        at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.
ister(XmlConfigurationProvider.java:213)


根据上面蓝色的提示信息,将所需要的包commons-lang3-3.2.jar 拷贝到前述的lib目录下,则问题解决。tomcat可正确部署此web application了!

信息: Deploying web application directory test
六月 14, 2015 2:25:19 下午 org.apache.coyote.http11.Http11AprProtocol start
信息: Starting Coyote HTTP/1.1 on http-8080
六月 14, 2015 2:25:19 下午 org.apache.coyote.ajp.AjpAprProtocol start
信息: Starting Coyote AJP/1.3 on ajp-8009
六月 14, 2015 2:25:19 下午 org.apache.catalina.startup.Catalina start
信息: Server startup in 10257 ms

3.3  前台提示信息

前台出现下面提示:
type Status report
message There is no Action mapped for namespace [/] and action name [hello] associated with context path [/test].
description The requested resource is not available.


上面提示没有action被映射,即前台发起的action,没人处理;同时我们注意地址栏里面有:

​http://localhost:8080/test/hello?message=hh​

其中hello为action,对照structs.xml观察,对于此action,的确没有对应的函数接口。修改jsp中form的action名称或者structs.xml都可以。

3.4  权限问题

HTTP Status 403 - Access to the requested resource has been denied

type Status report
message Access to the requested resource has been denied
description Access to the specified resource has been forbidden.

web.xml原先有如下代码,删除后则权限OK。下面代码具体含义以后再做了解

<!-- Restricts access to pure JSP files - access available only via Struts action -->
<security-constraint>
<display-name>No direct JSP access</display-name>
<web-resource-collection>
<web-resource-name>No-JSP</web-resource-name>
<url-pattern>*.jsp</url-pattern>
</web-resource-collection>
<auth-constraint>
<role-name>no-users</role-name>
</auth-constraint>
</security-constraint>

<security-role>
<description>Don't assign users to this role</description>
<role-name>no-users</role-name>
</security-role>



4. 总结

     首先要设计需要哪些action,据此可以设计前台的action和后台的类及类的实现;对于不同的action,需要考虑它在运行时被调用的频次、并发等。

        StrutsPrepareAndExecuteFilter负责拦截由<url-pattern>/*</url-pattern>指定的所有用户请求。

       下图为网图,其中用户请求(index.jsp),action(helloworld类) ,jsp/html(helloworld.jsp)