cetia4 1.1文档-2. Basic GET 访问

<!--[if gte mso 10]> <mce:style><! /* Style Definitions */ table.MsoNormalTable {mso-style-name:普通表格; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-parent:""; mso-padding-alt:0cm 5.4pt 0cm 5.4pt; mso-para-margin:0cm; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:10.0pt; font-family:"Times New Roman"; mso-fareast-font-family:"Times New Roman"; mso-ansi-language:#0400; mso-fareast-language:#0400; mso-bidi-language:#0400;} --> <!--[endif]-->

cetia4 REST 框架中,响应用户请求的主要组件是servlets,而不象其他框架一样使用帮助类。实际上,cetia4servlets

com.acsinet_solutions.cetia4.controller.rest.RestServlet的子类, 它继承自javax.servlet.http.HttpServlet.

cetia4 1.1文档-2. Basic GET 访问
<!--[if gte vml 1]><v:shapetype id="_x0000_t75" coordsize="21600,21600" o:spt="75" o:preferrelative="t" path="[email protected]@[email protected]@[email protected]@[email protected]@5xe" filled="f" stroked="f"> <v:stroke joinstyle="miter" /> <v:formulas> <v:f eqn="if lineDrawn pixelLineWidth 0" /> <v:f eqn="sum @0 1 0" /> <v:f eqn="sum 0 0 @1" /> <v:f eqn="prod @2 1 2" /> <v:f eqn="prod @3 21600 pixelWidth" /> <v:f eqn="prod @3 21600 pixelHeight" /> <v:f eqn="sum @0 0 1" /> <v:f eqn="prod @6 1 2" /> <v:f eqn="prod @7 21600 pixelWidth" /> <v:f eqn="sum @8 21600 0" /> <v:f eqn="prod @7 21600 pixelHeight" /> <v:f eqn="sum @10 21600 0" /> </v:formulas> <v:path o:extrusionok="f" gradientshapeok="t" o:connecttype="rect" /> <o:lock v:ext="edit" aspectratio="t" /> </v:shapetype><v:shape id="_x0000_i1025" type="#_x0000_t75" style='width:260.25pt; height:245.25pt'> <v:imagedata src="file:///C:/DOCUME~1/ADMINI~1/LOCALS~1/Temp/msohtml1/01/clip_image001.emz" mce_src="file:///C:/DOCUME~1/ADMINI~1/LOCALS~1/Temp/msohtml1/01/clip_image001.emz" o:title="" /> </v:shape><![endif]--><!--[if !vml]-->

<!--[endif]-->

RestServlet 是一个实现doGet(), doPost(), doPut() doDelete()方法的抽象类,这些将在下面讨论。它还实现了init方法(没有参数),如果你想重写它,必须保证用super.init()调用父类的方法。

Cetia4 REST 框架使用了很到名称上的约定尽可能地避免过度的外部配置例如xml文件。如果代码约定不够用的话,还会使用j2se的注解-但是在大部分情况下不需要,因为得益于框架内的约定和合理的默认行为。框架的通过servlet初始参数设置的一些特性可以在标准的web部署描述文件( /WEB-INF/web.xml )中配置。上下文参数在配置主题中有详尽的说明。


<!--[if gte mso 9]><xml> <w:WordDocument> <w:View>Normal</w:View> <w:Zoom>0</w:Zoom> <w:PunctuationKerning /> <w:DrawingGridVerticalSpacing>7.8 磅</w:DrawingGridVerticalSpacing> <w:DisplayHorizontalDrawingGridEvery>0</w:DisplayHorizontalDrawingGridEvery> <w:DisplayVerticalDrawingGridEvery>2</w:DisplayVerticalDrawingGridEvery> <w:ValidateAgainstSchemas /> <w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid> <w:IgnoreMixedContent>false</w:IgnoreMixedContent> <w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText> <w:Compatibility> <w:SpaceForUL /> <w:BalanceSingleByteDoubleByteWidth /> <w:DoNotLeaveBackslashAlone /> <w:ULTrailSpace /> <w:DoNotExpandShiftReturn /> <w:AdjustLineHeightInTable /> <w:BreakWrappedTables /> <w:SnapToGridInCell /> <w:WrapTextWithPunct /> <w:UseAsianBreakRules /> <w:DontGrowAutofit /> <w:UseFELayout /> </w:Compatibility> <w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel> </w:WordDocument> </xml><![endif]--><!--[if gte mso 9]><xml> <w:LatentStyles DefLockedState="false" LatentStyleCount="156"> </w:LatentStyles> </xml><![endif]-->

<!--[if gte mso 10]> <mce:style><! /* Style Definitions */ table.MsoNormalTable {mso-style-name:普通表格; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-parent:""; mso-padding-alt:0cm 5.4pt 0cm 5.4pt; mso-para-margin:0cm; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:10.0pt; font-family:"Times New Roman"; mso-fareast-font-family:"Times New Roman"; mso-ansi-language:#0400; mso-fareast-language:#0400; mso-bidi-language:#0400;} --> <!--[endif]-->

Rest Servlets中,约定如下:

1, 以Servlet结尾的类,类名前面的部分第一个字符小写将和请求的url同名。

也就是说,例如一个servlet 叫做TopicsServlet(是RestServlet的子类)在forum应用中。那么这个servlet将和这样的url请求相对应例如:http://example.com/forum/topics

http://example.com/forum/topics/234.

但这不是必须的,如果servlet的名字和url不同的话也不会错误,但是为了保持一个项目中代码的一致性,这种做法是强烈推荐的。

2, 在/WEB-INF/web.xml配置的servlet类的管理名字也要求和资源的url相同(解释如上)。例如:对于TopicsServlet,在web部署描述文件中配置的<servlet-name>也必须是topics。一些由框架生成的url将会使用这些servlet名字。( using the standard getServletName() method of javax.servlet.ServletConfig ).

3 servlet的映射必须包括对基本资源的和子资源的响应。例如:TopicsServlet

servlet的名字是topics,servlet的映射必须匹配/topics/*.在没有子资源的情况下,映射可以不含有*,只映射/topics;但是这样情况下必须小心,如果子资源在随后被添加,那么这将会导致问题。

下面的图总结了上面这几点。框架对于servlet所在的资源包没有强制性的命名约定,但是推荐在开发小组使用一致的定义。

cetia4 1.1文档-2. Basic GET 访问