Struts相关

使用Struts2流程:

1.导入Struts2类包

2.在Web源代码文件夹中,创建名为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="Mypackage" namespace="/" extends="struts-default">
    <!-- 定义Action -->
    <action name="first">
        <!-- 定义处理成功后的映射界面 -->
        <result>/first.html</result>
    </action>
</package>
</struts>

3.在web.xml文件中声明Struts2提供的过滤器,类名为"org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter"

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>Struts2First</display-name>
  <filter>                            <!-- 配置Struct2过滤器 -->
      <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>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>

4.初始界面index.jsp:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="first.action">跳转</a>     <!-- 点击链接后,请求交给名为first的Action处理 -->  
</body>
</html>

5.处理后的映射界面first.jsp:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
我的第一个struts2实例
</body>
</html>
原文地址:https://www.cnblogs.com/zjlyyq/p/6104855.html