一个简单的struts2项目
1.新建一个 Dynamic Web Project 项目
2.配置 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.devMode" value="true"></constant>
<!--pakage标签
作用:给访问的action进行分包管理,吧配置文件按照面向对象管理
属性:name:指定包的名称,必须写,必须唯一
extends:指定当前包的父包,子包自动具备父包所指定的配置,我们的包一般都需要继承struts-default
-->
<package name="p1" extends="struts-default">
<action name="hello" class="com.action.HelloAction" method="getHello">
<result name="sucess" >/sucess.jsp</result>
</action>
<action name="hello2" class="com.action.HelloAction2" method="execute" >
<result name="success" >/sucess.jsp</result>
</action>
</package>
<!-- action的三种形式-->
<!-- <package name="user" extends="struts-default">
<action name="addUser" class="com.action.UserAction" method="addUser">
<result name="success">/sucess.jsp</result>
</action>
<action name="updateUser" class="com.action.UserAction" method="updateUser">
<result name="success">/sucess.jsp</result>
</action>
<action name="deleteUser" class="com.action.UserAction" method="deleteUser">
<result name="success">/sucess.jsp</result>
</action>
<action name="findUser" class="com.action.UserAction" method="findUser">
<result name="success">/sucess.jsp</result>
</action>
</package> -->
<!-- 使用通配符-->
<!-- <package name="user" extends="struts-default">
<action name="*" class="com.action.UserAction" method="{1}">
<result name="success">/sucess.jsp</result>
</action>
</package> -->
<!-- 开启动态调用-->
<constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>
<!-- -->
<package name="user" extends="struts-default">
<action name="user" class="com.action.UserAction" >
<result name="success">/sucess.jsp</result>
</action>
</package>
</struts>
web.xml文件配置==================
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>textstrutsAction</display-name>
<!-- struts2 过滤器核心配置-->
<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>
</web-app>
jsp文件=================
index.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<title>这是一个struts2</title>
</head>
<body>
<%-- <a href="${pageContext.request.contextPath}/addUser">添加</a>
<hr/>
<a href="${pageContext.request.contextPath}/updateUser">修改</a>
<hr/>
<a href="${pageContext.request.contextPath}/deleteUser">删除</a>
<hr/>
<a href="${pageContext.request.contextPath}/findUser">查找</a> --%>
<a href="${pageContext.request.contextPath}/user!addUser">添加</a>
<hr/>
<a href="${pageContext.request.contextPath}/user!updateUser">修改</a>
<hr/>
<a href="${pageContext.request.contextPath}/user!deleteUser">删除</a>
<hr/>
<a href="${pageContext.request.contextPath}/user!findUser">查找</a>
</body>
</html>
sucess.jsp文件===
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>执行结果</title>
</head>
<body>
执行成功
</body>
</html>
3.action类
package com.action;
import com.opensymphony.xwork2.ActionSupport;
public class UserAction extends ActionSupport{
public String addUser() {
System.out.println("11111");
return "success";
}
public String updateUser() {
System.out.println("22222");
return "success";
}
public String deleteUser() {
System.out.println("3333333333");
return "success";
}
public String findUser() {
System.out.println("4444444");
return "success";
}