配置步骤:

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. OAuth 2 的简单理解

    什么是 OAuth 2.0 根据 oauth.net 的描述,我们可以将它简述为以下内容:OAuth 2.0 是 OAuth 1.0 框架协议的升级版本,简化了多种平台上身份及授权认证的流程. 具体的 ...

  2. 浅析dex文件加载机制

    我们可以利用DexClassLoader来实现动态加载dex文件,而很多资料也只是对于DexClassLoader的使用进行了介绍,没有深入讲解dex的动态加载机制,我们就借助于Android4.4的 ...

  3. VS2013试用期结束后如何激活

    在激活框中输入密钥:BWG7X-J98B3-W34RT-33B3R-JVYW9

  4. hdu 1124 Factorial(数论)

    题意: 求n!的尾0的个数 分析: 0一定是由因子2和5相乘产生的: 2的个数显然大于5的个数,故只需统计因子5的个数 n/5不能完全表示n!中5的个数(egg: 25),应该n/=5后,累加上n/2 ...

  5. 进入第一个Android应用界面

    前话 距离上次学习Android已经过去了半年了,这半年我干嘛去了? 嘛相信大家也没兴趣了解,简单来说就是我学习了周边的知识技术,最后终于转回Android. 感觉开发一个Android需要很多知识吧 ...

  6. 301. Remove Invalid Parentheses

    题目: Remove the minimum number of invalid parentheses in order to make the input string valid. Return ...

  7. Linux命令-date

    [root@localhost ~]# date 2016年 09月 07日 星期三 :: CST [root@localhost ~]# date "+%Y" [root@loc ...

  8. android 使用静态变量传递数据

    使用静态变量传递数据之通用方式. 测试应用:当前页面点击button传递数据到一个新的页面显示在textview中. 首先在,mainActivity.xml文件中加入一个button按钮 <B ...

  9. Linux下 执行程序

    看到有人问Linux下的./表示什么意思,我就趁机在这里写一下个人愚见: ./的意思是执行当前目录下的某可执行文件. . /相当于 source 根目录下的一个脚本.

  10. Matlab绘图高级部分

    图形是呈现数据的一种直观方式,在用Matlab进行数据处理和计算后,我们一般都会以图形的形式将结果呈现出来.尤其在论文的撰写中,优雅的图形无疑会为文章加分.本篇文章非完全原创,我的工作就是把见到的Ma ...