1. 在 web.xml 加入以下代码:
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<!-- 使用该核心Filter来接受所有的Web请求 -->
<url-pattern>/*</url-pattern>
</filter-mapping>
2. 在scr 目录下创建名为struts 的xml 文件:
struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<include file="/struts-basic.xml"></include>
<include file="/com/itcast/test/config/test-struts.xml"></include>
</struts>
3.对struts.xml进行扩展,struts-basic.xml存放一些基本配置信息(如:异常处理), *-struts.xml存放具体的处理信息:
struts-basic.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="basic" extends="struts-default">
<global-results>
<result name="exception">/jsp/common/exception.jsp</result>
</global-results>
<global-exception-mappings>
<exception-mapping result="exception" exception="java.lang.RuntimeException"></exception-mapping>
</global-exception-mappings>
</package>
</struts>
test-struts.xml
<struts>
<!-- Action必须放在指定的包名空间中-->
<package name="test" extends="basic" namespace="/test">
<action name="login" class="com.itcast.test.action.LoginAction" method="execute">
<result name="TEST_SUCCESS">/jsp/test/test2.jsp</result>
</action>
</package>
</struts>
4.创建具体的处理文件:
LoginAction.java
package com.itcast.test.action;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport {
private static final long serialVersionUID = 1L;
@Override
public String execute() throws Exception {
return "TEST_SUCCESS";
}
}
5. jsp 文件:
test1.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>test</title>
</head>
<body>
<a href="http://localhost:8088/alex_shop2/test/login.action">login</a>
</body>
</html>
test2.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>test2</title>
</head>
<body>
hello world
</body>
</html>
页面流为: test1.jsp -> click "login" -> test2.jsp(show "hello world")