SSH学习-struts2配置基本步骤

前面学习了基于SpringMVC+Spring+MyBatis(SSM)的云笔记项目知识,最后需要学习另外一个前端框架Struts2,以及另外一个数据库框架Hibernate。虽然Struts2以及用得比较少,没有SpringMVC那么流行了,但是很多早期项目是用这个建立的,维护可能需要用到Struts2的知识,因此简单的学习了下,本文记录如何配置struts2。

Struts2简单介绍

Struts2是基于MVC设计模式,用于表现层的一种应用框架,跟前面学的SpringMVC功能类似,主要功能为接受浏览器传递的参数,并按照一定格式返回浏览器数据结果。使用方法与其他框架类似,也是使用既定的套路:导包→配置文件→调用API。

另外Struts2的前身是webwork,后面被Apache收购改名Struts2,Apache在它前面还开发了一款产品,叫做Struts1,但是不够流行普及度不高。

Struts2配置步骤

step1 导包:struts2-core

step2 配置文件:web.xml,struts.xml

(1)配置web.xml

web.xml文件中配置的是filter,类似以前配置servlet,但是filter的实现类需要记住,全局名叫做'org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter',在Struts2中使用Filter来处理浏览器请求。

<?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_2_5.xsd" version="2.5">
  <display-name>SSH01</display-name>
  <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>
  
  <!-- struts2中使用filter来实现servlet的功能 -->
  <filter>
    <filter-name>mvc</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    <init-param>
      <!-- 告诉struts2,struts.xml文件的读取位置,
      如果配置了inti-param标签,需要在param-value标签添加struts-default.xml和struts-plugin.xml两个文件,默认情况是自动读取,不需要添加
      手动情况下需要添加这两个文件进行读取 --> 
      <param-name>config</param-name>
      <param-value>
      struts-default.xml,
      struts-plugin.xml,
      config/struts.xml
      </param-value>
    </init-param>
  </filter>
  
  <filter-mapping>
    <filter-name>mvc</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  
</web-app>

其中struts.xml的作用类似SpringMVC中的HandlerMapping,浏览器发送请求到服务器后,首先struts的filter会接受请求,并读取struts.xml文件,由于文件中包含了配置匹配的请求路径,具体请求和对结果处理等,因此类似HandlerMapping的作用。

(2)配置struts.xml

配置struts.xml,其名字必须为struts,跟以前SpringMVC不一样可以随便取名字。其中struts2.xml文件中最常见的三个标签是package、action和result。具体配置在文件中已经说明,本文以实现一个简单的‘hello struts2’功能进行了配置。

<?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">
    
<!-- dtd文件导入的作用:决定了xml文件可以使用什么标签 -->    
   
<!-- struts根标签,其他还有比较常用的package,action,result等子标签 -->    
<!-- package标签,name属性用处不是很大,namespace代表匹配请求路径,extends类似Java继承,继承struts-default.xml -->
<!-- action标签,name代表请求路径,class为处理请求的类,会返回一个String类型结果,给result标签使用 -->
<!-- result标签,name属性值就是action返回的String名字,作用是返回结果处理 -->
<struts>
  <package name="demo" namespace="/hello" extends="struts-default">
    <!-- 没写具体方法时,默认执行类里名字为execute的方法,如果在action里配置method="",就读取对应的自定义方法 -->
    <action name="helloStruts" class="com.boe.Filter.Hello"> 
       <!-- result属性值配置name属性的话,就是默认进行转发,如果是其他的,需要配置 -->
       <result name="success">
         /WEB-INF/helloStruts.jsp
       </result>
    </action>
  </package>
  
  
</struts>    

(3)写控制器

action方法里的class属性对应的属性值的类,即控制器,其内部包含一个名为execute默认方法,如果不进行配置就默认执行execute方法,如果配置了method属性就执行method配置的方法。

package com.boe.Filter;

/**
 * 处理类
 * @author yangchaolin
 */
public class Hello {
    
    //写默认execute方法
    public String execute() {
        System.out.println("success");
        return "success";
    }

}

(4)写JSP文件

<%@page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>第一个struts程序</title>
</head>
<body style="font-family:'微软雅黑';font-size:20px;">
     <h1>Hello Struts2</h1>
</body>
</html>

项目启动测试

网页正常显示'Hello Struts2'。

控制台输出:

结论

(1)Struts2也是用于表现层的一种框架,类似SpringMVC。

(2)配置struts需要配置web.xml,struts.xml,其中web.xml中配置Filter,类似Servlet的配置,而struts.xml中主要配置三个常用标签package、action和result。

参考博文:

https://blog.csdn.net/weixin_38107930/article/details/83416337

原文地址:https://www.cnblogs.com/youngchaolin/p/10803516.html