struts2入門Demo,struts2demo

來源:互聯網
上載者:User

struts2入門Demo,struts2demo

一.引入必要的jar包,所需jar包如下:

二.配置web.xml.主要目的是攔截請求

<?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"          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>struts2</display-name>  <welcome-file-list>    <welcome-file>index.jsp</welcome-file>  </welcome-file-list>  <filter>    <filter-name>s2</filter-name>    <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>  </filter>  <filter-mapping>   <filter-name>s2</filter-name>   <url-pattern>*.action</url-pattern>  </filter-mapping></web-app>

三.建立並編寫struts.xml,建立位置在src目錄下

<?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>   <package name="Demo" extends="struts-default">   <!-- 2.5之後使用萬用字元時需要使用這句代碼,否則會發生一些錯誤 -->   <global-allowed-methods>regex:.*</global-allowed-methods>       <action name="simple_*" class="com.hck.controller.SimpleAction" method="{1}">        <result name="ok">/MyJsp.jsp</result>        <result name="success">/index.jsp</result>           </action>   </package></struts>

四.編寫action類,該類需要繼承ActionSupport,在編寫時需要注意action方法的格式,action方法不能帶有參數

public class SimpleAction extends ActionSupport{   List<String> lists=new ArrayList<String>();    public List<String> getLists() {    return lists;}public void setLists(List<String> lists) {    this.lists = lists;}    public String List()    {                lists.add("張三");        lists.add("李四");        lists.add("王五");        lists.add("老劉");        lists.add("123");        System.out.println("-----------");        return "ok";    }}

五.編寫兩個用於測試的jsp頁面。index.jsp跟MyJsp.jsp,其中MyJsp.jsp使用到了sturts2標籤所以需要引入相應的包。

 <body>      <form action="simple_List.action" method="post">             使用者名稱:<input type="text" name="username"/>             密碼:<input type="password" name="password"/>          <button type="submit">提交</button>      </form>  </body>
<%@taglib prefix="s" uri="/struts-tags" %><body>   <s:debug></s:debug>   <s:iterator value="lists" var="list">   <s:property value="list"/>   </s:iterator>  </body>

六.測試結果

七.總結:struts2相較於servlet來說要便捷的多,省去了許多的煩雜的代碼量.只有有一個全域的變數,再定義其set跟get方法即可在跳轉頁面輕鬆擷取其資料。

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.