最简单的struts2程序
项目名First
src下:
包名:org.xmh.demo----类:HelloWord.java
与包名同一级别:struts.xml
Web App Libraries
commons-beanutils-1.7.0.jar
commons-chain-1.2.jar
commons-collections-3.2.jar
commons-digester-2.0.jar
commons-fileupload-1.2.1.jar
commons-io-1.3.2.jar
commons-lang-2.3.jar
commons-logging-1.0.4.jar
commons-logging-api-1.1.jar
commons-validator-1.3.1.jar
freemarker-2.3.15.jar
ognl-2.7.3.jar
struts2-core-2.1.8.1.jar
xwork-core-2.1.6.jar
WebContent
WEB-INF:web.xml(自带)
与WEB-INF同级:
HelloWord.jsp
------------------------
HelloWord.java
package org.xmh.demo;
import java.util.Date;
import java.text.DateFormat;
import com.opensymphony.xwork2.ActionSupport;
public class HelloWord extends ActionSupport {
private String message;
public String getMessage(){
return message;
}
public String execute(){
message = "Hello world, Now is" +
DateFormat.getInstance().format(new Date());;
return SUCCESS;
}
}
----------------------------
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="First" extends="struts-default" namespace="/">
<action name="HelloWord" encoding="UTF-8"?>
<web-app id="WebApp_ID" 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>First</display-name>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>first.jsp</welcome-file>
</welcome-file-list>
</web-app>
---------------------------------
HelloWord.jsp
<%@ page contentType=" text/html; charset=UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>HelloWord</title>
</head>
<body>
<h2><s:property value="message" /></h2>
</body>
</html>
----------------------------------
在浏览器中输入http://localhost:8080/First/HelloWord.action.
出现404错误的原因可能是 程序没有放在/tomcat/webapp目录下。