Struts2学习:HelloWorld

项目结构:html

一、用IDEA新建一个SpringBoot+Maven的项目java

二、新建的项目是没有webapp、WEB-INF、与web.xml文件的,须要在下图中添加:web

三、在pom.xml引入struct2apache

<dependency>
    <groupId>org.apache.struts</groupId>
    <artifactId>struts2-core</artifactId>
    <version>2.5.18</version>
</dependency>

四、编写主页home.jsp:浏览器

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
    <title>Home</title>
</head>
<body>
<form action="hello">
    <label for="pername">Please enter your name</label>
    <input id="pername" type="text" name="name" />
    <input type="submit" value="提交" />
</form>
</body>
</html>

该页面输入一个名字,经过名为hello的action,将名字显示在另外一个页面。服务器

五、定义动做HelloWorldAction.javaapp

public class HelloWorldAction {
    private String name;
    public String excute(){
        return "success";
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

六、jsp页面与动做之间须要经过WEB-INF\classes\struts.xml关联,且须要在web.xml中定义过滤器webapp

struts.xml以下:jsp

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
        "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
    <!-- 设置struts是否为开发模式,默认为false,测试阶段通常设为true. -->
    <constant name="struts.devMode" value="true" />
    <package name="suibian" extends="struts-default">
        <action name="hello" class="com.owlforest.home.action.HelloWorldAction" method="excute">
            <result name="success">/HelloWorld.jsp</result>
        </action>
    </package>
</struts>

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_4_0.xsd"
         version="4.0">
    <display-name>Struts 2</display-name>
    <welcome-file-list>
        <welcome-file>home.jsp</welcome-file>
    </welcome-file-list>

    <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>
</web-app>

七、编写跳转页HelloWorld.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
    <title>show name</title>
</head>
<body>
Hello World : <s:property value="name" />
</body>
</html>

须要注意的是,home.jsp、HelloWorldAction.java与HelloWorld.jsp中的name属性须要在名称上面保持一致。

form表单中的action名字与struct.xml中的action name要保持一致

这个地方在属性过多的状况下可能会出现命名冲突致使错误出现,不知道struct2是如何解决的,后续再继续学习。

八、result的type参数

上面的struts.xml配置的result为:

<result name="success">/HelloWorld.jsp</result>

这实际上时一种简写的形式,完整的形式为:

<result name="success" type="dispatcher">
    <param name="location">/HelloWorld.jsp</param>
</result>

dispatcher结果类型是默认的类型。它用于转发到服务器上的servlet,JSP,HTML等页面。它使用RequestDispatcher.forward()方法。

除了上述的dispatcher类型外,还有redirect、freemarker类型。

redirect结果类型调用标准的response.sendRedirect()方法,使得浏览器向给定的位置建立一个新请求。

<result name="success" type="redirect">
    <param name="location">https://www.baidu.com</param>
</result>

freemarker是一个流行的模板引擎,使用预约义的模板生成输出。这与JSP相似。使用方法以下:

在webapp下新建文件 hello.fm,内容为:

What's your name ?${name}

修改struts.xml便可完成配置。

<result name="success" type="freemarker">
    <param name="location">/hello.fm</param>
</result>
相关文章
相关标签/搜索