Struts2 环境的搭建及和HelloWorld
2014-08-14 本文已影响325人
金发萌音
环境搭建
分为三步
- 引入关键包
- 加入struts的filter
- 配置struts的配置文件 struts.xml
准备 eclipse for javaEE
struts-2.3.16.3
首先是包
QQ截图20140813193017.png
将上述包复制到该位置中,其他包按需添加,这里的包只是基本
完全的包是struts-2.3.16.3/lib/里面的包 不用全复制进去
之后配置web.xml
向web.xml中添加如下内容
<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>
配置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>
这里是内容
</struts>
HelloWorld
微博桌面截图_20140814154214.jpg 微博桌面截图_20140814154602.jpg 微博桌面截图_20140814154628.jpg代码如下
struts.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="helloworld" extends="struts-default">
<action name="product-input">
<result>/WEB-INF/pages/input.jsp</result>
</action>
<action name="product-save" class="Filter.Product" method="save">
<result name="details">/WEB-INF/pages/details.jsp</result>
</action>
</package>
</struts>
Product.java
package Filter;
public class Product {
private Integer productId;
private String ProductName;
private String ProductDesc;
private double productPrice;
public Integer getProductId() {
return productId;
}
public void setProductId(Integer productId) {
this.productId = productId;
}
public String getProductName() {
return ProductName;
}
public void setProductName(String productName) {
ProductName = productName;
}
public String getProductDesc() {
return ProductDesc;
}
public void setProductDesc(String productDesc) {
ProductDesc = productDesc;
}
public double getProductPrice() {
return productPrice;
}
public void setProductPrice(double productPrice) {
this.productPrice = productPrice;
}
@Override
public String toString() {
return "Product [productId=" + productId + ", ProductName="
+ ProductName + ", ProductDesc=" + ProductDesc
+ ", productPrice=" + productPrice + "]";
}
public String save(){
return "details";
}
}
pages/input.jsp
<body>
<form action="product-save.action" method="post">
ProductName:<input type="text" name="productName"/>
<br> <br>
ProductName:<input type="text" name="productDesc"/>
<br> <br>
ProductName:<input type="text" name="productPrice"/>
<br> <br>
<input type="submit" value="Submit"/>
</form>
</body>
pages/details.jsp
<body>
ProductID: ${productId }
<br><br>
ProductName: ${productName }
<br><br>
ProductDesc: ${productDesc }
<br><br>
ProductPrice: ${productPrice }
<br><br>
</body>