zoukankan      html  css  js  c++  java
  • Struts2学习笔记

    Struts2简介

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

    Struts2的优点:

    1. 结构清晰,开发流程一目了然,使开发者只关注业务逻辑实现即可。
    2. 提供丰富的标签,大大提高开发效率。
    3. 提供页面导航,通过配置文件把握整个系统各部分之间的联系,便于后期维护和扩展(扩展性高)。
    4. 不在依赖Servlet API,便于测试。
    5.  Struts2提供丰富的拦截器实现。
    6. 异常处理机制,只需在配置文件中配置异常的映射,即可对异常做相应的处理,减少try-catch的工作量。

     

    Struts2开发环境-配置

    1. 可以在下列网址下载Struts2:http://struts.apache.org/download.cgi(谷歌浏览器翻译后样子)
    2. 在项目中导入必需的jar包
    • commons-fileupload-1.2.2.jar :Struts文件的上传和下载。
    • commons-io-2.0.1.jar :文件读取。
    • commons-lang3-3.1.jar :为java.lang包提供扩展。
    • freemarker-2.3.19.jar :FreeMarker是一个模板引擎,是一个基于模板生成文本输出的通用工具。
    • ognl-3.0.5.jar :支持OGNL表达式。
    • Struts 2-core-2.3.4.1.jar :Struts2核心包。
    • xwork-core-2.3.4.1.jar :xwork核心包。
    • javassist-3.11.0.GA.jar :分析,编辑和创建Java字节码的类库。

          3.修改Web.xml配置文件

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     3     xmlns="http://java.sun.com/xml/ns/javaee"
     4     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
     5     id="WebApp_ID" version="3.0">
     6     <display-name>Struts2_hellostruts2</display-name>
     7         <welcome-file-list>
     8             <welcome-file>index.jsp</welcome-file>
     9         </welcome-file-list>
    10 
    11     <filter>
    12         <filter-name>struts2</filter-name>
    13         <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    14     </filter>
    15     
    16     <filter-mapping>
    17         <filter-name>struts2</filter-name>
    18         <url-pattern>/*</url-pattern>
    19     </filter-mapping>
    20 </web-app>
    21         

      4.添加struts.xml配置文件

     1 <?xml version="1.0" encoding="UTF-8" ?>
     2 <!DOCTYPE struts PUBLIC
     3     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
     4     "http://struts.apache.org/dtds/struts-2.3.dtd">
     5 <struts>
     6     <package name="default"  namespace="/"  extends="struts-default">
     7                <action name="hello" class="hellostruts2.helloAction"  method="execute">
     8                        <result  name="success">/helloStruts2.jsp</result>      
     9            </action> 
    10     </package>
    11 </struts>
    12

      5.添加并实现Action类

     1 package hellostruts2
     2 
     3 import com.opensymphony.xwork2.ActionSupport;
     4 
     5 public class helloAction extends ActionSupport{
     6 private String emg;
     7 
     8 public String getEmg() {
     9 return emg;
    10 }
    11 
    12 public void setEmg(String emg) {
    13 this.emg = emg;
    14 }
    15 @Override
    16 public String execute()throws Exception{
    17 emg="hello Struts2";
    18 return "success";
    19 
    20 }
    21 }

    配置好struts.xml文件后,Struts2即可完成整个请求过程,当用户发送一个请求后,Web服务器将这个请求的URL(地址)对应到一个Action类(helloAction),并调用相应的方法[execute()]进行处理,该方法的(success)返回值,Struts2根据这个返回值对应到相应的<result>,并使用<result>中的配置(/helloStruts2.jsp)来响应客户请求。

  • 相关阅读:
    查看系统的所有port的状态
    python技巧26[python的egg包的安装和制作]
    python类库31[进程subprocess与管道pipe]
    [BuildRelease Management]hudson插件
    python类库31[使用xml.etree.ElementTree读写xml]
    开机自动运行VMWare
    python实例26[计算MD5]
    2021年最新大厂php+go面试题集(四)
    Jumpserver开源跳板机系统
    报错:ImportError: libmysqlclient.so.20: cannot open shared object file: No such file or director(亲测可用)
  • 原文地址:https://www.cnblogs.com/xiaohao-Marldoro1997/p/6686238.html
Copyright © 2011-2022 走看看