struts2框架简介+搭建新项目

一、概述

  • 应用在Javaee中web层的框架
  • 本质是过滤器
  • struts2是struts1和webwork基础上发展的全新框架
  • struts2解决的问题:crud(增删改查操作),和servlet的不同


    image.png
  • 常见框架:struts2、springMVC
  • 基本执行过程


    image.png

二、搭建新项目

1.用到的struts2版本
image.png
2.配置struts.xml

在src根目录下建立struts.xml文件


image.png

找到下载的strust2中blank这个包


image.png

打开找到strust.xml复制他的约束条件,按照打开文件的格式写自己的xml


image.png

建立的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>
    <constant name="false" value="struts.enable.DynamicMethodInvocation"/>
    <constant value="true" name="struts.devMode"/>
    <package name="default" extends="struts-default" namespace="/">
    ...这里写配置
        
    </package>
</struts>

3.导入jar包

复制struts2-blank中lib下的所有jar包导入项目中


image.png

image.png
4.在web.xml中配置过滤器

复制struts2-blank中web.xml下的过滤器配置


image.png

配置好的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.217</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>
  <!--配置过滤器-->
  <filter>
    <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>

</web-app>

三、简单的hello world

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.helloworld</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>
  
  <filter>
    <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>
  
</web-app>

2.src下配置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>
    <constant name="false" value="struts.enable.DynamicMethodInvocation"/>
    <constant value="true" name="struts.devMode"/>
    <package name="default" extends="struts-default" namespace="/">
        <!--name:访问名称 -->
    <action name="ok">
            <!--配置返回值到页面-->
            <result>/HelloWord.jsp</result>
        </action>
    </package>
</struts>

3.index.jsp界面

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>hello world</title>
  </head>
  
  <body>
    <a href="ok">点击跳转<a>
  </body>
</html>
4.HelloWorld.jsp界面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>helloworld</title>
  </head>

  <body>
    Hello World
  </body>
</html>

5.运行效果

image.png

image.png
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 概述 什么是Struts2的框架Struts2是Struts1的下一代产品,是在 struts1和WebWork的...
    inke阅读 2,380评论 0赞 50
  • 本文包括: 1、Struts 2 概述2、Struts 2 快速入门3、Struts 2 的执行流程4、配置 st...
    廖少少阅读 3,262评论 3赞 13
  • Java是一种可以撰写跨平台应用软件的面向对象的程序设计语言。Java 技术具有卓越的通用性、高效性、平台移植性和...
    Java小辰阅读 1,183评论 0赞 14
  • 幸福是内心的感受。我很认同这样的解读。可是原来在职场,惶惶不可终日的自己,又哪有心情静下来品读这个词呢?只要稍有人...
    蝶舞醉花荫阅读 197评论 0赞 0
  • 青春梦 1 阳光的色彩被重新涂改。一波未平 被一瓣花朵击中的心情任性得像一生的遗憾 徒劳的等待与追忆宛若挥之不去的...
    南吕先生阅读 729评论 1赞 51

友情链接更多精彩内容