• Struts2实验01--HelloWorld


    1、通过IDEA创建一个maven项目

    项目的结构如下: 

    pom.xml : 描述项目对其他组件的依赖

    web.xml : 对Web项目进行配置, 在此项目中, 用来声明Struts2

    src目录下面的main目录下面的java目录用来存放Java文件

    src目录下面的main目录下面的resource目录用来存放配置文件

    struts.xml用来描述struts2的配置

    src目录下面的main目录下面的webapp目录用来存放前端显示的界面的资源

    HelloStruts2
        ├── HelloStruts2.iml
        ├── pom.xml
        ├── src
        │   └── main
        │       ├── java
        │       │   └── shiyanlou
        │       │       └── HelloWorldAction.java
        │       ├── resources
        │       │   └── struts.xml
        │       └── webapp
        │           ├── Error.jsp
        │           ├── HelloWorld.jsp
        │           ├── WEB-INF
        │           │   └── web.xml
        │           └── index.jsp

    1、pom.xml文件: 引入了struts2的依赖jar文件和jetty服务器容器

    <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/maven-v4_0_0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>shiyanlou</groupId>
      <artifactId>HelloStruts2</artifactId>
      <packaging>war</packaging>
      <version>1.0-SNAPSHOT</version>
      <name>HelloStruts2 Maven Webapp</name>
      <dependencies>
        <dependency>
          <groupId>org.apache.struts</groupId>
          <artifactId>struts2-core</artifactId>
          <version>2.5.18</version>
        </dependency>
      </dependencies>
      <build>
        <finalName>HelloStruts2</finalName>
        <plugins>
          <plugin>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-maven-plugin</artifactId>
            <version>9.4.12.v20180830</version>
            <configuration>
              <scanIntervalSeconds>10</scanIntervalSeconds>
              <webApp>
                <contextPath>/</contextPath>
              </webApp>
            </configuration>
          </plugin>
        </plugins>
      </build>
    </project>

    2、web.xml : 配置struts2拦截器, 所有的访问都要通过struts2的拦截器, 从而实现struts2的访问控制的功能, 同时设置欢迎页面为罗列出的以下的页面, 当访问的URL只是[IP地址][:][端口号][/]的时候会显示以下的界面. 越往上显示的级别越高

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
      <display-name>HelloStruts2</display-name>
      <filter>
        <filter-name>struts2</filter-name>
        <filter-class>
          org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter
        </filter-class>
      </filter>
    
      <filter-mapping>
        <filter-name>struts2</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>

    3、struts.xml : 在struts.xml文件中写页面的跳转的逻辑

    这个小项目的逻辑是, 访问首页是index.jsp, 在这个页面又一个表单, 在表单中输入自己的名字, 就回跳转到HelloWorld.jsp文件, 在这个文件中会显示你输入的名字. 如果你在表单中输入的名字为空, 或者直接通过URL访问成功输入名字的页面, 就回跳转到失败界面Error.jsp, 显示失败.

    <?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.enable.DynamicMethodInvocation" value="true" />
    
        <package name="shiyanlou" extends="struts-default">
            <!--    用来设置当URL无效的时候页面跳转的位置    -->
            <!--    此处无效页面跳转到index, 也就是/index 代表的页面    -->
            <default-action-ref name="index" />
            <!--    这里设置index为webapp/index.jsp页面    -->
            <action name="index" >
            <!--    提交表单以后会返回一个success, 从而跳转到指定的页面    -->
                <result name="success">/index.jsp</result>
            </action>
    
            <!--    配置action, form中写hello来调用该函数的execute方法    -->
            <action name="hello" class="shiyanlou.HelloWorldAction" method="execute">
                <!--     会自动的调用execute()函数, 从而设置name的值    -->
                <!--     发送给hello的值为success, 就跳转到webapp/HelloWorld.jsp页面       -->
                <result name="success">/HelloWorld.jsp</result>
                <!--     发送给hello的值为error, 就跳转到webapp/Error.jsp页面       -->
                <result name="error">/Error.jsp</result>
            </action>
        </package>
    </struts>

    4、HelloWorldAction.java : Action文件, 通过在struts.xml中配置了URL访问/index就回执行这个文件的execute()方法, 根据表单传入的name值来返回值, 在struts.xml中根据返回值再进行页面转发.

    package shiyanlou;
    
    public class HelloWorldAction {
    
        private String name;
    
        public String execute() throws Exception {
    
            if ( getName().equals("") || getName() == null )
                return "error";
    
            return "success";
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    }

    6、显示页面: 

    index.jsp : 登录的首页, 提供一个表单用来输入名字, 注意input中的name, 和HelloWorldAction.java中的String类型的name变量必须是一致的, 当变量的名称发生改动, 此处的for的属性值也必须改动.

    <%@ page language="java" contentType="text/html; charset=UTF-8"
             pageEncoding="UTF-8"%>
    <%@ taglib prefix="s" uri="/struts-tags" %>
    <!DOCTYPE html>
    <html>
    <head>
        <title>Hello World</title>
    </head>
    <body>
    <h1>Hello World Struts2</h1>
    <form action="hello">
        <label >Please enter your name</label><br/>
        <input type="text" name="name"/>
        <input type="submit" value="Enter"/>
    </form>
    </body>
    </html>

    HelloWorld.jsp : 用来显示成功输入的界面, 通过struts自带的EL语句来获得输入的名字, 注意此处的name同样的必须是和HelloWorldAction.java 中的String类型的变量name的名称保持一致.

    <%@ page language="java" contentType="text/html; charset=UTF-8"
             pageEncoding="UTF-8"%>
    <%@ taglib prefix="s" uri="/struts-tags" %>
    <!DOCTYPE html>
    <html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Hello World</title>
    </head>
    <body>
    Hello World, Welcome! <s:property value="name"/>
    </body>
    </html>

    Error.jsp : 失败的页面

    <%@ page language="java" contentType="text/html; charset=UTF-8"
             pageEncoding="UTF-8"%>
    <%@ taglib prefix="s" uri="/struts-tags" %>
    <!DOCTYPE html>
    <html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Hello World</title>
    </head>
    <body>
    You did not have entered a name!
    </body>
    </html>

    注意, HelloWorldAction.java 文件中的name和getName, setName的名称要保持一致

    如果改变名称为name22(这只是打一个比方, 可以改成其他任意的值), getName()和setName()同样要修改成getName22()和setName22()

    index.jsp中的input的name值也要改成name22, HelloWorld.jsp中的value也要改成name22


     注意: IDEA自动创建的maven项目的pom.xml文件中没有对发布的打包的方式的描述, 从而导致一直无法使用mvn jetty:run命令来通过jetty容器启动网页项目

    在pom.xml中田间<packaging>war</packaging>, 添加的位置看上面pom.xml, 设置打包的方式为war, 成功使用jetty容器.

    感觉好麻烦

  • 相关阅读:
    sql中触发器的使用
    sql中优化查询
    sql中case when语句的使用
    CSRF学习小结
    javascript正则表达式笔记
    elementUI单选框获取值
    elementUI内置过渡(折叠)
    elementUI内置缩放过渡(缩放)
    elementUi内置过渡动画(淡入)
    创建koa2项目
  • 原文地址:https://www.cnblogs.com/huangZ-H/p/12247346.html
Copyright © 2020-2023  润新知