目录结构
C:WorkSpacejavaStrutsTestsrcmain
C:WorkSpacejavaStrutsTestsrc est
C:WorkSpacejavaStrutsTestsrcmainjava
C:WorkSpacejavaStrutsTestsrcmain
esources
C:WorkSpacejavaStrutsTestsrcmainwebapp
C:WorkSpacejavaStrutsTestsrcmainjavacom
C:WorkSpacejavaStrutsTestsrcmainjavacomchzhao
C:WorkSpacejavaStrutsTestsrcmainjavacomchzhaostrutstest
C:WorkSpacejavaStrutsTestsrcmainjavacomchzhaostrutstestHelloAction.java
C:WorkSpacejavaStrutsTestsrcmain
esourcesstruts.xml
C:WorkSpacejavaStrutsTestsrcmainwebapphello.jsp
C:WorkSpacejavaStrutsTestsrcmainwebappWEB-INF
C:WorkSpacejavaStrutsTestsrcmainwebappWEB-INFweb.xml
C:WorkSpacejavaStrutsTestsrc estjava
C:WorkSpacejavaStrutsTestsrc est
esources
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.zhao</groupId>
<artifactId>StrutsTest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>StrutsTest</name>
<description>StrutsTest</description>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.3.16.1</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.14</version>
</dependency>
</dependencies>
</project>
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="test" namespace="/" extends="struts-default">
<action name="hello" class="com.chzhao.strutstest.HelloAction" method="hello">
<result name="success">/hello.jsp</result>
</action>
</package>
</struts>
HelloAction.java
package com.chzhao.strutstest;
import com.opensymphony.xwork2.ActionSupport;
public class HelloAction extends ActionSupport {
private static final long serialVersionUID = 1L;
private String str;
public String hello() {
this.str = "zhaokeke!!!";
return "success";
}
public String getStr() {
return str;
}
public void setStr(String str) {
this.str = str;
}
}
hello.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!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=UTF-8">
<title>Test</title>
</head>
<body>
<h1>
<s:property value="str" />
</h1>
</body>
</html>
运行程序
修改Run Configurations,把context改为/,然后run as jetty
打开浏览器,输入http://localhost:8080/hello.action即可。