一、导入struts2的包
二、在web.xml中配置核心过滤器
<filter>
<filter-name>centerFilter</filter-name>
<!--
注意类的路径:
自2.1.3版本开始,ActionContextCleanUp和FilterDispatcher都由StrutsPrepareAndExecuteFilter代替。
-->
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>centerFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
三、在src下创建struts.xml
<?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>
<!--
name --随意
namespace --外部访问时需要加上命名空间
extends -- 继承的默认类
-->
<package name="default" namespace="/test" extends="struts-default">
<!--
name -随意 这个配置外部访问的路径:工程名/test/login
class -- action类的类全名
如果不写method属性那么execute是默认执行的方法
<action name="login" class = "com.porject.actions.LoginAction" method="otherMethod">
-->
<action name="login" class = "com.porject.actions.LoginAction">
<!--
name --action类返回时用到 return "success";
中间是需要跳转的JSP
-->
<result name="success">/result.jsp</result>
</action>
</package>
</struts>
四、hello world
测试类
package com.porject.actions;
public class LoginAction {
//execute是默认执行的方法
public String execute() {
System.out.println("欢迎访问");
return "success";
}
}
jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>欢迎访问</h1>
</body>
</html>