第一个struts2程序

struts2第一个应用程序,首先我们得到struts的官方网站去下载struts2(http://struts.apache.org/)的包,下载包之后,现在我们开始我们得第一个struts2程序。


struts2开发的第一步骤,首先我们得在eclipse下面建立一个动态的web工程,注意建立动态工程选择,如图:


建立好之后我们可以看到,我们在需要加入struts的jar包到我们的lib文件夹下面,一般用到了如下几个包:

asm-3.3.jar
asm-commons-3.3.jar
asm-tree-3.3.jar
commons-fileupload-1.3.1.jar
commons-io-2.2.jar
commons-lang3-3.2.jar
commons-logging-1.1.3.jar
freemarker-2.3.22.jar
javassist-3.11.0.GA.jar
ognl-3.0.6.jar
struts2-core-2.3.24.jar
xwork-core-2.3.24.jar
第三步骤:我们需要在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_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>hello_struts</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list> 
  <!-- struts2的过滤器 -->
<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> 
</web-app>
 第四步:现在在我们的src下面建立一个包名,并写一个HelloWorldAction.java类,如下
package com.struts.action;
import com.opensymphony.xwork2.Action;
public class HelloWorldAction implements Action {
	//默认执行此方法
	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		System.out.println("我的第一个struts程序");
		return "success";
	}

}

第五步:在src下面建立我们的struts的配置文件struts.xml,这里可以拷贝我们的下载包中的app下面的项目中的struts.xml,拷贝之后,注释掉<struts>里面的全部内容,配置action请求时对应的处理类并放回给视图层的jsp
<?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>
<constant name="struts.devMode" value="true"></constant>
<package name="defalut" namespace="/" extends="struts-default">
	<action name="hello" class="com.struts.action.HelloWorldAction">
		<result>/hello.jsp</result>
	</action>
</package>



<!-- 
    <constant name="struts.enable.DynamicMethodInvocation" value="false" />
    <constant name="struts.devMode" value="true" />

    <package name="default" namespace="/" extends="struts-default">

        <default-action-ref name="index" />

        <global-results>
            <result name="error">/WEB-INF/jsp/error.jsp</result>
        </global-results>

        <global-exception-mappings>
            <exception-mapping exception="java.lang.Exception" result="error"/>
        </global-exception-mappings>

        <action name="index">
            <result type="redirectAction">
                <param name="actionName">HelloWorld</param>
                <param name="namespace">/example</param>
            </result>
        </action>
    </package>

    <include file="example.xml"/>
 -->
    <!-- Add packages here -->
</struts>
第六步:在WebContent下面建立hello.jsp文件,建立之后写入一点内容,然后发布项目,就可以看到如下的效果:

现在我们可以看到项目的结构图如下:

现在我们打开浏览器可以看到如下效果图:访问   http://localhost:8080/hello_struts/hello 



现在我们的第一个struts程序就完成了

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值