当前页面: 开发资料首页 → Java 专题 → Struts开发指南之安装与使用
Struts开发指南之安装与使用
摘要: 基本安装说明同时用一个实例解释了Struts的基本用法
<table width="570" border="0" cellspacing="0" cellpadding="5" bgcolor="FBE392"> <tr> <td> <table width="570" border="0" cellspacing="0" cellpadding="5"> <tr> <td bgcolor="E1B004" width="200" align="center">天极IT资讯短信服务 电脑小技巧
<table width="100%" border="0" cellspacing="0" cellpadding="3" bgcolor="fffcc0"> function check4() { if (dn.mobile.value.length!=11) { alert("手机号码不正确!"); dn.mobile.focus(); return false; } return true; } <form name=dn action="http://www.my5757.com/tj/join.jsp" target=_blank onSubmit="return check4()"> <tr valign=middle> <td>资费:包月5元
手机: <input type=text name=mobile size=11 style="border:1px solid #000000;height=16px"> <input type=image src="http://www.my5757.com/yesky/images/d34.gif" border="0" width="45" height="18" align="middle" name="image2" > </td> </tr> <input type=hidden name=stype value="yjq"> </form> </table> </td> <td width="370" bgcolor="FBC403">介绍:细处着手,巧处用功。高手和菜鸟之间的差别就是:高手什么都知道,菜鸟知道一些。电脑小技巧收集最新奇招高招,让你轻松踏上高手之路。 </td> </tr> </table> </td> </tr> </table>
Struts可以运行在任何一个支持JSP1.2和Servlet2.3的WEB Container中Struts将所有的请求提交到同一个中心控制器,org.apache.struts.action.ActionServlet 类
<iframe width="360" height="300" align="right"scrolling="No" frameborder="0" marginheight="0" marginwidth="0" SRC="http://images.chinabyte.com/adjs/iframe-pip/y-software-pip.html"></iframe>
web.xml配置
<table width="100%" bgColor=#ffffff>
<tr>
<td><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></td></tr></table>
一个标准的使用了Struts的URL样式如下:
扩展映射:http://www.my_site_name.com/mycontext/actionName.do
路径映射:http://www.my_site_name.com/mycontext/do/action_Name
<table width="100%" bgColor=#ffffff>
<tr>
<td><servlet-name>action</servlet-name>
<url-pattern>*.do或/do/*</url-pattern>
</servlet-mapping></td></tr></table>
Struts运行
Struts首先在Container启动的时候调用ActionServlet的init()方法。初始化各种配置。这些配置写在struts-config.xml文件中。
一个标准的struts-config文件包含如下结构:
<table width="100%" bgColor=#ffffff>
<tr>
<td><?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></td></tr></table>
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:
<table width="100%" bgColor=#ffffff>
<tr>
<td><form method="post" action="/example.do">
请输入值
<input type="text" name="test"/>
<br><br>
<input type="submit" name="Submit" >
<input name="reset" type="reset" >
</form></td></tr></table>
struts-config.xml:
<table width="100%" bgColor=#ffffff>
<tr>
<td><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> </td></tr></table>
Action:
<table width="100%" bgColor=#ffffff>
<tr>
<td>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");
}
}</td></tr></table>
FormBean:
<table width="100%" bgColor=#ffffff>
<tr>
<td>public class ExampleActionForm extends ActionForm {
private String test;
public String getTest() {
return test;
}
public void setTest(String test) {
this.test = test;
}
}</td></tr></table>
↑返回目录
前一篇:
Struts开发指南之工作流程
后一篇:
Struts开发指南之J2EE n层结构