Struts2框架搭建

Struts2框架搭建

1.创建一个web项目
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述
2.Struts2jar包导入Struts2官网

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述
3.配置Struts2
(1.)配置web.xml

在这里插入图片描述

<?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>struts2_demo</display-name>
  
  <!-- struts 前控制器的主要作用是提供一个统一入口,所有请求都先经过前控制器 -->
    <filter>
        <filter-name>action2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>action2</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>

(2.)配置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>
   
</struts>

(3.)编写后控制器Action类

在这里插入图片描述

package com.demo.struts2;
/** * 后端的控制器 * @author libaolei * */
public class HelloAction {
	public String helloDemo() {
		System.out.println("hello struts");
		return null;
	}
}

(4.)在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>

   <package name="demo" extends="struts-default" namespace="/hello">
   	<action name="helloAction" class="com.demo.struts2.HelloAction" method="helloDemo"></action>
   </package>
</struts>

4.测试
部署项目成功访问:
http://127.0.0.1:8080/struts2_demo/hello/helloAction.action
http://127.0.0.1:8080/项目名/nameSpace/action的名字

在这里插入图片描述

这只是简单的搭建,Struts2用起来相比Spring MVC麻烦多了,而且还出现过漏洞。。。