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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

尚硅谷公开课--struts2--2--搭建struts2环境以及struts2简单例子

發布時間:2025/1/21 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 尚硅谷公开课--struts2--2--搭建struts2环境以及struts2简单例子 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

2019獨角獸企業重金招聘Python工程師標準>>>

一、搭建struts環境

1、在eclipse中新建一個java web項目

2、復制jar包

在下載的struts2中,有一個apps文件夾,這個文件夾下的.war文件即是官方給出的例子,其中struts2-blank.war是一個空的應用,即里面什么都沒有。但是這個并不是最小的應該。

解壓struts2-blank.war,將struts2-blank\WEB-INF\lib下的.jar文件復制到java web項目中的lib文件夾中、


3、配置web.xml文件

復制struts2-blank中web.xml中關于fileter的配置代碼到java web項目的web.xml中,我的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_2_5.xsd" id="WebApp_ID" version="2.5"><display-name>struts2-2</display-name><!-- 配置struts2的Filter --><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><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> 這個配置的意思是:


?所有的請求都要被StrutsPrepareAndExecuteFilter所攔截

4、添加struts-2的配置文件

將struts2-blank\WEB-INF\classes目錄下的struts2.xml文件復制到src下。可以刪除多余的東西,只保留struts根結點


5、添加struts.xml的提示

復制struts2.xml中的:http://struts.apache.org/dtds/struts-2.3.dtd

windows->preferences->xml->xml catalog

點擊add,將其復制到key后面的文本框中,key type選擇URI,

點擊file system,添加struts-2.3.dtd,位于:struts-2.3.16.3\src\core\src\main\resources

如圖所示:

ok

將struts.xml重新打開,可見提示:


二、struts例子

文件列表


├─src │ │ struts.xml │ │ │ └─com │ └─laolang │ └─domain │ Product.java │ └─WebContent│ index.jsp││├─pages│ details.jsp│ input.jsp│PS D:\program\java\tomcat\tomcat7\webapps\guigu\struts2\struts2-2>




說明:

Product.java

javaBean


index.jsp

首頁,提供一個到input.jsp的鏈接


input.jsp

輸入


details.jsp

顯示

代碼:

Product.java


package com.laolang.domain;/*** The Class Product.*/ public class Product {/*** Instantiates a new product.*/public Product() {super();}/*** Instantiates a new product.** @param productId* the product id* @param productName* the product name* @param productDesc* the product desc* @param productPrice* the product price*/public Product(Integer productId, String productName, String productDesc,double productPrice) {super();this.productId = productId;this.productName = productName;this.productDesc = productDesc;this.productPrice = productPrice;}/*** Save.* input的響應action** @return the string*/public String save() {return "details";}/** (non-Javadoc)* * @see java.lang.Object#toString()*/@Overridepublic String toString() {return "Product [productId=" + productId + ", productName="+ productName + ", productDesc=" + productDesc+ ", productPrice=" + productPrice + "]";}/*** Gets the product id.** @return the product id*/public Integer getProductId() {return productId;}/*** Sets the product id.** @param productId* the new product id*/public void setProductId(Integer productId) {this.productId = productId;}/*** Gets the product name.** @return the product name*/public String getProductName() {return productName;}/*** Sets the product name.** @param productName* the new product name*/public void setProductName(String productName) {this.productName = productName;}/*** Gets the product desc.** @return the product desc*/public String getProductDesc() {return productDesc;}/*** Sets the product desc.** @param productDesc* the new product desc*/public void setProductDesc(String productDesc) {this.productDesc = productDesc;}/*** Gets the product price.** @return the product price*/public double getProductPrice() {return productPrice;}/*** Sets the product price.** @param productPrice* the new product price*/public void setProductPrice(double productPrice) {this.productPrice = productPrice;}/** The product id. */private Integer productId;/** The product name. */private String productName;/** The product desc. */private String productDesc;/** The product price. */private double productPrice; }



index.jsp


<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>struts2例子</title> </head> <body><a href="product-input.action">Product Input</a><br><br></body> </html>



input.jsp


<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>輸入</title> </head> <body><form action="product-save.action" method="post">ProductId: <input type="text" name="productId"/><br><br>ProductName: <input type="text" name="productName"/><br><br>ProductDesc: <input type="text" name="productDesc"/><br><br>ProductPrice: <input type="text" name="productPrice" /><br><br><input type="submit" value="Submit"/><br><br></form></body> </html>



details.jsp


<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>顯示</title> </head> <body><% request.setCharacterEncoding("UTF-8");%>ProductId: ${productId }<br><br>ProductName: ${productName }<br><br>ProductDesc: ${productDesc }<br><br>ProductPrice: ${productPrice }<br><br></body> </html>



struts2.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><package name="demo" extends="struts-default"><action name="product-input"><result>/pages/input.jsp</result></action><action name="product-save" class="com.laolang.domain.Product" method="save"><result name="details">/pages/details.jsp</result></action></package> </struts>



運行效果:


struts2.xml解釋

package: 包. struts2 使用 package 來組織模塊.
name 屬性: 必須. 用于其它的包應用當前包.
extends: 當前包繼承哪個包, 繼承的, 即可以繼承其中的所有的配置. 通常情況下繼承 struts-default
struts-default 這個包在 struts-default.xml 文件中定義.

action: 一個 struts2 的請求就是一個 action
name: 對應一個 struts2 的請求的名字(或對一個 servletPath, 但去除 / 和擴展名), 不包含擴展名
class 的默認值為: com.opensymphony.xwork2.ActionSupport
method 的默認值為: execute
result: 結果.

轉載于:https://my.oschina.net/iamhere/blog/488899

總結

以上是生活随笔為你收集整理的尚硅谷公开课--struts2--2--搭建struts2环境以及struts2简单例子的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。