一、搭建环境
1.导包:
2.web.xml配置StrutsPrepareAndExecuteFilter
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.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>
</web-app>
3.src下创建核心配置文件struts.xml
二、Hello项目
1.创建HelloAction类,请求处理类(Action)
2.在Action类中添加请求处理方法excite()
/**
* 请求处理类
*/
public class HelloAction {
// 请求处理方法
// 请求处理方法必须是这个格式
// 相当于Servlet中的service()方法
public String execute() {
System.out.println("Hello Struts2!");
// return null;//不跳转
return "success";
}
}
3.struts.xml中配置action
<?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>
<package name="day01" namespace="/" extends="struts-default">
<action name="helloAction" class="cn.wang.demo1.action.HelloAction">
<result name="success">/demo1/result.jsp</result>
</action>
</package>
</struts>
4.跳转页面
(1)start.jsp
<a href="<c:url value='/helloAction.action'/>">访问Action</a>
(2)result.jsp
hello
(3)在请求方法中返回”success”
public String execute() {
System.out.println("Hello Struts2!");
// return null;//不跳转
return "success";
}
(4)在action元素中添加结果配置
<result name="success">/demo1/result.jsp</result>