配置步骤:

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. Struts2 Convention插件的使用(3)方法前的@Action注解

    package com.hyy.action; import org.apache.struts2.convention.annotation.Action; import com.opensymph ...

  2. java 语法错误 (操作符丢失) 在查询表达式

    遇到的详细问题: a[0]="11"; a[1]="2223"; a[2]="333"; sta.executeUpdate("i ...

  3. Android:Android SDK Manager

    Android SDK Manager 包含:Tools(构建工具.编译工具.平台工具等) .各种版本SDK.Extras(安卓知识库和辅助工具) 每个SDK至少包含:1.SDK Plaform 2. ...

  4. 275. H-Index II

    题目: Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optim ...

  5. Java:包的使用Pack

    在包A中创建一个类并在类中定义一个方法 package packA; public class PackDemoA { public void show() { System.out.println( ...

  6. AlarmManager.RTC和ELAPSED_REALTIME的区别

    AlarmManager.RTC,硬件闹钟,不唤醒手机(也可能是其它设备)休眠:当手机休眠时不发射闹钟. AlarmManager.RTC_WAKEUP,硬件闹钟,当闹钟发躰时唤醒手机休眠: Alar ...

  7. html 页面表单如果是disabled,则不能提交到服务器端,request.getParameter得到的将为null

    html 页面表单如果是disabled,则不能提交到服务器端,request.getParameter得到的将为null 解决方法:使用hidden 利用javascript赋值,传递到后台

  8. opencv 金字塔图像分割

    我所知的opencv中分割函数:watershed(只是看看效果,不能返回每类pixel类属),cvsegmentImage,cvPyrSegmentation(返回pixel类属) 金字塔分割原理篇 ...

  9. 设置Windows Azure Linux虚拟机中的root账户

    使用Windows Azure 创建好Linux虚拟机之后,如果你使用默认的用户密码登陆root是不行的,如下图所示: 其原因是Windows Azure创建Linux虚拟机时并没有同时设置root密 ...

  10. windows下的文件遍历(使用CFindFile)

    这个我一直都很想做了,前两天开始准备,查找了一下CFindFile的资料,然后把思路理清楚,就直接开始做了. 文件系统是一个操作系统以一部分,所以想文件操作,基本上就要依赖于操作系统提供的接口函数. ...