Struts2 Hello World示例


当前第2页 返回上一页

如果它是struts2-core-2.5.jar,那么将web.xml中的过滤器类标记值更改为

<filter-class>

     org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter

</filter-class>

如果它是struts2-core-2.1.3.jar,那么将web.xml中的过滤器类标记值更改为

<filter-class>

     org.apache.struts2.dispatcher.FilterDispatcher

</filter-class>

自Struts 2.1.3以来,FilterDispatcher就不推荐使用了。如果您使用的是较旧的版本,则用户高于解决方案。

如果它是struts2-core-2.3.X.jar,那么将web.xml中的过滤器类标记值更改为

<filter-class> 

     org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter

</filter-class>

启用详细日志

通过在WEB-INF/classes文件夹下创建logging.properties文件,可以实现在使用Struts 2时启用完整的日志记录功能。 属性文件中需保留以下两行::
org.apache.catalina.core.ContainerBase.[Catalina].level = INFO
org.apache.catalina.core.ContainerBase.[Catalina].handlers = \
                              java.util.logging.ConsoleHandler

默认由logging.properties指定一个ConsoleHandler将日志记录按指定路线发送给stdout和FileHandler。程序运行日志的级别阈值可以使用SEVERE,WARNING,INFO,CONFIG,FINE,FINER,FINEST或者ALL。

这样,我们就准备好使用Struts 2运行我们的Hello World程序了。

执行应用程序

右键点击项目名称,接着点击“Export”>“WAR File”创建WAR文件,然后将WAR部署到Tomcat的webapps目录中。最后,启动Tomcat服务器并尝试访问URL http://localhost:8080/HelloWorldStruts2/index.jsp ,将会呈现如下图所示的结果:

输入一个“Struts2”值并提交页面,你可以看到以下页面

注意,你可以在struts.xml文件中定义一个索引作为操作,这样你可以调用索引页面 http://localhost:8080/HelloWorldStruts2/index.action 。查看下面是怎样定义索引作为操作:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
   "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
   "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.devMode" value="true" />
   <package name="helloworld" extends="struts-default">

      <action name="index">
            <result >/index.jsp</result>
      </action>

      <action name="hello" 
            class="cn.Vue5教程.struts2.HelloWorldAction" 
            method="execute">
            <result name="success">/HelloWorld.jsp</result>
      </action>

   </package>
</struts>



标签:Struts2

返回前面的内容

相关阅读 >>

Struts2 注释

Struts2 本地化国际化(i18n)

Struts2 hello world示例

Struts2 异常处理

Struts2 主题和模板

Struts2 值栈ognl

Struts2 数据库访问

Struts2 发送电子邮件

Struts2 拦截器

Struts2 表单标签

更多相关阅读请进入《Struts2》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...