- jar包
https://pan.baidu.com/s/1dOaGt3ZIodfxBc0K_6iWCw
struts2共有107个jar包,基本用13个就可以了。
- 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">
<!-- 配置strust2核心过滤器 -->
<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.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
- struts2.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>
<!--
写常量,可修改默认struts2中默认配置的诸多选项,如果在web.xml中修改常量会覆盖struts.xml
struts.action.extension设置映射后缀的名称,此处改为do,,逗号中间为空表示不写也可以
<constant name="struts.action.extension" value="do,,"></constant>
-->
<!-- 包结构 -->
<package name="default" namespace="/" extends="struts-default">
<!--
配置action
如果不写method属性,struts2则调用默认的方法execute
class也可不写,没尝试过
-->
<action name="hello" class="action.HelloAction" method="hello">
<!--
根据返回的string来跳转到相应的页面 ,struts2不用写工程名,默认为转发,如需重定向
需要在result标签内添加type属性值为redirect即可
result也可以设置多个,需要配置。。。。。。
-->
<result name="retMsg">/retMsg.jsp</result>
</action>
</package>
<!--
通过这种方式来分包处理配置文件,不过现实中可能用的不多
<include file="testPackage/sturts_test.xml"></include>
-->
</struts>
- 前端页面发送请求
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h3>Action</h3>
<a href="${pageContext.request.contextPath}/hello.action">helloAction</a>
</body>
</html>
- action类的简单编写
package action;
import javax.swing.plaf.synth.SynthSeparatorUI;
/**
* 测试action用类
* action类必要条件:public修饰符 返回值String
* 需要在strust.xml配置文件中配置,才能使用此类中的方法。
* @author Administrator
*
*/
public class HelloAction {
public String hello() {
//业务代码
System.out.println("成功运行HelloAction");
//返回字符串在strust.xml配置文件中对应的action找到对应的result转发或重定向
return "retMsg";
}
}