当前位置:天才代写 > tutorial > JAVA 教程 > Struts开拓指南之安装与利用

Struts开拓指南之安装与利用

2017-11-11 08:00 星期六 所属: JAVA 教程 浏览:881

副标题#e#

Struts可以运行在任何一个支持JSP1.2和Servlet2.3的WEB Container中Struts将所有的请求提交到同一其中心节制器,org.apache.struts.action.ActionServlet 类

web.xml设置

<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>
<load-on-startup>2</load-on-startup>
<servlet-mapping>

一个尺度的利用了Struts的URL样式如下:

扩展映射:http://www.my_site_name.com/mycontext/actionName.do

路径映射:http://www.my_site_name.com/mycontext/do/action_Name

<servlet-name>action</servlet-name>
<url-pattern>*.do或/do/*</url-pattern>
</servlet-mapping>

Struts运行

Struts首先在Container启动的时候挪用ActionServlet的init()要领。初始化各类设置。这些设置写在struts-config.xml文件中。

一个尺度的struts-config文件包括如下布局:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>
<data-sources /> // 界说数据源
<form-beans /> // 界说ActionForm
<global-exceptions /> // 界说全局异常
<global-forwards /> // 界说全局转向url
<action-mappings /> // 界说action
<controller /> // 设置Controller
<message-resources /> // 设置资源文件
</struts-config>

Struts由上述几部门构成。个中最主要的是Action和Form。下面简朴论述一下其处理惩罚进程。

Struts开辟指南之安装与操作

一个请求提交给ActionServlet,ActionServlet会寻找相应的Form和Action,首先将提交的request工具,映射到form中。,然后将form通报给action来举办处理惩罚。action获得form,对xml的mapping,request,response四个工具,并挪用execute()要领然后返回一个forward-url(相应视图)给ActionServlet,最终返回给客户端。


#p#副标题#e#

我们来看一个最简朴的实例

说明:实例一是最简朴的Struts措施。它仅仅利用了1个form和1个action成果是将首页输入的值通报给action,颠末判定后返回功效。假如是空则返回empty。代码如下:

input.jsp:

<form method="post" action="/example.do">
请输入值
  <input type="text" name="test"/>
  <br><br>
  <input type="submit" name="Submit" >
  <input name="reset" type="reset" >
</form>

struts-config.xml:

<struts-config>
  // 设置formbean
  <form-beans>
   <form-bean name="exampleActionForm" type="com.zotn.struts.example1.ExampleActionForm" />
  </form-beans>
  // 设置action
  <action-mappings>
   <action name="exampleActionForm" path="/example" type="com.zotn.struts.example1.ExampleAction">
  // action内部的foward
    <forward name="foward" path="/foward.jsp" />
   </action>
  </action-mappings>
</struts-config>

Action:

public class ExampleAction extends Action {
  public ActionForward execute(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) {
   // 获得对应的form
   ExampleActionForm eaf = (ExampleActionForm)actionForm;
   // 取得输入的test
   String test = eaf.getTest();
   // 判定并将值放入request
   if ("".equals(test)){
    request.setAttribute("test","empty");
   }else{
    request.setAttribute("test",test);
   }
   // 通过mapping寻找相应的url,返回ActionFoward
   return actionMapping.findForward("foward");
  }
}

FormBean:

public class ExampleActionForm extends ActionForm {
  private String test;
  public String getTest() {
   return test;
  }
  public void setTest(String test) {
   this.test = test;
  }
}

 

    关键字:

天才代写-代写联系方式