随笔-25  评论-1702  文章-0  trackbacks-0

Struts作为MVC 2的Web框架,自推出以来不断受到开发者的追捧,得到用广泛的应用。作为最成功的Web框架,Struts自然拥有众多的优点:

  • MVC 2模型的使用
  • 功能齐全的标志库(Tag Library)
  • 开放源代码

但是,所谓“金无赤金,人无完人”,Struts自身也有不少的缺点:

  • 需要编写的代码过多,容易引起“类爆炸”
  • 单元测试困难

这些缺点随着Web的发展越来越明显。这就促生了Struts 2.0,它的诞生能很好的解决上述问题。 好啦,废话就不多说了,现在就让我们感受一下的Struts 2.0的魅力吧。

  1. 搭建开发和运行环境
    1. 到Apache下载Struts 2.0包

    2. 打开Eclipse 3.2新建Web工程

      点击菜单File\New\Project,出现如图1所示对话框
      图1 新建工程对话框
      图1 新建工程对话框
      选择Web\Dynamic Web Project,点击“Next”,出现图2对话框

      图2 新建动态Web工程对话框
      图2 新建动态Web工程对话框
      在“Project Name”中键入Struts2_HelloWorld,点击“New”,出现以下对话框

      图3 新建服务器运行时对话框
      图3 新建服务器运行时对话框
      选择“Apache\Apache Tomat v5.5”,点击“Next”,出现以下对话框

      图4新建服务器运行时对话框
      图4新建服务器运行时对话框
      点击“Finish”,关闭对话框。

    3. 将Struts 2.0 lib下的jar文件加到工程的构建路径(build path)

      图5 Struts 2.0的lib目录
      图5 Struts 2.0的lib目录
      按ctr+a全选,复制,再转到Eclipse窗口,在“Project Explorer”子窗口中选中Struts2_HelloWorld\WebContent\WEB-INF\lib,然后粘贴。经过Eclipse自动刷新“Project Explorer”子窗口,刚才所粘贴的jar文件应该会出现在Struts2_HelloWorld\Java Resources: src\Libraries\Web App Libraries下,如图6所示:

      图6 Project Explorer子窗口
      图6 Project Explorer子窗口

    4. 打开web.xml文件,将其修改为以下代码:
      <?xml version="1.0" encoding="ISO-8859-1"?>
      <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
      <web-app>
          
      <display-name>Struts 2.0 Hello World</display-name>
          
      <filter>
              
      <filter-name>struts2</filter-name>        <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
          
      </filter>
          
      <filter-mapping>
              
      <filter-name>struts2</filter-name>
              
      <url-pattern>/*</url-pattern>
          
      </filter-mapping>
          
      <welcome-file-list>
              
      <welcome-file>index.html</welcome-file>
          
      </welcome-file-list>
      </web-app>

    5. 新建struts.xml文件

      右键点击,Struts2_HelloWorld\Java Resources: src,出现如图7所示菜单
      图7 新建Other菜单
      图7 新建Other菜单
      点击“Other”,出现新建对话框,如图8所示

      图8 新建对话框
      图8 新建对话框
      点击“Next”,出现新建文件对话框,如图9所示

      图9 新建文件对话框
      图9 新建文件对话框
      在“File name”中键入sturts.xml,点击“Finish”,然后将struts.xml的内容修改为:

      <!DOCTYPE struts PUBLIC
              "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
              "http://struts.apache.org/dtds/struts-2.0.dtd"
      >
      <struts>
          
      <include file="struts-default.xml"/>
      </struts>

    6. 新建index.html文件

      右键点击Struts2_HelloWorld\WebContent,出现如图10所示的菜单
      图10 新建Other菜单
      图10 新建Other菜单
      点击“Other”,出现新建对话框,如图11所示

      图11 新建对话框
      图11 新建对话框
      选择Web\HTML,点击“Next”出现如图12所示的对话框

      图12 新建HTML页面对话框
      图12 新建HTML页面对话框
      在“File Name”中键入index.html,点击“Next”,出现如图13所示的对话框

      图13 模板选择对话框
      图13 模板选择对话框
      点击“Finish”,将index.html的内容修改为以下内容:

      <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
      <html>
      <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      <title>Hello World</title>
      </head>
      <body>
      <h3>Hello World!</h3>
      </body>
      </html>

    7. 将应用程序打包到tomcat上

      右键点击Struts_HelloWorld,出现如图14所示的菜单
      图14 工程菜单
      图14 工程菜单
      点击“Export\WAR file”,出现如图15所示的对话框

      图15 输出对话框
      图15 输出对话框
      选择“Web\WAR file”,点击“Next”,出现如图16所示的对话框

      图16 输出路径对话框
      图16 输出路径对话框
      输入war文件的路径(如%tomcat%\webapps\Struts2_HelloWorld.war),点击“Finish”关闭对话框。

    8. 启动tomcat,运行应用程序

      打开你的Internet Explorer,键入http://localhost:8080/Struts2_HelloWorld/,窗口输出如图17所示
      图17 Hello World窗口
      图17 Hello World窗口

  2. 第一个Struts 2.0应用程序——Hello World
    1. 新建类包(package)

      右键点击Struts2_HelloWorld\Java Resources: src,出现如图18所示菜单
      图18 新建菜单
      图18 新建菜单"
      点击“New\Package”,出现如图19所示对话框

      图19新建Java类包对话框
      图19新建Java类包对话框
      在“Name”键入tutorial,点击“Finish”关闭对话框。

    2. 新建HelloWorld.java文件

      右键点击Struts2_HelloWorld\Java Resources: src\tutorial,出现如图20所示菜单
      图20 新建菜单
      图20 新建菜单
      点击“New\Class”,出现如图21所示对话框

      图21 新建Java类对话框
      图21 新建Java类对话框
      在“Name”中键入HelloWorld,在“Superclass”中键入com.opensymphony.xwork2.ActionSupport,点击“Finish”关闭对话框。将HelloWorld.java的内容修改为:

      package tutorial;

      import com.opensymphony.xwork2.ActionSupport;

      public class HelloWorld extends ActionSupport {
          
      private String name;
          
          
      public String getName() {
              
      return name;
          }

          
          
      public void setName(String name) {
              
      this.name = name;
          }

          
          
      public String execute() {
              name 
      = "Hello, " + name + "!"
              
      return SUCCESS;
          }

      }

    3. 在struts.xml中添加action映射(mapping)
      <!DOCTYPE struts PUBLIC
              "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
              "http://struts.apache.org/dtds/struts-2.0.dtd"
      >
      <struts>
          
      <include file="struts-default.xml"/>
          
      <package name="tutorial" extends="struts-default">
              
      <action name="HelloWorld" class="tutorial.HelloWorld">
                  
      <result>HelloWorld.jsp</result>
              
      </action>
          
      </package>
      </struts>

    4. 新建SayHello.jsp

      参考“新建index.html文件”步骤,弹出如图22所示对话框
      图22 新建对话框
      图22 新建对话框
      点击“Next”, 进入下一步,如图23所示

      图23 新建JSP对话框
      图23 新建JSP对话框
      在“File name”键入SayHello.jsp,点击“Next”进入下一步,如图24所示

      图24 模板选择对话框
      图24 模板选择对话框
      点击“Finish”关闭对话框,并将SayHello.jsp的内容修改为:

      <%@ page contentType="text/html; charset=UTF-8" %>
      <%@ taglib prefix="s" uri="/struts-tags" %>
      <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
      <html>
          
      <head>
              
      <title>Say Hello</title>
          
      </head>
          
      <body>
              
      <h3>Say "Hello" to: </h3>
              
      <s:form action="HelloWorld">
                  Name: 
      <s:textfield name="name" />
                  
      <s:submit />
              
      </s:form>
          
      </body>
      </html>

    5. 新建HelloWorld.jsp(请参考上一步),HelloWorld.jsp的内容为:
      <%@ page contentType="text/html; charset=UTF-8" %>
      <%@ taglib prefix="s" uri="/struts-tags" %>
      <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
      <html>
          
      <head>
              
      <title>Hello</title>
          
      </head>
          
      <body>
              
      <h3><s:property value="name" /></h3>
          
      </body>
      </html>

    6. 重新打包发布应用程序

      先停止tomcat, 再将tomcat里webapps下的Struts2_HelloWorld.war和Struts2_HelloWorld文件夹删除,参照“将应用程序打包到tomcat上”重新发布应用程序。

    7. 启动tomcat,运行测试

      打开Internet Explorer,键入http://localhost:8080/Struts2_HelloWorld/SayHello.jsp,窗口输出如图25所示
      图25 SayHello.jsp
      图25 SayHello.jsp
      在“Name”键入字符串(如World),点击Submit,转到HelloWorld.jsp页面,如图26所示

      图26 HelloWorld.jsp
      图26 HelloWorld.jsp

  3. 单元测试Hello World

    在文章开始的时候提及,单元测试困难是Struts一大缺点。现在让我们在体验一下,在Struts 2.0中是如何进行测试的。

    1. 新建JUnit单元测试

      右键点击Struts2_HelloWorld\Java Resources: src\tutorial,弹出如图27所示对话框
      图27 新建菜单
      图27 新建菜单
      点击“Next\Other”

      图28 新建对话框
      图28 新建对话框
      选择“Java\JUnit\JUnit Test Case”,点击“Next”

      图29 新建JUnit 测试用例对话框
      图29 新建JUnit 测试用例对话框
      选择“New JUnit 4 test”,在“Name”中键入HelloWorldTest,在“Class under test”键入tutorial.HelloWorld,点击“Next”

      图30 选择方法对话框
      图30 选择方法对话框
      选中HelloWorld\execute方法,点击Finish。如果生成的HelloWorldTest.java文件的图标(Icon)出现红色交叉标志,请进行以下步骤添加JUnit 4的jar包。

      右键点击Struts2_HelloWorld,出现如图所示菜单。 图31 新建菜单
      图31 新建菜单
      点击“Build Path\Add Libararis”,弹出图32对话框

      图32 添加库对话框
      图32 添加库对话框
      选中“JUnit”,点击“Next”

      图33 选择版本对话框
      图33 选择版本对话框
      选择“JUnit 4”,点击“Finish”关闭对话框,并将HelloWorldTest.java的内容修改为:

      package tutorial;

      import static org.junit.Assert.assertTrue;

      import org.junit.Test;

      import com.opensymphony.xwork2.ActionSupport;

      public class HelloWorldTest {

          @Test
          
      public void testExecute() {
              HelloWorld hello 
      = new HelloWorld();
              hello.setName(
      "World");
              String result 
      = hello.execute();
              
              assertTrue(
      "Expected a success result!", ActionSupport.SUCCESS.equals(result));
              
              
      final String msg = "Hello, World!";
              assertTrue(
      "Expected the default message!", msg.equals(hello.getName()));
          }


      }

    2. 运行单元测试

      右键点击Struts2_HelloWorld\Java Resources: src\tutorial\HelloWorldTest.java,弹出如图34所示菜单
      图34 运行为菜单
      图34 运行为菜单
      点击“Run As\JUnit Test”,出现JUnit子窗口如图35所示

      图35 JUnit子窗口
      图35 JUnit子窗口
      图35的绿色矩形表示,所有单元测试通过。

  4. 总结

    上面的例子简单地演示了,Web 应用程序的基本操作,也即是,页面输入->Action处理->再输出到另外页面。Struts 2.0的简单易用、方便测试相信也会给大家留下不错的印象吧。我相信,Struts 2.0作为一个全新的Web架构,将会再次掀起Web开发的热潮。 不过,Struts 2.0还在测试中,正式版的发布还需些时日,所以文档方面可能有所欠缺。请大家继续留意我的博客,我会尽我所能为大家写更多关于Struts 2.0的文章。

posted on 2006-10-10 18:35 Max 阅读(77063) 评论(307)  编辑  收藏 所属分类: Struts 2.0系列
评论共4页: 1 2 3 4 下一页 

评论:
# re: 为Struts 2.0做好准备 2007-11-16 12:14 | maszxy
@Mike
因为你在struts.xml里给这个action定的名字是HelloWorld  回复  更多评论
  
# re: 为Struts 2.0做好准备 2007-11-16 12:23 | cricy
都要发布才能测试吗?有没有可以直接测试的?  回复  更多评论
  
# re: 为Struts 2.0做好准备 2007-11-16 16:43 | onlykwx
写得很好,支持一下!  回复  更多评论
  
# re: 为Struts 2.0做好准备 2007-11-23 11:36 | gongbing
请问,您最后发布应用程序的时候采用的是打包成war文件到tomcat里,但是如果这样的话,好像每修改一次程序就要把先前的war文件删除掉,重新打包,这样做卫冕太麻烦了,请问有没有其他的简单一点的办法?谢谢  回复  更多评论
  
# re: 为Struts 2.0做好准备 2007-11-24 17:09 | sandbox
郁闷,怎么调都调试不通 ,提交表单后就是404错误,也不知道什么原因 望赐教

另外 感觉的文章很好,能否告诉一下出处
email:flywyvern@126.com 同时希望发一份这篇文章的测试源码,不用带包,我就不信调不通
最后 谢谢:)  回复  更多评论
  
# re: 为Struts 2.0做好准备 2007-11-28 16:36 | xiao
第一个Struts 2.0应用程序——Hello World
运行没问题

当我在浏览器中
键入http://localhost:8080/Struts2_HelloWorld/SayHello.jsp
后台报异常:
org.xml.sax.SAXParseException: Element type "bean" must be declared.

不知是什么原因

我都弄了一下午都没解决问题

麻烦各位了


  回复  更多评论
  
# re: 为Struts 2.0做好准备 2007-11-30 13:52 | 不能说
是不是Tomcat5.0不能跑Struts2.0啊???  回复  更多评论
  
# re: 为Struts 2.0做好准备 2007-12-08 18:25 |
非常好!~  回复  更多评论
  
# re: 为Struts 2.0做好准备 2007-12-08 18:26 |
ddd  回复  更多评论
  
# re: 为Struts 2.0做好准备 2007-12-08 18:27 |
@月
ddd  回复  更多评论
  
# re: 为Struts 2.0做好准备 2007-12-18 14:16 | windylovesandy
我的出错,以下错误是怎么回事?
我不知道struts-tags.jar在哪来?我在lib中没有找到,应该放在哪里。希望指教以下
HTTP Status 500 -

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: File "/struts-tags" not found
org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:50)
org.apache.jasper.compiler.ErrorDispatcher.dispatch(ErrorDispatcher.java:407)
org.apache.jasper.compiler.ErrorDispatcher.jspError(ErrorDispatcher.java:114)
org.apache.jasper.compiler.TagLibraryInfoImpl.<init>(TagLibraryInfoImpl.java:159)
org.apache.jasper.compiler.Parser.parseTaglibDirective(Parser.java:423)
org.apache.jasper.compiler.Parser.parseDirective(Parser.java:492)
org.apache.jasper.compiler.Parser.parseElements(Parser.java:1552)
org.apache.jasper.compiler.Parser.parse(Parser.java:126)
org.apache.jasper.compiler.ParserController.doParse(ParserController.java:211)
org.apache.jasper.compiler.ParserController.parse(ParserController.java:100)
org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:146)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:286)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:267)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:255)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:563)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:293)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

note The full stack trace of the root cause is available in the Apache Tomcat/5.5.12 logs.  回复  更多评论
  
# re: 为Struts 2.0做好准备 2007-12-20 11:40 | 阿祥
比较好!讲得比较细!thanks (the team)  回复  更多评论
  
# re: 为Struts 2.0做好准备 2007-12-20 17:19 |
有问题说出来  回复  更多评论
  
# re: 为Struts 2.0做好准备 2007-12-29 00:38 | 凌晨风
2007-12-29 0:24:48 org.apache.struts2.components.Form evaluateExtraParamsServletRequest
警告: No configuration found for the specified action: 'login.action' in namespace: ''. Form action defaulting to 'action' attribute's literal value.
2007-12-29 0:24:48 org.apache.struts2.components.Form evaluateExtraParamsServletRequest
警告: No configuration found for the specified action: 'login.action' in namespace: ''. Form action defaulting to 'action' attribute's literal value.

出现如下错误,程序运行正常!不知道只是怎么回事?
错误发生在login.jsp页面刷新时就出现。  回复  更多评论
  
# re: 为Struts 2.0做好准备 2007-12-30 16:28 | xx
做个视频多方便,这样一张一张的截图很麻烦  回复  更多评论
  
# re: 为Struts 2.0做好准备[未登录] 2008-01-01 18:12 | aa
@新手
写action的名字  回复  更多评论
  
# re: 为Struts 2.0做好准备 2008-01-07 17:20 | C++
警告: No configuration found for the specified action: 'HelloWorld' in namespace: ''. Form action defaulting to 'action' attribute's literal value.
2008-1-7 16:11:44 org.apache.struts2.components.Form evaluateExtraParamsServletRequest
警告: No configuration found for the specified action: 'HelloWorld' in namespace: ''. Form action defaulting to 'action' attribute's literal value.


我都弄了2个星期了,晕死了!  回复  更多评论
  
# re: 为Struts 2.0做好准备 2008-01-08 15:34 | 自由化xl
挺不错的, 谢谢楼主的细心总结!  回复  更多评论
  
# re: 为Struts 2.0做好准备 2008-01-11 23:16 | bizairshop
很好的例子
北京机票推荐 http://www.newskyair.com  回复  更多评论
  
# re: 为Struts 2.0做好准备 2008-01-18 14:12 | tt
@C++
表单提交的 action name 和 struts.xml 的 action name 是否相同  回复  更多评论
  
# re: 为Struts 2.0做好准备 2008-01-20 14:04 | 陈龙
是个好文章  回复  更多评论
  
# re: 为Struts 2.0做好准备 2008-01-23 16:27 | 漂泊
警告: No configuration found for the specified action: 'HelloWorld' in namespace
: ''. Form action defaulting to 'action' attribute's literal value.
2008-1-23 16:21:56 org.apache.struts2.components.Form evaluateExtraParamsServlet
Request
警告: No configuration found for the specified action: 'HelloWorld' in namespace
: ''. Form action defaulting to 'action' attribute's literal value.

什麽原因?

  回复  更多评论
  
# re: 为Struts 2.0做好准备 2008-01-23 17:04 | C++
@tt
我的代码全是照着复制、粘贴的,不会有错,我也反复检查过N!遍。郁闷。谢谢tt大侠了  回复  更多评论
  
# re: 为Struts 2.0做好准备 2008-01-24 17:16 |
junit.framework.AssertionFailedError: /WEB-INF/web.xmlが見つかりません
at servletunit.struts.MockStrutsTestCase.getActionServlet(MockStrutsTestCase.java:344)
at servletunit.struts.MockStrutsTestCase.tearDown(MockStrutsTestCase.java:130)
at junit.framework.TestCase.runBare(TestCase.java:130)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:128)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)

我用MockStrutsTestCase框架做的一个简单测试,哪位老兄帮忙看看是什么问题啊?

  回复  更多评论
  
# re: 为Struts 2.0做好准备 2008-01-27 13:38 | jinruihua
@zero
可能是你的.jsp页面的action名与struts中的action不一致导致。
maybe help.  回复  更多评论
  
# re: 为Struts 2.0做好准备 2008-01-28 16:38 |
是一致的,被测试的代码都是成品了,应该不是代码的问题  回复  更多评论
  
# re: 为Struts 2.0做好准备 2008-02-17 18:46 | 111
@jarwang
那是因为你的表单的编码还是ISO8859-1。  回复  更多评论
  
# re: 为Struts 2.0做好准备 2008-02-21 14:23 | laibin
谢谢!  回复  更多评论
  
# re: 为Struts 2.0做好准备 2008-02-22 09:12 | GG_wg
例子不错,我测试中文提交也没有问题(firefox)  回复  更多评论
  
# re: 为Struts 2.0做好准备[未登录] 2008-02-22 17:10 | lee
提交表单后显示下如错误:
警告: No configuration found for the specified action: 'HelloWorld' in namespace: ''. Form action defaulting to 'action' attribute's literal value.
2008-2-22 17:08:55 org.apache.struts2.components.Form evaluateExtraParamsServletRequest  回复  更多评论
  
# re: 为Struts 2.0做好准备 2008-03-10 11:35 | yuan29346
多谢!
我是在JBuider上工作的,按照你的提示,呵呵,顺利搞定到JB中了。
现在刚准备学这个struts2,现在有很多疑问啊!很郁闷,我们公司不用这个。
比如:
<s:form action="Login">
<s:text name="username"/><s:textfield key="username"/>
<s:text name="password"/><s:password key="password" />
<s:submit/>
</s:form>
这个会自动输出table,和我想要的不一样!我相知道怎么样才能控制输出的HTML标签?
  回复  更多评论
  
# re: 为Struts 2.0做好准备 2008-03-13 16:45 | zhengtu
太牛了;
照做了;
没问题;
讲的很细;
谢谢.  回复  更多评论
  
# re: 为Struts 2.0做好准备 2008-03-18 09:20 | 小魏
照做的,完全正确,在这个过程中遇到许多问题,解决之后发现自己学习到很多:)
谢谢:)  回复  更多评论
  
# re: 为Struts 2.0做好准备 2008-03-18 12:33 | 木风口子
我第一次运行HelloWorld没有问题,但后来又不行了,一直报错:
HTTP Status 404 - /projects/struts/Struts2_HelloWorld

--------------------------------------------------------------------------------

type Status report

message /projects/struts/Struts2_HelloWorld

description The requested resource (/projects/struts/Struts2_HelloWorld) is not available.

导出的WAR文件在ROOT/projects/struts/下
本机配置:
jdk1.5,
eclipse为最新europe
tomcat5.0.30
没有加载包,因为我已经有lomboz struts包
不会是没加包的原因,
请赐教!多谢!  回复  更多评论
  
# re: 为Struts 2.0做好准备 2008-03-25 17:21 | visitor
@凌晨风
也困绕我半天。。。
后来照另外的教程做出一个可以跑的,再Beyond compare比较一下,发现是建立struts.xml时错了,名字不对,浪费好几个小时  回复  更多评论
  
# re: 为Struts 2.0做好准备[未登录] 2008-03-25 22:37 | 绿茶
你好..谢谢..我觉得就是tomcat最好是5.x或6.X..JDK1.5以上吧..我刚开始也是按你说的那样去测试..在以前的版本上测试..都不行..后来下了个新版..就可以了..还是就是...struts.xml一定要放在classess目录下...  回复  更多评论
  
# re: 为Struts 2.0做好准备 2008-03-28 20:01 | pakinsen
我按照你的方法做了,第一部分是成功的,但是第二步添加了那几个JSP文件后,一打开TOMCAT就说找不到SayHello.jsp。这是为什么呢?我反复看了你的文章,没有地方不一致啊。  回复  更多评论
  
# re: 为Struts 2.0做好准备 2008-04-07 10:39 | 小新手
我用的是myeclipse5.0 tomcat5.5 struts2.0.11.1的包,按照上面的步骤连第一部分都不能正常运行,如果不按照上面步骤修改web.xml内容的话第一步反而能实现,但第二部分仍然不能实现,为什么呢。。。。  回复  更多评论
  
# re: 为Struts 2.0做好准备 2008-04-14 19:04 | 极度郁闷
按照步骤配置了,但SayHello.jsp提交失败,后台总是报错“no configuration found for the specified action...”,试了无数次,google了半天,还是不行,最后终于极度郁闷的发现,配置文件名称是“sturts.xml”,这个名称是从你文章里拷过去的,有一个字母顺序错了,狂晕!!!浪费了近3个小时,555555555555555.............  回复  更多评论
  
# re: 为Struts 2.0做好准备[未登录] 2008-04-21 18:00 | lf
以前一直都用的struts1.X, 学习下!
设置一切OK,绿色顺利出来
不错,支持  回复  更多评论
  
# re: 为Struts 2.0做好准备 2008-04-21 20:49 | sidekick
No configuration found for the specified action: 'HelloWorld' in namespace

是因為 copy 'sturts.xml' 的結果, 請更正文章中 sturts.xml 為 struts.xml

sturts.xml --> struts.xml  回复  更多评论
  
# re: 为Struts 2.0做好准备 2008-04-23 17:15 | 懒人
和1.0差在哪?不能出了新技术就什么地方都用吧?  回复  更多评论
  
# re: 为Struts 2.0做好准备 2008-04-29 17:26 | snowwolf76
写得太棒了。
我用的struts-2.0.11.1+jdk1.6.0_05+tomcat 5.5.26.
关于404的错误,我是先把所有struts2的jar文件粘贴到lib目录下,然后把struts2-spring-plugin-2.xxx.jar,
struts2-plexus-plugin-2.xxx.jar,
struts2-jsf-plugin-2.xxx.jar删除,
还对照截图删除了antlr-2xxx.jar, commons开头的几个jar(也许不是他们的原因),因为我是在eclipse里面debug工程看到了些异常。  回复  更多评论
  
# re: 为Struts 2.0做好准备 2008-04-30 11:44 | goldgood
在此页面中的,java源程序的树形结构,是如何实行的?  回复  更多评论
  
# re: 为Struts 2.0做好准备 2008-05-04 14:30 | goldgood
问题解决。  回复  更多评论
  
# re: 为Struts 2.0做好准备 2008-05-07 11:26 | 传说中的???
关于第1次启动时就报HTTP404错的解决方法
错误信息:
2008-5-7 11:17:24 org.apache.catalina.core.StandardContext start
严重: Error filterStart
2008-5-7 11:17:24 org.apache.catalina.core.StandardContext start
严重: Context [/Struts2_Test] startup failed due to previous errors

解决方法:
删除WebContent\WEB-INF\lib\struts2-spring-plugin-2.x.x.jar

这样启动就不会报错了  回复  更多评论
  
# re: 为Struts 2.0做好准备[未登录] 2008-05-09 17:35 | miracle
@小燕子
创建工程的时候,选中show all wizards!  回复  更多评论
  
# re: 为Struts 2.0做好准备 2008-05-13 01:39 | tomate
在我的环境下是因为"struts2-jsf-plugin-2.0.11.1.jar"这个jar包引起的404问题.

>关于第1次启动时就报HTTP404错的解决方法
>错误信息:
>2008-5-7 11:17:24 org.apache.catalina.core.StandardContext start
>严重: Error filterStart
>2008-5-7 11:17:24 org.apache.catalina.core.StandardContext start
>严重: Context [/Struts2_Test] startup failed due to previous errors

>解决方法:
>删除WebContent\WEB-INF\lib\struts2-spring-plugin-2.x.x.jar   回复  更多评论
  
# re: 为Struts 2.0做好准备 2008-05-13 13:58 | hover
和我报的错一样。不知道为什么,如果把里面的过滤器去掉就可以了,但这样就失去了struts2.0的意义了。  回复  更多评论
  
# re: 为Struts 2.0做好准备 2008-05-15 14:14 | hover
我觉得建立动态的java工程(dynamic web project)不太方便,比如我每次修改完一点程序:.java,.xml,.jsp,.html之类的文件后,都必须得停止tomcat,重新打包发布应用,再启动tomcat后才会看到修改后的页面,否则的话显示的还是以前的页面或干脆找不着路径,不知道各位有没有遇见过这种情况。
说明:动态的应用和普通的应用都要求tomcat版本必须是5.5版本(含)以上的。
刚开始用的5.1,怎么也不行,后来改成5.5就可以了,  回复  更多评论
  
# re: 为Struts 2.0做好准备 2008-05-16 22:03 | 迎风十八刀
受益非浅 以后常来逛逛  回复  更多评论
  
# re: 为Struts 2.0做好准备[未登录] 2008-05-28 12:25 | 哈哈
报404错误的
看一下web.xml中是index.jsp还是index.html
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>  回复  更多评论
  
# re: 国际机票预定中心010-65946500 2008-05-29 17:16 | 李瑞
北京华夏航空服务公司致力于从事国际、国内特价机票、头等公务特价、留学生机票、新移民机票、团体特价机票代理业务等......
常年为各大公司企事业单位免费订票,送票,成为我们的签约客户,享受最低折扣待遇!
我们的服务理念:
服务第一 !价位最低!重承诺!守信用!让您足不出户,就能享受最完善的服务,最低廉的价格!
公司网站:www.365piaowu.com www.1688jp.com www.v88jp.com www.beijingpiaowu.com
订票热线:010-65946500 010-65921465
24时热线:13811004039 15001025358  回复  更多评论
  
# re: 为Struts 2.0做好准备 2008-05-29 23:56 | ake
太感谢了。  回复  更多评论
  
# re: 为Struts 2.0做好准备 2008-06-03 15:49 | ysaas
好文章!
详细,易懂.
本人就是加包的时候多加了些包,花了些时间排错.其实不加入其它框架的情况,加截图中的五个包就够了,否则要加入其它框架的包  回复  更多评论
  
# re: 为Struts 2.0做好准备 2008-06-08 21:35 | jiangjkd
第一个Struts 2.0应用程序——Hello World
运行没问题

当我在浏览器中
键入http://localhost:8080/Struts2_HelloWorld/SayHello.jsp
后台报异常:
org.xml.sax.SAXParseException: Element type "bean" must be declared.

不知是什么原因

我都弄了一下午都没解决问题

麻烦各位了

怎么还没解决呢,我也碰到了,搞不到,郁闷.........  回复  更多评论
  
# re: 为Struts 2.0做好准备[未登录] 2008-06-10 15:06 | Lee
为什么我按照你说的做,出现404错误啊!  回复  更多评论
  
# re: 为Struts 2.0做好准备 2008-06-17 12:33 | WellBeing
哎呀,今天搞了一个上午总算搞出来了,工程建了好几遍,真不容易。

说实话,已经挺详细了,8过,有些地方还是不太细,也可能是Eclipse版本不同的缘故吧。另外,添加jar文件的那个地方,确实是不能Ctrl+A全选复制粘贴,就是因为这个,一直是404错误。

后来,按照 台风过境 的做法,只添加这个几个文件:
• struts2-core.jar
• xwork.jar
• ognl.jar
• freemarker.jar
• commons-logging.jar
就搞定了

我的环境是这样的:
Eclipse3.3
Tomcat6.0.16
Struts2.0.11.1

希望对没有成功的朋友有所帮助。  回复  更多评论
  
# re: 为Struts 2.0做好准备 2008-06-22 09:05 | Leyou
受益匪浅啊

用MyEclipse 6.5 的写起来,应该快多了……  回复  更多评论
  
# re: 为Struts 2.0做好准备[未登录] 2008-06-27 10:26 | YY
很好,很强大!  回复  更多评论
  
# re: 为Struts 2.0做好准备 2008-07-01 11:29 | everyman1978
学习中,谢谢楼主。  回复  更多评论
  
# re: 为Struts 2.0做好准备 2008-07-08 22:22 | 邱瑞
非常感谢。至于404问题,还是这位留言给解决的 非常感谢,同时也愿意在学习过程中将所学乐趣与大家分享
# re: 为Struts 2.0做好准备[未登录] 2008-05-28 12:25 | 哈哈

报404错误的
看一下web.xml中是index.jsp还是index.html
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>  回复  更多评论
  
# re: 为Struts 2.0做好准备[未登录] 2008-07-10 18:07 | gg
我照着做了,反复配置了许多遍,终于成功了!!!  回复  更多评论
  
# re: 为Struts 2.0做好准备 2008-07-23 20:27 | tinsoldier
运行这一页的第二个例子,在Sayhello.jsp页面点submit,每次都出现HTTP404错误。
描述:The requested resource (/Struts2_HelloWorld/HelloWorld) is not available.
为什么啊?  回复  更多评论
  
# re: 为Struts 2.0做好准备[未登录] 2008-07-26 10:08 | hfbupt
我一开始也是404错误,后来发现是自己把struts.xml放在了tutorial包里面,导致应用找不到struts.xml中对应的配置,所以出错。  回复  更多评论
  
# re: 为Struts 2.0做好准备 2008-07-28 11:59 | 狮子
@WellBeing

你那个是版本 不同了,肯定包少了
  回复  更多评论
  
# re: 为Struts 2.0做好准备 2008-07-31 10:42 | 风の稻草
你们不觉得每次打成war包很麻烦吗,还要重启tomcat,不只这样,我还有把tomcat/webapps下的项目文件夹删掉。不知道谁有更快更好的方法。  回复  更多评论
  
# re: 为Struts 2.0做好准备 2008-08-13 14:15 | しゅうさん
JDK 1.6.0_03
Eclipse3.3
Tomcat6.0.14
不知道是为什么用Struts2.0.11.1报错
而用Struts2.0.6可以  回复  更多评论
  
# re: 为Struts 2.0做好准备 2008-08-15 16:38 | krthur
广大报错404的朋友:
sturts.xml这个配置文件一定要放在web-inf的classes目录下,也就是你新建struts.xml文件是在src建的,而不是在web-inf下面建...
困扰一下午的 resource not avalible终于好了....激动....

  回复  更多评论
  
# re: 为Struts 2.0做好准备[未登录] 2008-08-27 17:45 | henry1451
sturts.xml这个配置文件放在src目录下就可以了,工程启动时,会自动拷贝到classes目录下的.  回复  更多评论
  
# re: 为Struts 2.0做好准备 2008-09-02 11:45 | wangxiaoyu
This release of Apache Tomcat was packaged to run on J2SE 5.0
or later. It can be run on earlier JVMs by downloading and
installing a compatibility package from the Apache Tomcat
binary download page.
机器上面装了两个jdk 一个1.4.7一个1.6.7!
tomcat:5.0.23
环境变量:配置的是1.6.7的
各位大侠 怎么回是???  回复  更多评论
  
# re: 为Struts 2.0做好准备 2008-09-10 16:47 | dinner1984
严重: Error filterStart
2007-9-13 23:06:10 org.apache.catalina.core.StandardContext start
严重: Context [/Struts2_HelloWorld] startup failed due to previous errors
====================
我也出现这个问题,我是这么解决的,在lib文件夹里面仅仅包含五个基本包。其他的包都删除了。
这五个基本包是:commons-logging-1.0.4.jar,freemarker-2.3.8.jar,ognl-2.6.11.jar,struts2-core-2.0.10.jar,xwork-2.0.4.jar  回复  更多评论
  
# re: 为Struts 2.0做好准备 2008-09-22 18:24 | wangyh
很不错  回复  更多评论
  
# re: 为Struts 2.0做好准备 2008-10-06 13:04 | scorpio215
我用eclipse出现了一个问题
我按照JSP教程上的例子 安装了j2sdk-1_4_2_06,,数据库:MySQL 5 以及MySQL图形化管理工具MySQL Administrator1.1.4 MySQL-Front V3.2
开发工具eclipse以及相应的Lomboz插件 WEB服务器Tomcat 5.0.16
现在我新建WEB Dynamic Web Project 项目的时候 点NEXT 下面的窗口一直出不来 不知道是怎么一回事 请指点一下。。。谢谢   回复  更多评论
  
# re: 为Struts 2.0做好准备 2008-10-06 13:05 | scorpio215
如能解决 请加我QQ:232214995 或者发我邮件:scorpio215@126.com
我将万分感激  回复  更多评论
  
# re: 为Struts 2.0做好准备 2008-10-15 13:54 | asdf
先下载比较新的jdk,别按教程上所说的,一般教程上说的版本都比较老了,eclipse上的所有插件先别装,你先试下看能不能用,一步一步来配置环境  回复  更多评论
  
# re: 为Struts 2.0做好准备[未登录] 2008-10-25 11:56 | 无名
@风の稻草
把tomcat中的sevrver<Host> 修改一下
<Host
name="localhost"
appBase="工程的具体位置(像:d:/workspace)\你的工程名字"
unpackWARs="true"
autoDeploy="true"
xmlValidation="false"
xmlNamespaceAware="false">
<Context
path="/"
docBase="工程的具体位置(像:d:\\workspace)\\你的工程名字\\JspSource"
reloadable="true">
</Context>
</Host>  回复  更多评论
  
# re: 为Struts 2.0做好准备[未登录] 2008-11-08 18:36 | yang
初学者可能要注意的一个问题是:struts.xml似乎只能放在src目录下.一开始的时候我把这个文件放置在WEB-INF下和web.xml放在一起,结果第一个页面sayHello.jsp出得来,提交给action时就出错了.后台提示:警告: No configuration found for the specified action: 'HelloWorld' in namespace: ''. Form action defaulting to 'action' attribute's literal value.
IE那边返回了一个404错误页面.把struts.xml移至src下,问题就解决了.  回复  更多评论
  
# re: 为Struts 2.0做好准备 2008-11-12 01:54 |
折腾了一个晚上,刚搞定第二步骤
提醒新手注意下面几点:

1, 一个是博主 写错了 应该是 struts.xml 不是 sturts.xml
2, 如果启动tomcat的时候发现错误,有关.class文件的版本号不对,那么注意一下当前eclipse的jdk,我用jdk1.6编译出来的就有错,用1.5才可以

  回复  更多评论
  
# re: 为Struts 2.0做好准备 2008-12-01 14:53 | 上帝禁区
@哎
是的.哈哈.
补充一点.现在官网上找不到2.0.0了.所以要添加的lib有些不同.只要5个jar文件.
注意楼猪的开发环境.
还有就是run on server运行不起来.不知道为什么.只能启动tomcat插件.就是那个小猫的图标.设置war文件导出路径也不行.只能每次删了再一步步导出.麻烦.  回复  更多评论
  
# re: 为Struts 2.0做好准备 2009-01-24 14:30 | 小鼬
好文章,谢谢老师指点!  回复  更多评论
  
# re: 为Struts 2.0做好准备 2009-02-03 18:20 | 流汗的牙齿
# re: 为Struts 2.0做好准备 2009-02-16 16:57 | Andy_long
好文章啊~··  回复  更多评论
  
# re: 为Struts 2.0做好准备[未登录] 2009-02-26 20:09 | jh
tdy  回复  更多评论
  
# re: 为Struts 2.0做好准备 2009-03-10 10:07 | 龙华城
@流汗的牙齿

你引入commons-fileupload-1.2.1.jar包再试试,新版本必须引入commons-fileupload-1.2.1.jar包.要不然就会出现404错误.  回复  更多评论
  
# re: 为Struts 2.0做好准备 2009-03-11 20:32 | 2少
老师,为什么我照你的步骤一步步地做.可是打包之后都不行啊.2个都是这样.
在IE上显示的是
type Status report

message /Struts2_HelloWorld/

description The requested resource (/Struts2_HelloWorld/) is not available.

为什么啊?刚刚学struts2,什么都不懂.
  回复  更多评论
  
# re: 为Struts 2.0做好准备 2009-03-20 10:22 | fanjj_023
# re: 为Struts 2.0做好准备 2008-09-10 16:47 | dinner1984

严重: Error filterStart
2007-9-13 23:06:10 org.apache.catalina.core.StandardContext start
严重: Context [/Struts2_HelloWorld] startup failed due to previous errors
====================
我也出现这个问题,我是这么解决的,在lib文件夹里面仅仅包含五个基本包。其他的包都删除了。
这五个基本包是:commons-logging-1.0.4.jar,freemarker-2.3.8.jar,ognl-2.6.11.jar,struts2-core-2.0.10.jar,xwork-2.0.4.jar 回复 更多评论  回复  更多评论
  
# re: 为Struts 2.0做好准备 2009-03-26 18:29 | cn
严重: Could not find action or result
There is no Action mapped for namespace / and action name HelloWorld. - [unknown location]
at com.opensymphony.xwork2.DefaultActionProxy.prepare(DefaultActionProxy.java:186)
at org.apache.struts2.impl.StrutsActionProxyFactory.createActionProxy(StrutsActionProxyFactory.java:41)
at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:494) 我按照您这个步骤做的,提交就出这错。麻烦老师能帮我解决一下  回复  更多评论
  
# re: 为Struts 2.0做好准备[未登录] 2009-03-27 20:33 | jackson
There is no Action mapped for namespace / and action name HelloWorld.

这是我的struts.xml
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<include file="struts-default.xml"></include>
<package name="tutorial" namespace="/" extends="struts-default.xml">
<action name="HelloWorld" class="tutorial.HelloWorld" type="">
<result>/HelloWorld.jsp</result>
</action>
</package>
</struts>  回复  更多评论
  
# re: 为Struts 2.0做好准备[未登录] 2009-03-31 11:42 | 张峰
真的很好,希望楼主继续努力,出更多精品
  回复  更多评论
  
# re: 为Struts 2.0做好准备 2009-04-09 02:44 | hoist
lz图文并茂,讲解和清晰又不累赘。同时对IDE的操作讲解的很好,学到很多操作技巧。谢谢  回复  更多评论
  
# re: 为Struts 2.0做好准备 2009-04-24 14:09 |
在“File name”中键入sturts.xml,点击“Finish”,然后将struts.xml的内容修改为:
哥哥把struts.xml第一个写成sturts.xml.大家注意下啊  回复  更多评论
  
# re: 为Struts 2.0做好准备 2009-04-24 17:19 | 谢谢max
受益匪浅
谢谢!

还有,你很厉害。  回复  更多评论
  
# re: 为Struts 2.0做好准备[未登录] 2009-04-28 16:04 | 无名
very good  回复  更多评论
  
# re: 为Struts 2.0做好准备 2009-05-09 19:45 | aman
太好了,收藏了!  回复  更多评论
  
# re: 为Struts 2.0做好准备 2009-05-14 17:21 | miaomiao
need: commons-fileupload-1.2.1.jar  回复  更多评论
  
# re: 为Struts 2.0做好准备 2009-05-14 21:02 | aiwori
谢谢 很详细  回复  更多评论
  
# re: 为Struts 2.0做好准备[未登录] 2009-05-27 22:08 | 木头人
是的,tomcat5.5以上才支持struts2.0
  回复  更多评论
  
# re: 为Struts 2.0做好准备 2009-06-02 09:00 | ynyee
@xiao
少JAR包  回复  更多评论
  
# re: 为Struts 2.0做好准备 2009-06-26 09:02 | qqqiyv
运行struts2是要用tomcat6.0以上的  回复  更多评论
  
评论共4页: 1 2 3 4 下一页 


标题  
姓名  
主页
验证码 *  
内容(请不要发表任何与政治相关的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      
该文被作者在 2007-01-26 16:41 编辑过