Struts2入门

1. Struts2框架介绍

  • Struts 2是Struts的下一代产品,是在WebWork的技术基础上开发了全新MVC框架。虽然Struts2号称是一个全新的框架,但这仅仅是相对Struts1而言。Struts2与Struts1相比,确实有很多革命性的改进。

  • Struts2框架适合分层开发。框架应用实现不依赖于Servlet,使用大量的拦截器来处理用户请求,属于无侵入式的设计

2. Struts2框架的工作原理

  image

  1)请求先到达Filter中央控制器

  2)然后为Action创建代理类

  3)将各个服务存放在拦截器中,执行完拦截器后再去执行action类行action类,action类调用service,再调用dao

  4)得到结果字符串,创建result对象

  5)转向相应的视图。

image

3. Struts2框架的使用

  1)创建web应用项目

  2)下载Struts2 jar包(下载地址:http://struts.apache.org)

  3)导入jar包

  image

  4)在web.xml文件中配置核心控制器

  <?xml version="1.0" encoding="UTF-8"?>
  <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
           version="3.1">
      <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>

  5)拷贝struts.xml文件
  将拷贝的配置文件放在根目录src下。struts.xml文件主要是配置请求路径对应action类的,以及结果跳转路径。

  <?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>

      <constant name="struts.devMode" value="true" />

      <package name="basicstruts2" extends="struts-default">
          <action name="index">
              <result>/index.jsp</result>
          </action>
      </package>
  </struts>

转载于:https://www.cnblogs.com/sgx0214/p/7488112.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值