HelloWorldAction.java
public class HelloWorldAction {
public String execute() throws Exception{
return "SUCCESS";
}
}
web.xml(2.5的新写法)
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<package name="HELLO" namespace="/" extends="struts-default">
<action name="helloworld"
class="com.caizhen.action.HelloWorldAction">
<result name="SUCCESS">/success.jsp</result>
</action>
</package>
</struts>
index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<a href="${pageContext.request.contextPath }/helloworld.action">login</a>
</body>
</html>
success.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
66666
</body>
</html>
在客户端浏览器上单击index.jsp上的超链接时,会发送一个helloword.action的请求,该请求被核心拦截器拦截后,会通过struts.xml文件中的配置找到相应的HelloWorldAction,并默认调用其中的execute()方法返回逻辑视图名,然后同根据配置文件找到并转发给对应的视图页面success.jsp中,最后生成响应内容并输出响应的返回结果
其流程为
1.发送请求
2.调用Action类(web.xml)
3.执行execute()方法,返回逻辑视图(struts.xml)
4.转发到视图界面
5.生成响应界面