日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Struts2 学习笔记 — 第一个struts2项目

發(fā)布時間:2025/7/14 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Struts2 学习笔记 — 第一个struts2项目 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

創(chuàng)建struts2項目

勾選生成web.xml文件

配置項目

打開項目后,首先導(dǎo)入struts2所需的jar包,關(guān)于這個,我們可以在下載的struts包中struts-2.3.34/apps中找到struts2-blank.war文件,解壓,

  • 進入src->找到lib文件夾, 將里面的jar文件全部復(fù)制到 我們創(chuàng)建的項目中 /WEB-INF/lib 中
  • 在java src中創(chuàng)建 struts.xml文件
  • 配置web.xml文件,配置Struts的核心過濾器
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"><filter><filter-name>struts2</filter-name><filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class></filter><filter-mapping><filter-name>struts2</filter-name><url-pattern>/*</url-pattern></filter-mapping> </web-app>

構(gòu)建項目

這里是做一個輸入產(chǎn)品信息并顯示在另一個頁面上的功能

  • 創(chuàng)建action

實際就是創(chuàng)建一個處理業(yè)務(wù)邏輯的Java類,

package com.theodore.struts2;public class Production {private int proId;private String proName;private String proDesc;private String proPrice;public Production(int proId, String proName, String proDesc, String proPrice) {this.proId = proId;this.proName = proName;this.proDesc = proDesc;this.proPrice = proPrice;}public Production() {}@Overridepublic String toString() {return "Production{" +"proId=" + proId +"," + "proName=" + proName + "," +"proDesc=" + proDesc + "," +"proPrice=" + proPrice +"}";}public int getProId() {return proId;}public void setProId(int proId) {this.proId = proId;}public String getProName() {return proName;}public void setProName(String proName) {this.proName = proName;}public String getProDesc() {return proDesc;}public void setProDesc(String proDesc) {this.proDesc = proDesc;}public String getProPrice() {return proPrice;}public void setProPrice(String proPrice) {this.proPrice = proPrice;}public String save() {System.out.println("result:" + this);return "detail";} }
  • 在struts.xml文檔中配置這個action

其實質(zhì)就是告訴頁面如何使用這個action,具體配置如下:

<?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><package name="helloworld" namespace="/" extends="struts-default"><action name="product-input"><result>/pages/input.jsp</result></action><action name="product-save" class="com.theodore.struts2.Production" method="save"><result name="detail">/pages/details.jsp</result></action></package></struts>
  • jsp頁面
  • index.jsp
<a href="product-input.action">Product Input : </a>
  • input.jsp
<form action="product-save.action" method="post">ProductName: <input type="text" name="proName"><br/>ProductDesc: <input type="text" name="proDesc"><br/>ProductPrice: <input type="text" name="proPrice"><br><input type="submit" value="確認"><br/> </form>
  • detail.jsp
<body>ProductId: ${proId} <br><br>ProductName: ${proName} <br><br>ProductDesc: ${proDesc} <br><br>ProductPrice: ${proPrice} <br><br> </body>

這里可以直接使用action中的變量名,因為struts2 中已經(jīng)為我們配置好了對應(yīng)關(guān)系。

總結(jié)

以上是生活随笔為你收集整理的Struts2 学习笔记 — 第一个struts2项目的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。