配置步骤:

1.在你的strut2目录下找到例子项目,把它的 lib 下的jar拷贝到你的项目。例如我的:struts-2.3.24\apps\struts2-blank

2.struts-2.3.24\apps\struts2-blank\WEB-INF\src\ ( 例子项目的WEB-INF\src )下的 struts.xml 文件拷到你的项目src下,并且删除里面的内容,只留下根标签

  <strut> </strut>

3.在你的项目的web.xml下配置struts,看看例子项目的web.xml,复制即可:

 <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>

4.helloworld演示:

先准备3个页面一个类:

1)index.jsp

 <body>
<br>
<a href="product-input.action">Input</a>
</body>

2) input.jsp

<body>

        <br>
<form action="product-save.action">
<table>
<tr>
<td>ProductId</td>
<td><input type="text" name="productId"></td>
</tr>
<tr>
<td>ProductName</td>
<td><input type="text" name="productName"></td>
</tr>
<tr>
<td>ProductDesc</td>
<td><input type="text" name="productDesc"></td>
</tr>
<tr>
<td>ProductPrice</td>
<td><input type="text" name="productPrice"></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="提交" /></td>
</tr>
</table>
</form> </body>

3) details.jsp

<body>

            <br><br>
<table>
<tr>
<td>ProductId</td>
<td>${productId }</td>
</tr>
<tr>
<td>ProductName</td>
<td>${productName }</td>
</tr>
<tr>
<td>ProductDesc</td>
<td>${productDesc }</td>
</tr>
<tr>
<td>ProductPrice</td>
<td>${productPrice }</td>
</tr>
</table> </body>

4) 准备一个类

package com.gy.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 + "]";
} }

5)以上目录结构为:

6) 配置struts.xml

<struts>
<package name="Struts2-1" extends="struts-default">
<action name="product-input">
<result>/WEB-INF/pages/input.jsp</result>
</action> <action name="product-save" class="com.gy.helloworld.Product" method="save">
<result name="details">/WEB-INF/pages/details.jsp</result>
</action>
</package>
</struts>

7)注意:

a: extends="struts-default" 为继承struts默认拦截器配置

b:在第二个action里面 class="com.gy.helloworld.Product" method="save" 刚才我们的Product 类没有save()方法,一定要加上,且返回值为

<result name="details">/WEB-INF/pages/details.jsp</result>标签里面的name属性值:
public String save() {
System.out.println("product save ..");
return "details"; }

c:注意jsp各标签name属性和Product类的字段的对应。

Struts2 的 helloworld的更多相关文章

  1. 使用maven+eclipse搭建最简单的struts2的helloworld

    使用maven+eclipse搭建最简单的struts2的helloworld 一.web分层结构简介 1.web[细]粒度分层结构: 按细粒度分层可以分为以下6种: 1).表现层:html/css/ ...

  2. Struts2之HelloWorld

    首先既然是开发Struts程序的话,那么自然需要用到Struts2开发包,Struts2是apache旗下的开源框架,所有的开发包和源代码都可以在Apache官网下载. 那么,就来开始编写第一个Str ...

  3. 创建一个struts2的HelloWorld

    1.下载struts2的jar包 http://struts.apache.org/download.cgi#struts255 下载一个稳定版本Struts 2.3.31 里面提供了maven ja ...

  4. 使用MyEclipse 9.0 创建 struts2 的HelloWorld 工程

    作为眼高手低的典范,必须痛改前非,好好动手多做开发! 本文是struts2的入门指导,权当笔记! 在MyEclipse中,配置Apache tomcat 7.x 服务器 创建新的Web project ...

  5. 2.第一个Struts2程序-HelloWorld程序

    1.新建Web Project项目:Study_Struts2 2.新建HelloWordAction.java类 3.复制struts.xml文件到src目录下,配置struts.xml文件内容如下 ...

  6. struts2的HelloWorld的基本过程

    login.jsp中 <form action="Login"... 该页面提交后, web.xml中设置的过滤器会起作用     <filter>        ...

  7. Struts2学习笔记 - HelloWorld总结

    相信网上已经有很多关于struts2的HelloWorld可参考,我这里就不重复了,没个学struts2的人都会做过很多个HelloWorld,而我这里就总结一下一个简单的HelloWorld 我在一 ...

  8. 菜鸟学Struts2——Struts工作原理

    在完成Struts2的HelloWorld后,对Struts2的工作原理进行学习.Struts2框架可以按照模块来划分为Servlet Filters,Struts核心模块,拦截器和用户实现部分,其中 ...

  9. 2. Struts2 基础

    1. Struts2简介 Struts2是一个WEB端MVC框架.作为比较早的MVC 框架之一,Struts2在使用中还是比较多的.虽然个人感受没有SpringMVC还那么的好用 Struts2 官网 ...

随机推荐

  1. ActionResult 返回类型

    类名 抽象类 父类 功能 ContentResult     根据内容的类型和编码,数据内容. EmptyResult     空方法. FileResult abstract   写入文件内容,具体 ...

  2. 用android模拟器Genymotion定位元素

    1.下载并安装android模拟器Genymotion 2.拖apk包到模拟器,双击模拟器中的apk软件包,进入应用程序 3.下载并安装android sdk 3.1 点击...\Android\an ...

  3. Python十分钟学会

    初试牛刀 假设你希望学习Python这门语言,却苦于找不到一个简短而全面的入门教程.那么本教程将花费十分钟的时间带你走入Python的大门.本文的内容介于教程(Toturial)和速查手册(Cheat ...

  4. VCC、VDD、VEE、VSS等有关电源标注的区别

    Almost all integrated circuits (ICs) have at least two pins which connect to the power rails of the ...

  5. [PHP]利用XAMPP搭建本地服务器, 然后利用iOS客户端上传数据到本地服务器中(三. PHP端代码实现)

    一.安装XAMPP   http://www.cnblogs.com/lidongxu/p/5256330.html 二. 配置MySql http://www.cnblogs.com/lidongx ...

  6. CentOS7-64bit 编译 Hadoop-2.5.0,并分布式安装

    摘要 CentOS7-64bit 编译 Hadoop-2.5.0,并分布式安装 目录[-] 1.系统环境说明 2.安装前的准备工作 2.1 关闭防火墙 2.2 检查ssh安装情况,如果没有则安装ssh ...

  7. 如何避免JSP页面自动生成session对象?为什么要这么做?

    JSP // 在默认情况下,在对一个JSP页面发出请求时,如果session还没有建立,JSP页面会自动为请求建立一个session对象,但是session是比较消耗资源的,如果没必要保持和使用ses ...

  8. jQuery deferred when用法

    一.什么是deferred对象? 开发网站的过程中,我们经常遇到某些耗时很长的javascript操作.其中,既有异步的操作(比如ajax读取服务器数据),也有同步的操作(比如遍历一个大型数组),它们 ...

  9. wust 1061 链表的合并

    怒刷存在感! ~从此wustoj踏上ty博客这样高端霸气上档次的地方啊啦啦~ 只是顺便看了下保研复试题,原来觉得链表好讨厌,现在数据结构学的没办法了,写了个大概是标准的链表合并的写法吧... #inc ...

  10. Java面试汇总

    转自:http://zy19982004.iteye.com/blog/1846537#comments 一.All 最近找工作,遇到的笔试面试题,归纳如下,供大家参考. 二.J2SE 容器 Hash ...