1.Struts的几个基本概念
1.struts是一个开源框架(frameset)
2.struts是一个Web框架
3.struts是一个基于MVC的Web框架
2.所需jar包
下载地址:http://archive.apache.org/dist/struts/binaries/
3.配置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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>struts2Demo</display-name>
<!-- Struts2的配置 -->
<filter>
<!-- 配置过滤器的名字 -->
<filter-name>struts2</filter-name>
<!-- 配置核心过滤器类 -->
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<!--配置要拦截的URL,辞去配置全部拦截 -->
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter 如果路径找不到,可通过eclipse快捷键ctrl+shift+t 输入StrutsPrepareAndExecuteFilter找到全名
4.编写action类
public class HolleAction {
public String execute() {
System.out.println("hffkfkfkk");
return "success";
}
}
5.新建xml文件
5.1, 在java Resources目录下新建conf包,用于存放xml文件
![在这里插入图片描述](https://img-blog.csdn.net/20181011162034734?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQwOTczNDc1/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70
5.2,创建struts-sy.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>
<package name="sy" extends="base" namespace="/sy">
<action name="helloAction" class="com.zking.one.web.HolleAction">
<result name="success">/success.jsp</result>
</action>
</package>
</struts>
5.3,创建成功的jsp页面(在项目的根目录下:WebContent)
<%@page import="com.opensymphony.xwork2.ActionContext"%>
<%@ 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>
跳转成功
<%
ActionContext ac = ActionContext.getContext();
System.out.println(ac);
%>
</body>
</html>
5.4,从dome1页面跳转
<%@ 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 Word</h1><!-- /sy/helloAction.action 因为在struts-sy.xml里helloAction是在namespace为/sy下的name -->
<!-- 使用Struts框架,没有配置跳转方法,那么默认调用execute方法 -->
<a href="${pageContext.request.contextPath}/sy/helloAction.action">holleword</a>
</body>
</html>
测试结果: