Struts2的環境配置
下載最新的struts的jar包,到app裏找到struts2-blank.war解壓,struts2-blank.war中black意味着空的工程,不會有多餘的jar包。搭建Struts2的環境:
--加入jar包:複製struts\app\struts2-blank\WEB-INF\lib下的所有jar包到當前web應用的lib目錄下。
--在web.xml文件中配置struts2:複製struts\app\struts2-blank\WEB-INF\lib文件中的過濾器配置到當前web應用的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_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>struts2_2</display-name>
<span style="color:#ff0000;">
<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></span>
<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>
--在當前web應用的classpath下添加struts2的配置文件struts.xml:複製struts\app\struts2-blank\WEB-INF\classes下的struts.xml文件到當前web應用的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>
</struts>
添加DTD約束
如果struts.xml默認沒有提示,需要提示的話添加DTD約束
下面寫兩個struts2的小實例
按照下面建立包和類
Product.java
package com.wul.strust2.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 + "]";
}
public String save(){
System.out.println("save: "+this.toString());
return "details";
}
}
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>Hello</title>
</head>
<body>
<a href="product-input.action">Product Input</a>
</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>Hello</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"/>
</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>Hello</title>
</head>
<body>
ProductId: ${productId }
<br><br>
ProductName: ${productName }
<br><br>
ProductDesc: ${productDesc }
<br><br>
ProductPrice: ${productPrice }
<br><br>
</body>
</html>
web.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="wul" namespace="/" extends="struts-default">
<action name="product-input">
<result>/WEB-INF/pages/input.jsp</result>
</action>
</package>
</struts>
上面配置了product-input的action,可以完成由index.jsp轉到WEB-INF/pages/input.jsp
再來添加product-save的action,
<action name="product-save" class="com.wul.strust2.helloworld.Product" method="save">
<result name="details">/WEB-INF/pages/details.jsp</result>
</action>
可以完成由.由input.jsp頁面的action:product-save.action 到Product' save方法,再到WEB-INF/pages/details.jsp
我們來對其與之前的struts2_1:strust2的設計模式中的工程做個比較與總結
1).搭建Struts2的開發環境
2).不需要顯示的定義Filter,而是使用struts2的配置文件
3).details.jsp比以前變得簡單
${requestScope.product.productId } -->${productId }