1、开发工具:eclipse3.4.0 tomcat 6.0
2、开发步骤:
(1)新建工程MyFirstApp;
(2)引入Struts2所需的jar包,拷贝到lib目录下:
一、struts2-core-2.3.16.1.jar --------------- struts2的核心包
二、xwork-core-2.3.16.1.jar -------------- xwork的包 由于Struts2是由xwork的延伸 有些类依然关联着 xwork的类
三、freemarker-2.3.19.jar--------------- FreeMarker是一个模板引擎,一个基于模板生成文本输出的通用工具
四、ognl-3.0.6.jar -------------- 支持ognl表达式
五、javassist-3.9.0.GA
六、commons-fileupload-1.3.1.jar ------------ struts的上传下载
七、commons-io-1.4
八、commons-lang3-3.1.jar----------Commons Lang这一组API提供一些基础的、通用的操作和处理,如自动生成toString()的结果、自动实现hashCode()和equals()方法、数组操作、枚举、日期和时间的处理等等。
ArrayUtils – 用于对数组的操作,如添加、查找、删除、子数组、倒序、元素类型转换等;
StringUtils – 处理String的核心类,提供了相当多的功能;
commons-lang-2.5.jar包的介绍:转载“http://blog.csdn.net/terryzero/article/details/4317320”
(3)配置web.xml文件;
(4)在WebContent或者WebRoot目录下新建欢迎 页index.jsp
(5)在WebContent或者WebRoot目录下新建jsp目录,用于存储jsp文件;
(6)在jsp目录下新建success.jsp,在src目录下新建包com.test.action,并新建MyTestAction.java
(7)在src目录(根目录)下新建strtus.xml,并配置struts.xml文件;
3、代码:
<?xml version="1.0" encoding="UTF-8"?>
<web-app 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">
<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>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
</head>
<body>
<form action="/MyFirstApp/login.do" method="post">
<input type="text" name="userName"/>不能为空<br>
<input type="text" name="passWord"/>不能为空<br>
<input type="submit" value="提交"/>
</form>
</body>
</html>
(3)success.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!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>Insert title here</title>
</head>
<body>
<h6>Hello world!!!</h6>
${msg}${userName}${passWord}
</body>
</html>
(4)
MyTestAction.java
package com.test.action;
import com.opensymphony.xwork2.ActionSupport;
public class MyTestAction extends ActionSupport {
private String userName;
private String passWord;
private String msg;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassWord() {
return passWord;
}
public void setPassWord(String passWord) {
this.passWord = passWord;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public String execute() throws Exception {
msg = "登陸成功";
return "success";
}
}
(5)struts.xml
<?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.action.extension" value="do,action"/><!-- 如果不配置这个常量,form提交就无法找到对应的action -->
<package name="default" namespace="/" extends="struts-default">
<!-- 第一个Action案例 -->
<action name="login" class="com.test.action.MyTestAction" method="execute">
<result name="success">/WEB-INF/jsp/success.jsp</result>
</action>
</package>
</struts>
4、遇到的问题:
(1)struts.xml文件位置放置错误,正确的路径应当是:src根目录下
(2)报ClassNotFound错误, org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter。问题原因是jar引用的版本错误,并不是任意版本的jar都可以互相兼容。
(3)报“There is no Action mapped for namespace [/] and action name [Login] associated with context path [/eprint]”原因是index.jsp未放置到webroot目录下,放置到了web-inf目录下。
(4)服务启动后,进入欢迎 页面,无法提交到action,问题原因是未在strtuts.xml中配置常量
<constant name="struts.action.extension" value="do,action"/>