strust2入門

1.Actions是Struts2框架的核心,每個URL映射到特定的action

2.struts目前使用的不多,問題漏洞反饋比較多,框架出來的比較早,學習下基本的使用,實戰項目用的不多

代碼倉庫:https://gitee.com/DerekAndroid/strutsOne.git

參考教程:https://www.w3cschool.cn/struts_2/struts_examples.html

效果:

index.jsp 

<%--
  Created by IntelliJ IDEA.
  User: agen
  Date: 2019/12/29
  Time: 15:21
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
    <title>Hello World</title>
</head>
<body>
<h1>Hello World From Struts2</h1>

<%--Actions是Struts2框架的核心,每個URL映射到特定的action--%>
<form action="hello">
    <label for="name">Please enter your name</label><br/>
    <input type="text" name="name" id="name" placeholder="輸入error跳轉到error.jsp頁面"/>
    <input type="submit" value="Say Hello"/>
</form>

<form action="one">
    <input type="submit" value="直接跳轉到one.jsp"/>
</form>

<%--攔截器+--%>
<%--攔截器在概念上與servlet過濾器或JDK代理類相同。攔截器允許橫切功能,把action以及框架分開實現。--%>
<form action="interceptor">
  <label for="name">Please enter your name</label><br/>
  <input type="text" name="name" />
  <input type="submit" value="攔截器"/>
</form>

<%--結果類型Result:Redirect Action示例--%>
<form action="redirect">
  <input type="submit" value="結果類型:Redirect Action示例"/>
</form>

<%--驗證框架--%>
<%--Struts的核心中的驗證框架,可在執行action方法之前,幫助應用程序運行規則執行驗證--%>
<div>驗證框架</div>
<s:form action="empinfo" method="post">
  <s:textfield name="name" label="Name" size="20" />
  <s:textfield name="age" label="Age" size="20" />
  <s:submit name="submit" label="Submit" align="center" />
</s:form>

<%--Struts2 應用程序可以使用Java5註解來替代XML和Java屬性的配置--%>
<div>Struts2註解</div>
<s:form action="empinfotwo" method="post">
    <s:textfield name="name" label="Name" size="20" />
    <s:textfield name="age" label="Age" size="20" />
    <s:submit name="submit" label="Submit" align="center" />
</s:form>
</body>

HelloWorld.jsp

<%--
  Created by IntelliJ IDEA.
  User: agen
  Date: 2019/12/29
  Time: 15:28
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
    <title>Hello World</title>
</head>
<body>
Hello World,<s:property value="name"/>
</body>
</html>

struts.xml 

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
        "http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
    <constant name="struts.devMode" value="true" />
    <package name="helloworld" extends="struts-default">

<!--        直接跳轉jsp頁面-->
        <action name="one">
            <result>/one.jsp</result>
        </action>

        <action name="redirect">
            <result name="success" type="redirect">
                <param name="location">
                    /one.jsp
                </param >
            </result>
        </action>

        <action name="hello"
                class="strust.HelloWorldAction"
                method="execute">
            <result name="success">/HelloWorld.jsp</result>
            <result name="error">/error.jsp</result>
        </action>

        <action name="interceptor"
                class="strust.InterceptorOne"
                method="execute">
            <interceptor-ref name="params"/>
            <interceptor-ref name="timer" />
            <result name="success">/HelloWorld.jsp</result>
        </action>

        <action name="empinfo"
                class="strust.Employee"
                method="execute">
            <result name="input">/index.jsp</result>
            <result name="success">/success.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>index.jsp</welcome-file>
    </welcome-file-list>
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>
            org.apache.struts2.dispatcher.FilterDispatcher
        </filter-class>
        <init-param>
            <param-name>struts.devMode</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>

    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>

 

發佈了178 篇原創文章 · 獲贊 44 · 訪問量 24萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章