Struts入门第一个小示例程序

                                第一章 Struts入门

一 课程目标
1. 掌握Struts2的环境搭建
2. 掌握Struts2的运行机制
3. 使用Struts2实现Hello World实例程序

二 先操作,通过操作后,再来理解Struts2框架。实现Hello World实例程序

1.环境搭建
- 打开MyEclipse,创建一个Web Project。
- 在MyEclipse->window->preference->MyEclipse->Server->Tomcat设置Tomcat的位置
- 在MyEclipse->window->preference->java->Installed JREs设置JDK的位置
- 下载Struts2,然后解压。解压后打开app目录,解压里面的struts2-blank.war文件。解压以后把解压后的示例项目中的WEB-INF->classes目录下的struts.xml文件拷贝到自己项目中的src下。把struts2加压后的lib目录里面的jar包拷贝到自己项目中的WEB-INF->lib目录下。
- 打开Web.xml,配置Struts2。配置代码参考Struts2中的示例程序的web.xml

2.代码实现

  • 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" id="WebApp_ID" version="3.0">
    <filter>
        <filter-name>struts</filter-name>
        <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>struts</filter-name>
        <url-pattern>*.action</url-pattern>
    </filter-mapping>

  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>
  • struts.xml
<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
        "http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>

    <package name="default" extends="struts-default" namespace="/">
        <action name="hello" class="action.TestActionSupport">
            <result>/hello.jsp</result>
        </action>       
    </package>
</struts>
  • hello.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">

    <title>My JSP 'index.jsp' starting page</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
  </head>

  <body>
        <!-- 测试ActionSupport的用户 -->
        <form action="test1.action" method="post">
            用户名:<input type="text" name="username" ><br>
            密码:<input type="text" name="password" ><br>
            <input type="submit" value="提交">            
        </form>
  </body>
</html>

3.配置常量为开发模式,在开发过程中修改struts.xml不需要重启服务器。可以立即反馈结果。

<constant name="struts.devMode" value="true"></constant>

4.关联struts2源代码和javadoc文档。关联源代码在我们使用Struts2中的类时,可以随时按ctrl+单击就可以查看源代码。关联javadoc文档,在开发中可以直接在myeclipse中查看API文档。

5.Struts2的运行机制。

  • 浏览器(客户端)发送一个请求给服务器。服务器收到浏览器的请求以后,会到webapps目录里面去寻找是否包含该应用。如果有这个应用,就会去读取该应用下的web.xml,看该请求是否有Servlet或者Filter或者监听器来处理。
  • 由于我们之前在web.xml中配置过struts2,是一个filter。所以会把请求交给这个struts2中的filter来处理。
  • struts2中的filter的处理方式是,第一步去读取struts.xml文件,查询在请求URL中,有没有匹配的package,如果有就查询package中有没有处理该请求的action。如果有就交给这个action处理。
  • 处理的结果就是result。把result标签中的页面返回给浏览器。以上就是struts2的大概运行机制。
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值