选择显示字体大小

struts开发指南之安装与使用


  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。下面简单叙述一下其处理过程。



  一个请求提交给actionservlet,actionservlet会寻找相应的form和action,首先将提交的request对象,映射到form中。,然后将form传递给action来进行处理。action得到form,对xml的mapping,request,response四个对象,并调用execute()方法然后返回一个forward-url(相应视图)给actionservlet,最终返回给客户端。

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

  说明:实例一是最简单的struts程序。它仅仅使用了1个form和1个action功能是将首页输入的值传递给action,经过判断后返回结果。如果是空则返回empty。代码如下:

  input.jsp

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


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


 


关键字 本文所属关键字

相关 与本文相关文章

分类 所有文章关键字导航

源码编程相关

Java   Asp   PHP   .Net   XML   C/C++   CGI   VB   Jsp   J2ee   J2se   J2me   EJB   Servlet   Tomcat   Resin   Struts   Weblogic   Eclipse   ANT   GUI   JMS   Web servise   IDEA   Webphere   Hibernate   Spring   Jboss   Applet   Swing   Socket   Javamail   Perl   Ajax   P2P   安全   模式   框架   测试   开源   游戏

SQL数据库相关

My-SQL   Ms-SQL   Access   DB2   Oracle   Sybase   SQLserver   索引   存储过程   加密   数据库   分页   视图  

手机无线相关

3G   Wap   CDMA   GRPS   GSM   IVR   彩信   短信   无线   增值业务

网页设计制作相关

HTML   CSS   网页配色   网页特效   Javascript   VBscript   Dreamweaver   Frontpage   JS   Web   网站设计

网站建设推广相关

建站经验   网站优化   网站排名   推广   Alexa

操作系统/服务器相关

Windows XP   Windows 2000   Windows 2003   Windows Me   Windows 9.x   Linux   UNIX   注册表   操作系统   服务器   应用服务器

图形图像多媒体相关

Photoshop   Fireworks   Flash   Coreldraw   Illustrator   Freehand   Photoimpact   多媒体   图形图像

标准 网站致力的规范

Valid CSS!

无不良内容,无不良广告,无恶意代码

Valid XHTML 1.0 Transitional

creativecommons