struts2的Hello World demo

  1.  准备jar包:
  2. 在WEB-INF目录下创建一个web.xml,具体内容如下:
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xmlns="http://java.sun.com/xml/ns/javaee"
    	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    	version="3.0">
    	<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>*.action</url-pattern>
    	</filter-mapping>
    </web-app>

    org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter是struts2定义的一个过滤器,它会拦截指定的请求,这里会拦截以.action结尾的请求,来完成请求到java后台方法的调用以及视图页面的解析工作等。

  3. 编写一个action类,struts2里面的action类并没有强制要求其继承或实现框架指定的规范类,但为了获取struts2框架的一些增强功能,通常会继承一个类:com.opensymphony.xwork2.ActionSupport,具体的代码如下:
    package com.web;
    
    import com.opensymphony.xwork2.ActionSupport;
    
    public class HelloAction extends ActionSupport {
    
    	private static final long serialVersionUID = 1L;
    
    	public String doWork() {
    		System.out.println("这里写要做的事...");
    		return "success";
    	}
    }

     

  4. struts2的过滤器被web容器初始化时,默认会加载类路径下的struts.xml,现在我们编写一个struts.xml文件
     

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
        "http://struts.apache.org/dtds/struts-2.3.dtd">
    <struts>
    	<!-- 默认包路径包含action,actions,struts,struts2的所有包都会被struts作为含有Action类的 -->
    	<!-- 路径来搜索,你可以通过设置struts.convention.package.locators属性来修改这个配置-->
    	<constant name="struts.convention.package.locators" value="web,action" />
    	<constant name="struts.enable.DynamicMethodInvocation" value="true" />
    	<package name="basePackage" extends="struts-default" namespace="/">
    		<action name="hello" class="com.web.HelloAction" method="doWork">
    			<result name="success">/WEB-INF/helloPage.jsp</result>
    		</action>
    	</package>
    </struts>

    这里我们指定一个package(包),定义了包名为basePackage,继承于struts-default.xml文件(在struts2-core这个jar里面能找到它),定义了一个命名空间为正斜杠,在包里面定义一个action元素,指定action的名字为hello,指向的类名为com.web.HelloAction,调用action的方法为doWork,方法结束后会返回一个字符串,struts2里面的action方法的返回值一般为一个字符串,规定要跳转的视图页面的逻辑名称,在result元素里面会具体规定哪个逻辑名称映射到哪个物理存在的页面,通常为一个jsp文件。

  5. 现在,一个简单的struts2的Hello World小项目就基本写完了。接下来我们把项目部署到一个web容器,这里我将它放到我本地的tomcat的webapps路径下面,tomcat对外公布8080 http访问端口,然后启动tomcat。浏览器访问:http://localhost:8080/struts2project/hello!doWork.action,将看到如下内容:

  6. 通过上面这个例子我们可以大概了解到struts2的一个的工作流程。由于文章篇幅有限,大家可参考我的gitee仓库了解更多:https://gitee.com/niefeilong/struts2project,下次再会~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值