`

Structs1.2的HelloWorld

阅读更多

Structs1.2的HelloWorld

一、工作原理

 

1.初始化 :struts框架的总控制器ActionServlet一个Servlet ,它在web.xml中配置成自动启动的Servlet ,在启动时 总控制器会读取配置文件(struts-config.xml)的配置信息 ,为struts中不同的模块初始化相应的对象


2.发送请求 :用户提交表单或通过URL向WEB服务器提交请求,请求的数据用HTTP协议传给web服务器。
3.form填充 :struts的总控制器ActionServlet在用户提交请求时将数据放到 对应的form对象中的成员变量 中。
4.派发请求 :控制器根据配置信息对象ActionConfig 将请求派发到具体的Action ,对应的formBean一并传给这个Action中的excute()方法。
5.处理业务 :Action一般只包含一个excute()方法 ,它负责执行相应的业务逻辑(调用其它的业务模块)完毕后返回一个ActionForward对象 。服务器通过ActionForward对象进行转发工作。
6.返回响应 :Action将业务处理的不同结果返回一个目标响应对象 给总控制器。
 7.查找响应 :总控制器根据Action处理业务返回的目标响应对象 ,找到对应的资源对象,一般情况下为jsp页面。

 8.响应用户 :目标响应对象将结果传递给资源对象,将结果展现给用户。

二、例子中的场景

一个简单的登录流程,用户输入用户名和密码后,跳转到主界面main.jsp
 index.jsp-->ActionServlet--->IndexForm--->IndexAction-->ActionServlet-->main.jsp


三、代码实现

使用环境:MyEclipse 8

1.搭建struts1.2环境,包括 newweb project和MyEclipse-->Project Capabiliteis-->Add Struts Capabilities



2.New Action,Form&JSP



3.New Forward


4.编写代码

5.部署到Tomcat

6.运行调试


四、解释

(一)web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee   http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
  <servlet>
    <servlet-name>action</servlet-name>
    <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
    <init-param>
      <param-name>config</param-name>
      <param-value>/WEB-INF/struts-config.xml</param-value>
    </init-param>
    <init-param>
      <param-name>debug</param-name>
      <param-value>3</param-value>
    </init-param>
    <init-param>
      <param-name>detail</param-name>
      <param-value>3</param-value>
    </init-param>
    <load-on-startup>0</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>action</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

   web中注册了:

1.自动启动的Struts总控制器ActionServlet

2.struts的配置文件struts-config.xml

3.一些初始化参数

4.ActionServlet处理的资源:*.do

(二)struts-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">

<struts-config>
  <data-sources />
  <form-beans >
    <form-bean name="indexForm" type="com.linys.struts.form.IndexForm" />

  </form-beans>

  <global-exceptions />
  <global-forwards />
  <action-mappings >
    <action
      attribute="indexForm"
      input="/index.jsp"
      name="indexForm"
      path="/index"
      scope="request"
      type="com.linys.struts.action.IndexAction">
      <forward
        name="success"
        path="/main.jsp"
        redirect="true" />
    </action>

  </action-mappings>

  <message-resources parameter="com.linys.struts.ApplicationResources" />
</struts-config>

 struts-config.xml中配置了由struts管理的资源:

1.form:

 <form-beans >
    <form-bean name="indexForm" type="com.linys.struts.form.IndexForm" />

  </form-beans>

 2.action:

 <action-mappings >
    <action
      attribute="indexForm"
      input="/index.jsp"
      name="indexForm"
      path="/index"
      scope="request"
      type="com.linys.struts.action.IndexAction">
      <forward
        name="success"
        path="/main.jsp"
        redirect="true" />
    </action>

  </action-mappings>

 

 3.资源文件ApplicationResources.properties

<message-resources parameter="com.linys.struts.ApplicationResources" />
 

 

  • 大小: 64.3 KB
  • 大小: 73.3 KB
  • 大小: 82.6 KB
  • 大小: 78 KB
  • 大小: 58.8 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics