第一个struts2 程序

环境: tomact7/jdk1.6/eclipse Indigo

今天开始看<<Struts 2 Design and Programming: A Tutorial>> by Budi Kurniawan,

由于公司不能下载,只能使用struts-2.2.1了。当我把所有的jar包copy到lib目录下启动tomact时(使用tomact6),竞然报错,找了一下,是少了这个包: javassist-3.11.0.GA.jar,找了个,再启动,还是报错,原来是没有写spring listener,删除了struts2包里所有的spring相关的包和所有的plugin相关包,启动ok.
以下为本书46页的第一个struts2小程序,运行ok!


核心中有如下几个文件,

Product.java:

package business.action;
import java.io.Serializable;;

public class Product implements Serializable{
	private String productName;
	private String description;
	private String price;
	
	public String getProductName() {
		return productName;
	}
	public void setProductName(String productName) {
		this.productName = productName;
	}
	public String getDescription() {
		return description;
	}
	public void setDescription(String description) {
		this.description = description;
	}
	public String getPrice() {
		return price;
	}
	public void setPrice(String price) {
		this.price = price;
	}
	
	public String execute(){
		return "success";
	}
}


product.jsp:

<html>
<head>
<title>Add Product Form</title>
</head>
<body>
	<div id="global" align="center">
		<h3>add a product</h3>
		<form method="post" action="Product_save.action">
			<table>
			<tr>
				<td>Product Name:</td>
				<td><input type="text" name="productName"/></td>
			</tr>
			<tr>
				<td>Description:</td>
				<td><input type="text" name="description"/></td>
			</tr>
			<tr>
				<td>Price:</td>
				<td><input type="text" name="price"/></td>
			</tr>							
			<tr>
				<td><input type="reset"/></td>
				<td><input type="submit" value="Add Product"/></td>
			</tr>							
			</table>
		</form>
	</div>
</body>
</html>



details.jsp:

<html>
<head>
<title>Save Product</title>
</head>
<body>
	<div id="global" align="center">
		<h4>The product has been saved.</h4>
		<p>
		       <h5>Details:</h5>
			product Names :${productName}<br/>
			Description :${description}<br/>
			Price :${price}<br/> 
			
		</p>
	</div>
</body>
</html>


struts.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
	<constant name="struts.enable.DynamicMethodInvocation" value="true" />
	<constant name="struts.devMode" value="false" />
	<constant name="struts.i18n.encoding" value="utf-8" />
	<constant name="struts.custom.i18n.resources" value="globalMessages" />

<include file="conf/action/product.xml" />
</struts>


product.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
	<package name="app02a" namespace="/" extends="struts-default">
		<action name="Product_input">
			<result>/jsp/product.jsp</result>
		</action>
		<action name="Product_save" class="business.action.Product">
			<result>/jsp/details.jsp</result>
		</action>
	</package>
</struts>


web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"  
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_5.xsd"  
version="2.5">  

	<filter>
		<filter-name>struts2</filter-name>
		<filter-class>
			org.apache.struts2.dispatcher.FilterDispatcher
		</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	
	<!-- 
	为防止直接进入jsp,以下security-constraint和login必须添加
	 -->
	
	<security-constraint>
		<web-resource-collection>
			<web-resource-name>JSPs</web-resource-name>
			<url-pattern>/jsp/*</url-pattern>
		</web-resource-collection>
		<auth-constraint/>
	</security-constraint>
	
	<login-config>
		<auth-method>BASIC</auth-method>
	</login-config>
	

    
</web-app>


无关jar包太多,文件有点大,不过,总算跑起来了。

猜你喜欢

转载自coderanch.iteye.com/blog/1597717