第一章 配置struts2并创建web项目

1.  struts2是什么

        struts2是一个基于MVC设计模式的web层的框架。

2. 软件开发包介绍

     官方下载地址: http://struts.apache.org/download.cgi 版本:struts-2.3.34-all.zip

        下载压缩包并进行解压。解压后的目录如图所示

apps::包含基于struts2的实例应用。(struts项目的jar包可以直接从这里找)

docs: 包含struts2相关文档,包括struts2快速入门、struts2的文风已经API文档等

lib: 包含struts2框架和核心类库,已经srtrus2的第三方插件类库

src: 包含struts2的全部源代码

3. 第一个struts项目的创建

    3.1 创建web工程

        在eclipse中创建一个 Dynamic Web Project,选择Dynamic web module version为2.5版本(版本的不同主要区别是所支持的tomcat服务器版本不同。2.5:tomcat5及以后,  3.0: tomcat7及以后)。

    

    3.2 导入必要的jar包(struts2开发jar包)

        解压下载的struts目录找到app/struts2-blank.war包,并将其复制一份修改名称为struts2-blank.zip,解压并找到WEB-INF/lib目录,获取到strust2的项目jar包。将jar包拷贝到项目的WebContent/WEB-INF/lib目录下。

 

    3.3 编写jsp页面

       创建hello.jsp页面,在页面写hello world

<%@ page language="java" contentType="text/html; charset=UTF-8"
     pageEncoding="UTF-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
     <h1>Hello World</h1>
</body>
</html>

    3.4 编写Action代码处理逻辑

    创建HelloAction类,实现Action接口    

/**

 * 实现Action接口的类,被调用时自动实现execute方法( 类似于servlet里面的post请求执行doPost方法)
 * @author L
 */
public class HelloAction implements Action{
     public String execute() throws Exception {
          System.out.println("Hello World");
          return SUCCESS;
     }
}

    3.5 进行框架配置web.xml,struts.xml

        在/struts2_01/WebContent/WEB-INF/web.xml的<web-app>节点中加入以下配置

<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>

        在src目录下创建名为struts.xml文件,文件名不能写错。

<?xml version="1.0" encoding="UTF-8"?>
<!-- 引入struts xml规范dtd -->
 <!DOCTYPE struts PUBLIC
     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
     "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
      <package name="default" extends="struts-default" namespace="/">
      <!-- hello代表hello.action  struts中的请求默认为xxx.action  class指向对应的action类-->
              <action name="hello" class="com.lf.struts.HelloAction">
                   <!-- name="success" 对应HelloAction返回值success 为默认值  -->
                   <result name="success">hello.jsp</result>
              </action>
     </package>
</struts>

    dtd怎么引用


    3.6 运行测试

    启动项目,选择tomcat5以后的服务器版本进行运行,请求路径为http://localhost:8080/struts2_01/hello.action

    

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值