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上面已给
<?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
- Struts2简单构建
- maven构建struts2+spring项目简单实例
- WEB Struts2 构建struts2框架
- NetBeans构建struts2项目
- 构建struts2步骤
- Struts2构建HelloWorld
- Maven - 构建Struts2项目
- 构建一个Struts2项目
- Struts2框架的构建
- 使用maven构建struts2
- 胖子实习(一)——SSH(spring3+Struts2+Hibernate4)简单构建框架
- 使用Maven2 构建struts2项目
- 使用Maven构建Struts2项目
- 使用Maven_Jetty构建Struts2工程
- 使用Maven构建Struts2项目
- Maven构建struts2路径出错
- struts2 项目构建规划 (6)
- 构建简单http服务器
- 网络获取Json文件解析范例
- 那些双引号和单引号一起用的情况
- 关于BOM模型
- xlistview的写法
- VS2010 ENTITY FRAMEWORK6 SQLite3 安装搭建环境
- Struts2简单构建
- SQL的四种连接-左外连接、右外连接、内连接、全连接
- Maven入门(含实例教程)
- Docker学习二(部署一个可以自由获取环境变量的SpringWeb)
- Git
- 将一个CIImage转换为我们的UIImage
- odroidXu4 GPIO
- linux文件权限
- git,切换以前的版本,增加代码,然后合并的操作步骤