搭建一个Struts2工程
生活随笔
收集整理的這篇文章主要介紹了
搭建一个Struts2工程
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1、使用Eclipse創建一個Dynamic Web Project功能。
2、從Struts2官網上下載Struts2 struts-2.3.30-all.zip
3、向工程中導入Struts2所必須的jar包:
4、在web.xml文件中加入Struts2的配置信息
5、在src目錄下創建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="com.zjp.domain.Product" method="save"><result name="details">/WEB-INF/pages/details.jsp</result></action></package></struts>在struts.xml中配置一個action,與之對應的class類為Product
package com.zjp.domain;public class Product {private int productId;private String productName;private String productDesc;private String productPrice;public int getProductId() {return productId;}public void setProductId(int 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 String getProductPrice() {return productPrice;}public void setProductPrice(String productPrice) {this.productPrice = productPrice;}@Overridepublic String toString() {return "Product [productId=" + productId + ", productName="+ productName + ", productDesc=" + productDesc+ ", productPrice=" + productPrice + "]";}public String save(){System.out.println("保存product"+this);return "details";} }配置文件中method對應的方法與product類中的save必須一一對應,在result中當save方法返回detail字符串,將跳轉到/WEB-INF/pages/details.jsp
Product參數需要從一個表中輸入,在index.jsp頁面中:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"pageEncoding="ISO-8859-1"%> <!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=ISO-8859-1"> <title>Insert title here</title> </head> <body><a href="product-input.action">Product Input</a> </body> </html>當用戶點擊Product Input時,在struts.xml中配置其跳轉到/WEB-INF/pages/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>Insert title here</title> </head> <body><form action="product-save.action" method="post">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>當用戶提交該表格,product-save.action設置跳轉到detail頁面。
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags" %> <!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>Insert title here</title> </head> <body>ProductId: ${productId }<br><br>ProductName: ${productName }<br><br>ProductDesc: ${productDesc }<br><br>ProductPrice: ${productPrice }<br><br></body> </html>測試結果:
總結
以上是生活随笔為你收集整理的搭建一个Struts2工程的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 体彩3中3复式中奖几组啊?
- 下一篇: Hibernate环境搭建以及Hello