<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>首页</title>
</head>
<body>
<a href="https://www.linuxidc.com/<%=path%>/user/userAction!toAddUser.action">跳转</a>
<br/>
<a href="https://www.linuxidc.com/<%=path%>/user/userAction!addUser.action">添加用户</a>
</body>
</html>
跳转是跳到到success.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'success.jsp' starting page</title>
</head>
<body>
This is my JSP page. <br>
成功了
</body>
</html>
添加用户是跳到adduser.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'addUser.jsp' starting page</title>
</head>
<body>
This is my JSP page. <br>
添加用户了
</body>
</html>
写好了jsp之后我们就要过滤这些连接,进行跳转,写一个struts2的过滤器:
public class StrutsFilter implements Filter {
@Override
public void destroy() {
}
@Override
public void doFilter(ServletRequest req, ServletResponse res,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest request=(HttpServletRequest)req;
String path=request.getServletPath();
System.out.println(path);
String[] pathArr=path.split("/");
String namespace=pathArr[1];
String actionString=pathArr[2];
String actionname=actionString.split("!")[0];
String methodname=actionString.split("!")[1].split("\\.")[0];
System.out.println(actionname+":"+methodname);
PackageEntity pe=ConfigUtils.pe;
List<ActionEntity> actions=pe.getActions();
ActionEntity doAction=null;
for(ActionEntity ae:actions){
if(ae.getName().equals(actionname)){
doAction=ae;
break;
}
}
try {
Class actioncls=Class.forName(doAction.getClassname());
Object actionObj=actioncls.newInstance();
Class cls=actionObj.getClass();
Method actionMethod=cls.getDeclaredMethod(methodname);
String resultValue=(String)actionMethod.invoke(actionObj,null);
List<ResultEntity> results=doAction.getResults();
ResultEntity re=null;
for(ResultEntity result:results){
if(resultValue.equals(result.getName())){
re=result;
break;
}
}
request.getRequestDispatcher(re.getPage()).forward(request, res);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void init(FilterConfig filterConfig) throws ServletException {
ConfigUtils.config();
}
}