2019獨(dú)角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>
一、搭建struts環(huán)境
1、在eclipse中新建一個(gè)java web項(xiàng)目
2、復(fù)制jar包
在下載的struts2中,有一個(gè)apps文件夾,這個(gè)文件夾下的.war文件即是官方給出的例子,其中struts2-blank.war是一個(gè)空的應(yīng)用,即里面什么都沒(méi)有。但是這個(gè)并不是最小的應(yīng)該。
解壓struts2-blank.war,將struts2-blank\WEB-INF\lib下的.jar文件復(fù)制到j(luò)ava web項(xiàng)目中的lib文件夾中、
3、配置web.xml文件
復(fù)制struts2-blank中web.xml中關(guān)于fileter的配置代碼到j(luò)ava web項(xiàng)目的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> 這個(gè)配置的意思是:
?所有的請(qǐng)求都要被StrutsPrepareAndExecuteFilter所攔截
4、添加struts-2的配置文件
將struts2-blank\WEB-INF\classes目錄下的struts2.xml文件復(fù)制到src下。可以刪除多余的東西,只保留struts根結(jié)點(diǎn)
5、添加struts.xml的提示
復(fù)制struts2.xml中的:http://struts.apache.org/dtds/struts-2.3.dtd
windows->preferences->xml->xml catalog
點(diǎn)擊add,將其復(fù)制到key后面的文本框中,key type選擇URI,
點(diǎn)擊file system,添加struts-2.3.dtd,位于:struts-2.3.16.3\src\core\src\main\resources
如圖所示:
ok
將struts.xml重新打開(kāi),可見(jiàn)提示:
二、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>
說(shuō)明:
Product.java
javaBean
index.jsp
首頁(yè),提供一個(gè)到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的響應(yīng)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>
運(yùn)行效果:
struts2.xml解釋
package: 包. struts2 使用 package 來(lái)組織模塊.
name 屬性: 必須. 用于其它的包應(yīng)用當(dāng)前包.
extends: 當(dāng)前包繼承哪個(gè)包, 繼承的, 即可以繼承其中的所有的配置. 通常情況下繼承 struts-default
struts-default 這個(gè)包在 struts-default.xml 文件中定義.
action: 一個(gè) struts2 的請(qǐng)求就是一個(gè) action
name: 對(duì)應(yīng)一個(gè) struts2 的請(qǐng)求的名字(或?qū)σ粋€(gè) servletPath, 但去除 / 和擴(kuò)展名), 不包含擴(kuò)展名
class 的默認(rèn)值為: com.opensymphony.xwork2.ActionSupport
method 的默認(rèn)值為: execute
result: 結(jié)果.
轉(zhuǎn)載于:https://my.oschina.net/iamhere/blog/488899
總結(jié)
以上是生活随笔為你收集整理的尚硅谷公开课--struts2--2--搭建struts2环境以及struts2简单例子的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。