您的位置:首页 > 编程语言 > Java开发

Struts2 的 helloworld

2015-08-13 11:17 417 查看
配置步骤:

1.在你的strut2目录下找到例子项目,把它的 lib 下的jar拷贝到你的项目。例如我的:struts-2.3.24\apps\struts2-blank

2.struts-2.3.24\apps\struts2-blank\WEB-INF\src\ ( 例子项目的WEB-INF\src )下的 struts.xml 文件拷到你的项目src下,并且删除里面的内容,只留下根标签

  <strut> </strut>

3.在你的项目的web.xml下配置struts,看看例子项目的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>


4.helloworld演示:

先准备3个页面一个类:

1)index.jsp

<body>
<br>
<a href="product-input.action">Input</a>
</body>


2) input.jsp

<body>

<br>
<form action="product-save.action">
<table>
<tr>
<td>ProductId</td>
<td><input type="text" name="productId"></td>
</tr>
<tr>
<td>ProductName</td>
<td><input type="text" name="productName"></td>
</tr>
<tr>
<td>ProductDesc</td>
<td><input type="text" name="productDesc"></td>
</tr>
<tr>
<td>ProductPrice</td>
<td><input type="text" name="productPrice"></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="提交" /></td>
</tr>
</table>
</form>

</body>


3) details.jsp

<body>

<br><br>
<table>
<tr>
<td>ProductId</td>
<td>${productId }</td>
</tr>
<tr>
<td>ProductName</td>
<td>${productName }</td>
</tr>
<tr>
<td>ProductDesc</td>
<td>${productDesc }</td>
</tr>
<tr>
<td>ProductPrice</td>
<td>${productPrice }</td>
</tr>
</table>

</body>


4) 准备一个类

package com.gy.helloworld;

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) {
this.productName = productName;
}
public String getProductDesc() {
return productDesc;
}
public void setProductDesc(String productDesc) {
this.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 + "]";
}

}


5)以上目录结构为:



6) 配置struts.xml

<struts>
<package name="Struts2-1" extends="struts-default">
<action name="product-input">
<result>/WEB-INF/pages/input.jsp</result>
</action>

<action name="product-save" class="com.gy.helloworld.Product" method="save">
<result name="details">/WEB-INF/pages/details.jsp</result>
</action>
</package>
</struts>


7)注意:

a: extends="struts-default" 为继承struts默认拦截器配置

b:在第二个action里面 class="com.gy.helloworld.Product" method="save" 刚才我们的Product 类没有save()方法,一定要加上,且返回值为

<result name="details">/WEB-INF/pages/details.jsp</result>标签里面的name属性值:


public String save() {
System.out.println("product save ..");
return "details";

}


c:注意jsp各标签name属性和Product类的字段的对应。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: