在下載的struts2中,有一個apps文件夾,這個文件夾下的.war文件便是官方給出的例子,其中struts2-blank.war是一個空的應用,即裏面什麼都沒有。可是這個並非最小的應該。 html
解壓struts2-blank.war,將struts2-blank\WEB-INF\lib下的.jar文件複製到java web項目中的lib文件夾中、 java
複製struts2-blank中web.xml中關於fileter的配置代碼到java web項目的web.xml中,個人web.xml文件以下: web
<?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所攔截 shell
將struts2-blank\WEB-INF\classes目錄下的struts2.xml文件複製到src下。能夠刪除多餘的東西,只保留struts根結點 apache
複製struts2.xml中的:http://struts.apache.org/dtds/struts-2.3.dtd windows
windows->preferences->xml->xml catalog tomcat
點擊add,將其複製到key後面的文本框中,key type選擇URI, app
點擊file system,添加struts-2.3.dtd,位於:struts-2.3.16.3\src\core\src\main\resources dom
如圖所示: eclipse
ok
將struts.xml從新打開,可見提示:
├─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
顯示
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() */ @Override public 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; }
<%@ 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>
<%@ 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>
<%@ 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>
<?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>
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: 結果.