struts2工作原理

struts2工作原理图:

struts2工作原理_第1张图片

执行步骤:

struts2工作原理_第2张图片

Action.java(Action接口)

package cn.itcast.action;
public interface Action {
public String execute();
}
Struts2Filter.java(filter过滤器,实现Filter接口):相当于strtus.xml的配置功能(这里演示struts2的工作原理)

package cn.itcast.action;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Struts2Filter implements Filter {

Map<String, String> map = new HashMap<String, String>();

public void init(FilterConfig config) throws ServletException {
map.put("/primer/userAction.action", "cn.itcast.action.UserAction");
map.put("/helloWorld/helloWorldAction.action", "cn.itcast.action.HelloWorldAction");

}

public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {

//强转
HttpServletRequest req = (HttpServletRequest)request;
HttpServletResponse res = (HttpServletResponse)response;

String path = req.getServletPath();

System.out.println("path = "+path);

if(path.equals("/test.jsp")){
chain.doFilter(req, res);
}else{
try {
Action action;

//利用反射,通过newInstance()得到实例化
action = (Action)Class.forName(map.get(path)).newInstance();

action.execute();

req.getRequestDispatcher("/success.jsp").forward(req, res);

} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

}

public void destroy() {
// TODO Auto-generated method stub

}

}

UserAction,java(实现Action接口)

package cn.itcast.action;
public class UserAction implements Action {
public String execute() {
System.out.println("UserAction ********** execute()");
return "success";
}
}

HelloWorldAction(实现Action接口)

package cn.itcast.action;
public class HelloWorldAction implements Action {
public String execute() {
System.out.println("HelloWorldAction ********** execute()");
return "success";
}

}

实现Action接口示例图:

struts2工作原理_第3张图片

test.jsp

<%@ page language="java" pageEncoding="utf-8" contentType="text/html; charset=utf-8"%>
<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
</head>
<body>
入门的路径:<br>

<!--${pageContext.request.contextPath}绝对路径表示:网站根路径 -->
<!-- action: 命名空间/action名.action -->
<a href="${pageContext.request.contextPath}/primer/userAction.action">userWorld</a><br>

<br>
<a href="${pageContext.request.contextPath}/helloWorld/helloWorldAction.action">helloWorld</a><br>

</body>
</html>

success.jsp

<%@ page language="java" pageEncoding="utf-8" contentType="text/html; charset=utf-8"%>
<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
</head>
<body>
Welcome!
</body>
</html>


 

 

 

版权声明:本文为博主原创文章,未经博主允许不得转载。

你可能感兴趣的:(Struts工作原理)