步骤:
1、拷贝struts的jar到项目中(apps中的blank项目中可以找到这些jar包)
2、将struts2的过滤器添加到web.xml中
3、配置struts2的配置文件(在src目录中创建struts.xml文件)
4、创建action(action就是一个POJO类)
4.1、为action编写execute方法
4.2、在struts.xml文件中配置action和返回结果集
struts.xml
<?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="default" namespace="/" extends="struts-default">
<action name="hello" class="itat.org.zttc.action.HelloAction">
<result name="success">/hello.jsp</result>
</action>
</package>
</struts>
web.xml中配置所有请求的拦截器
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<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>
处理请求的action
package itat.org.zttc.action;
public class HelloAction {
public String execute() {
System.out.println("hello struts");
return "success";
}
}
最终视图页面
<%@ 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>Insert title here</title>
</head>
<body>
<h1>Hello Struts2</h1>
</body>
</html>
结果:
Hello Struts2