1、下载struts2的jar包

http://struts.apache.org/download.cgi#struts255

下载一个稳定版本Struts 2.3.31

里面提供了maven jar

<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.3.31</version>
</dependency>

2、创建一个动态web工程导入这些jar包

修改web.xml文件

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
  5.  
  6. <display-name>Struts Demo</display-name>
  7.  
  8. <filter>
  9. <filter-name>struts2</filter-name>
  10. <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  11. </filter>
  12.  
  13. <filter-mapping>
  14. <filter-name>struts2</filter-name>
  15. <url-pattern>/*</url-pattern>
  16. </filter-mapping>
  17.  
  18. <welcome-file-list>
  19. <welcome-file>index.jsp</welcome-file>
  20. </welcome-file-list>
  21.  
  22. </web-app>

可以参照struts2-blank中的配置

3、在src目录下创建struts.xml

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE struts PUBLIC
  3. "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
  4. "http://struts.apache.org/dtds/struts-2.3.dtd">
  5.  
  6. <struts>
  7.  
  8. <constant name="struts.enable.DynamicMethodInvocation" value="false" />
  9. <constant name="struts.devMode" value="false" />
  10.  
  11. <package name="default" namespace="/" extends="struts-default">
  12.  
  13. <default-action-ref name="index" />
  14.  
  15. <global-results>
  16. <result name="error">/WEB-INF/jsp/error.jsp</result>
  17. </global-results>
  18.  
  19. <global-exception-mappings>
  20. <exception-mapping exception="java.lang.Exception"
  21. result="error" />
  22. </global-exception-mappings>
  23.  
  24. <action name="index">
  25. <result type="redirectAction">
  26. <param name="actionName">HelloWorld</param>
  27. <param name="namespace">/example</param>
  28. </result>
  29. </action>
  30.  
  31. <action name="products-input">
  32. <result>/WEB-INF/pages/products.jsp</result>
  33. </action>
  34.  
  35. <action name="products-detail" class="com.hy.Products">
  36. <result name="success">/WEB-INF/pages/detail.jsp</result>
  37. </action>
  38.  
  39. </package>
  40.  
  41. <!-- Add packages here -->
  42.  
  43. </struts>

参照truts2-blank中的配置

struts.enable.DynamicMethodInvocation指的是是否支持模糊匹配方法名

这里面有一个pojo

  1. package com.hy;
  2.  
  3. import com.opensymphony.xwork2.ActionSupport;
  4.  
  5. @SuppressWarnings("serial")
  6. public class Products{
  7.  
  8. private String productID;
  9.  
  10. private String productName;
  11.  
  12. private String productDesc;
  13.  
  14. public String getProductID() {
  15. return productID;
  16. }
  17.  
  18. public void setProductID(String productID) {
  19. this.productID = productID;
  20. }
  21.  
  22. public String getProductName() {
  23. return productName;
  24. }
  25.  
  26. public void setProductName(String productName) {
  27. this.productName = productName;
  28. }
  29.  
  30. public String getProductDesc() {
  31. return productDesc;
  32. }
  33.  
  34. public void setProductDesc(String productDesc) {
  35. this.productDesc = productDesc;
  36. }
  37.  
  38. public String detail() {
  39. return SUCCESS;
  40. }
  41.  
  42. @Override
  43. public String execute() throws Exception {
  44. return SUCCESS;
  45. }
  46.  
  47. }

两个跳转的页面

  1. <%@ page language="java" contentType="text/html; charset=utf-8"
  2. pageEncoding="utf-8"%>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  4. <html>
  5. <head>
  6. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  7. <title>商品录入</title>
  8. </head>
  9. <body>
  10. <form action="products-detail" method="post">
  11. productID:<input type="text" name="productID" /><br>
  12. productName:<input type="text" name="productName" /><br>
  13. productDesc:<input type="text" name="productDesc" /><br>
  14. <input type="submit" value="submit"><br>
  15. </form>
  16. </body>
  17. </html>
  1. <%@ page language="java" contentType="text/html; charset=utf-8"
  2. pageEncoding="utf-8"%>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  4. <html>
  5. <head>
  6. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  7. <title>商品详细</title>
  8. </head>
  9. <body>
  10. productID:<input type="text" name="productID" value="${productID}"/><br>
  11. productName:<input type="text" name="productName" value="${productName}"/><br>
  12. productDesc:<input type="text" name="productDesc" value="${productName}"/><br>
  13. </body>
  14. </html>

大概就这样

注意:

导入struts2-blank到eclipse时报错,这是一个maven工程

首先要安装maven插件(高版本eclipse自带)

http://maven.apache.org/download.cgi

选择一个较新的版本下载

下载完成后解压到本地,配置m2_home

D:\Program Files\apache-maven-3.3.9

再配置bin目录到path变量中

测试 cmd mvn -version

现在import exist maven projects 导入后项目编译报错

选择直接忽略。具体原因未知

这时候maven update project 把改下的jar包下载到maven jar包仓库中

启动tomcat会发现struts2核心包classnotfound

需要把maven dependence加入到部署环境中

运行之后可能会出现jsp报错,这是因为maven中包含了较低版本的tomcat jar删除依赖后启动成功

代码链接:http://pan.baidu.com/s/1qXDXli0

创建一个struts2的HelloWorld的更多相关文章

  1. struts2官方 中文教程 系列一:创建一个struts2 web Application

    先贴了本帖地址,以免被爬  http://www.cnblogs.com/linghaoxinpian/p/6898779.html 本教程将会通过安装struts2框架来创建一个简单的应用程序.虽然 ...

  2. 创建一个Spring的HelloWorld程序

    Spring IOC IOC指的是控制反转,把对象的创建.初始化.销毁等工作都交给Spring容器.由spring容器来控制对象的生命周期.下图能够说明我们传统创建类的方式和使用Spring之后的差别 ...

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

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

  4. Node.js快速创建一个Express应用的几个步骤

    Node.js 的 Express 框架学习 转载和参考地址: https://developer.mozilla.org/zh-CN/docs/Learn/Server-side/Express_N ...

  5. SpringMVC基础入门,创建一个HelloWorld程序

    ref:http://www.admin10000.com/document/6436.html 一.SpringMVC基础入门,创建一个HelloWorld程序 1.首先,导入SpringMVC需要 ...

  6. 简单创建一个完整的struts2框架小程序

    要完成一个struts2框架的搭建, 1.首先应该从官网上下载最新的jar包,网络连接:http://struts.apache.org/download.cgi#struts2514.1,选择下载F ...

  7. 创建一个spring helloworld

    1.下载所需要的jar包 http://projects.spring.io/spring-framework/ 这里使用了maven方式给出jar <dependencies> < ...

  8. 如何创建一个简单的struts2程序

    如何创建一个简单的Struts2程序 “计应134(实验班) 凌豪” 1.创建一个新的Web项目test(File->new->Web Project) 2.Struts2框架的核心配置文 ...

  9. 图解使用IDEA创建第一个Java程序HelloWorld

    前几次给大家分享了怎么在自己的电脑上配置 java 环境,准备工作做好了,我们就要开始我们真正的编码学习了.下面介绍使用 IDEA 创建我们的第一个 HelloWorld 程序. 1.打开 IDEA, ...

随机推荐

  1. debug,trace,release项目配置区别

    Debug模式是用来调试用的,它生成的执行文件中含有大量调试信息,所以很大: Release模式生成的执行文件消除了这些调试信息,可用来作为成品发布 Debug只在debug状态下会输出,Trace在 ...

  2. OpenJudge计算概论-鸡尾酒疗法

    /*===================================== 鸡尾酒疗法 总时间限制: 1000ms 内存限制: 65536kB 描述 鸡尾酒疗法,原指“高效抗逆转录病毒治疗”(HA ...

  3. TKinter布局之pack

    pack布局非常简单,不用做过多的设置,直接使用一个 pack 函数就可以了. 1.我们使用 pack 函数的时候,默认先使用的放到上面,然 后 依次向下排,它会给我们的组件一个自认为合适的位置 和大 ...

  4. Jfinal基础学习(一)

    我负责的项目都是Jfinal的了,有必要写一些学习知识,记录下来. 1.PropKit.use("config.txt", "UTF-8"); PropKit ...

  5. CK方程

    上文中,“到时间n为止进入任意一个特定的状态集合”应理解为“在时间n及之前进入的都算”. 只要进入了该状态集合,之后是否离开已经不重要了.这个可类比于“先赢若干局”的赌徒问题:即使在赢得若干局后继续赌 ...

  6. html body标签的几个属性 禁用鼠标右键,禁用鼠标选中文字等

    <body oncontextmenu='return false' ondragstart='return false' onselectstart ='return false' onsel ...

  7. find_in_set mysql

    有个文章表里面有个type字段,他存储的是文章类型,有 1头条,2推荐,3热点,4图文 .....11,12,13等等 现在有篇文章他既是 头条,又是热点,还是图文, type中以 1,3,4的格式存 ...

  8. Mysql中is marked as crashed and should be repaired问题的处理

    问题描述:浏览页面提示:.bbs[Table]threads' is marked as crashed and should be repaired 产生原因:表在查询或其它系统操作下损坏. 解决方 ...

  9. flash bulider 无法启动

    更新airsdk后无法启动,google下http://www.25kx.com/art/1826181有n种方法,怀疑是.metadata目录原因,可能是旧版的airsdk和新版的airsdk在.m ...

  10. Java-螺旋方阵

    用Java实现螺旋方阵 螺旋方阵:是指呈螺旋状的矩阵. 具体实现如下: public void screwMatrix() { System.out.print("请输入数字:") ...