Struts2简单构建

来源:互联网 发布:java 两个数组值交换 编辑:程序博客网 时间:2024/06/01 08:27

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的小实例

注意与前面文章struts2_1:strust2的设计模式相比较

按照下面建立包和类




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;}@Overridepublic 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上面已给


再来配置重要的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="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 }







0 0
原创粉丝点击