• Struts2框架01【如果使用struts框架】【利用struts框架写一个 hello world】


    1 什么是Struts2框架

      基于MVC设计模式的web应用框架

      Struts2框架是一个轻量级的MVC流程框架

        轻量级是指程序的代码不是很多,运行时占用的资源不是很多,MVC流程框架就是说它是支持分层开发,控制数据的流程,从哪里来,到那里去,怎么来,怎么去的这样一个框架;  

      Struts1 和 Struts2 没有任何关系
      Struts2的前身是WebWork
      Struts1/Struts2/SpringMVC都是MVC设计模式的表现层框架【SpringMVC现在最流行】

    2 如何使用Struts2框架

      2.1 导包 Struts2-core

      2.2 配置主控制器,在web.xml中进行配置(注意:这里的主控制器相当于SpringMVC的前端控制器)

      2.3 配置struts.xml配置文件(注意:这里的配置文件名不能进行更改)

    3 案例:使用Struts2表现框架实现一个helloworld案例

      3.1 案例效果

        在浏览器输入:http://localhost:8080/ssh01/demo/hello

        服务器返回的结果是:

          

      3.2 配置主控制器    

        

          注意:主控制器类在maven中的位置

            

      3.3 配置struts.xml配置文件

        3.3.1 手动创建一个struts.xml文件

          》创建一个xml文件,文件名为struts

          》打开默认的struts.xml文件

            

          》将默认struts.xml文件中24——26中内容复制到自己创建的struts.xml文件中

            

          》在自己创建的struts.xml文件中添加一个<struts></struts>标签

         3.3.2 配置struts.xml文件

          》配置请求路径

            通过 package 标签实现

              name属性:随便写个名字就行,无什么实际意义

              namespace属性:请求路径

              extends属性:继承默认的struts.xml配置文件

          》配置具体请求名

            通过 action 标签实现

              name属性:具体的请求名(注意:会默认在后面添加 .action ,所以访问时使用 hello 和 hello.action 都可以)

              class属性:请求控制类

          》配置返回的结果

            通过 result 标签实现

              name属性:该属性值必须和控制类中execute方法的返回值保持一致

           

      3.4 编写控制类

        该类中必须有一个execute方法,该方法是用来处理请求的

        

     1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
     2   <modelVersion>4.0.0</modelVersion>
     3   <groupId>cn.xiangxu</groupId>
     4   <artifactId>ssh01</artifactId>
     5   <version>0.0.1-SNAPSHOT</version>
     6   <packaging>war</packaging>
     7   <dependencies>
     8       <dependency>
     9           <groupId>org.apache.struts</groupId>
    10           <artifactId>struts2-core</artifactId>
    11           <version>2.3.8</version>
    12       </dependency>
    13   </dependencies>
    14 </project>
    pom.xml (maven依赖)
     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <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">
     3   <display-name>ssh01</display-name>
     4   <welcome-file-list>
     5     <welcome-file>index.html</welcome-file>
     6     <welcome-file>index.htm</welcome-file>
     7     <welcome-file>index.jsp</welcome-file>
     8     <welcome-file>default.html</welcome-file>
     9     <welcome-file>default.htm</welcome-file>
    10     <welcome-file>default.jsp</welcome-file>
    11   </welcome-file-list>
    12   
    13   <!-- 配置主控制器
    14       配置方法和selvet的配置相似
    15       相当于SpringMVC框架的前端控制器 -->
    16   <filter>
    17       <filter-name>StrutsMVC</filter-name>
    18       <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    19   </filter>
    20   <filter-mapping>
    21       <filter-name>StrutsMVC</filter-name>
    22       <url-pattern>/*</url-pattern>
    23   </filter-mapping>
    24   
    25 </web-app>
    web.xml (主控制器配置)
     1 <?xml version="1.0" encoding="UTF-8"?>
     2 
     3 <!DOCTYPE struts PUBLIC
     4     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
     5     "http://struts.apache.org/dtds/struts-2.3.dtd">
     6 
     7 <struts>
     8     <package name="test" namespace="/demo" extends="struts-default">
     9         <action name="hello" class="cn.xiangxu.action.HelloAction">
    10             <result name="success">
    11             /WEB-INF/msg.jsp
    12             </result>
    13         </action>
    14     </package>
    15     
    16 </struts>
    struts.xml (struts框架配置文件)
    1 package cn.xiangxu.action;
    2 
    3 public class HelloAction {
    4     public String execute() {
    5         System.out.println("hello struts2");
    6         
    7         return "success";
    8     }
    9 }
    控制类
     1 <%@ page language="java" contentType="text/html; charset=utf-8"
     2     pageEncoding="utf-8"%>
     3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     4 <html>
     5 <head>
     6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
     7 <title>Insert title here</title>
     8 </head>
     9 <body>
    10     <h2>Hello Struts2</h2>
    11 </body>
    12 </html>
    msg.jsp (结果页面)

  • 相关阅读:
    UVA 12546 LCM Pair Sum
    两两间的距离都是整数的点集
    Codeforces 11.27
    Codeforces 11.27 B
    UVA 105
    打印自身的程序
    Interval DP
    Tree DP
    参加第五次全国工程建设行业信息化建设高峰论坛 (个人的一点感想)
    基础资料分类及清单版本管理
  • 原文地址:https://www.cnblogs.com/NeverCtrl-C/p/7112170.html
Copyright © 2020-2023  润新知