📄 csdn_struts原理与应用(二).htm
字号:
href="http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd</A>"><BR><struts-config><BR>
<data-sources /> // 定义数据源<BR>
<form-beans /> //
定义ActionForm<BR> <global-exceptions />
// 定义全局异常<BR> <global-forwards /> //
定义全局转向url<BR> <action-mappings
/> // 定义action<BR> <controller
/> // 配置Controller<BR>
<message-resources /> //
配置资源文件<BR></struts-config><BR>
</TD></TR></TBODY></TABLE>
<P>Struts由上述几部分组成。其中最主要的是Action和Form。下面简单叙述一下其处理过程。
<P><IMG alt="" hspace=0
src="CSDN_Struts原理与应用(二).files/CSDN_Dev_Image_2003-12-52023510.gif"
align=baseline border=0>
<P>
<P>一个请求提交给ActionServlet,ActionServlet会寻找相应的Form和Action,首先将提交的request对象,映射到form中。,然后将form传递给action来进行处理。action得到form,对xml的mapping,request,response四个对象,并调用execute()方法然后返回一个forward-url(相应视图)给ActionServlet,最终返回给客户端。
<P>我们来看一个最简单的实例。 </P>
<P> </P>
<H1>Chapter 4: Example 1: Basic Framework </H1>
<P><FONT color=blue>Struts principle and practice
</FONT><BR><BR>说明:实例一是最简单的Struts程序。它仅仅使用了1个form和1个action<BR>功能是将首页输入的值传递给action,经过判断后返回结果。如果是空则返回empty<BR>代码如下:<BR>
<P>input.jsp:<BR><TEXTAREA class=code style="WIDTH: 492px; HEIGHT: 120px" name=textarea rows=6 cols=60><form method="post" action="/example.do">
请输入值
<input type="text" name="test"/>
<br><br>
<input type="submit" name="Submit" >
<input name="reset" type="reset" >
</form>
</TEXTAREA>
<P>struts-config.xml:<BR><TEXTAREA class=code style="WIDTH: 492px; HEIGHT: 85px" name=textarea2 rows=4 cols=60><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>
</TEXTAREA>
<P>Action:<BR><TEXTAREA class=code style="WIDTH: 489px; HEIGHT: 100px" name=textarea2 rows=5 cols=59>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");
}
}
</TEXTAREA>
<P>FormBean:<BR><TEXTAREA class=code style="WIDTH: 485px; HEIGHT: 104px" name=textarea2 rows=4 cols=59>public class ExampleActionForm extends ActionForm {
private String test;
public String getTest() {
return test;
}
public void setTest(String test) {
this.test = test;
}
}
</TEXTAREA> </P>
<P><FONT color=#800080 size=+2><A
href="http://localhost:83/index.jsp"><U></U></A></FONT> </P><BR><!--内容结束//--></TD></TR></TBODY></TABLE><BR>
<TABLE width=600 border=0>
<TBODY>
<TR>
<TD>作者相关文章:
<LI><A href="http://www.csdn.net/develop/article/22/22753.shtm"
target=_blank>DOM4J 使用简介<FONT color=#ff0000>(原作)</FONT></A>
<LI><A href="http://www.csdn.net/develop/article/22/22574.shtm"
target=_blank>Struts原理与应用(完)<FONT color=#ff0000>(原作)</FONT></A>
<LI><A href="http://www.csdn.net/develop/article/22/22573.shtm"
target=_blank>Struts原理与应用(三)<FONT color=#ff0000>(原作)</FONT></A>
</LI></TD></TR></TBODY></TABLE><BR>
<TABLE width=600 border=0>
<TBODY>
<TR>
<TD>其它相关文章:
<LI><A href="http://www.csdn.net/develop/article/22/22722.shtm"
target=_blank>强烈推荐一篇文章及其评论 关于MVC等方面<FONT
color=#ff0000>(原作)</FONT></A>
<LI><A href="http://www.csdn.net/develop/article/22/22536.shtm"
target=_blank>Tiles傻瓜式入门―hajavaor―<FONT
color=#ff0000>(原作)</FONT></A>
<LI><A href="http://www.csdn.net/develop/article/22/22255.shtm"
target=_blank>Java Web应用测试指导<FONT color=#ff0000>(原作)</FONT></A>
<LI><A href="http://www.csdn.net/develop/article/22/22176.shtm"
target=_blank>Struts tags (3) ----Bean(2)<FONT
color=#ff0000>(翻译)</FONT></A>
<LI><A href="http://www.csdn.net/develop/article/22/22175.shtm"
target=_blank>Struts tags (2) ----Bean(1)<FONT
color=#ff0000>(翻译)</FONT></A> </LI></TD></TR></TBODY></TABLE><BR>
<TR></TD></TR></TBODY></TABLE><BR><!--文章评论开始//-->
<TABLE cellSpacing=0 cellPadding=0 width=770 align=center bgColor=#006699
border=0>
<TBODY>
<TR bgColor=#006699>
<TD id=white align=middle bgColor=#006699><FONT
color=#ffffff>对该文的评论</FONT></TD>
<TD align=middle><!--文章人气开始//-->
<SCRIPT src="CSDN_Struts原理与应用(二).files/readnum.htm"></SCRIPT>
<!--文章人气开始//--></TD></TR></TBODY></TABLE>
<TABLE cellSpacing=1 cellPadding=2 width=770 align=center bgColor=#666666
border=0>
<TBODY>
<TR>
<TD bgColor=#cccccc colSpan=3><SPAN style="COLOR: #990000"><IMG height=16
hspace=1 src="CSDN_Struts原理与应用(二).files/ico_pencil.gif" width=16>
</SPAN> icecloud<I>(2003-12-17 20:49:20)</I>
</TD></TR>
<TR>
<TD width=532 bgColor=#ffffff colSpan=3><BR>Mai_520,
不是的,就是Action,表示一个动作。ActionServlet是控制器。用默认的就行了<BR></TD></TR></TBODY></TABLE>
<TABLE cellSpacing=1 cellPadding=2 width=770 align=center bgColor=#666666
border=0>
<TBODY>
<TR>
<TD bgColor=#cccccc colSpan=3><SPAN style="COLOR: #990000"><IMG height=16
hspace=1 src="CSDN_Struts原理与应用(二).files/ico_pencil.gif" width=16>
</SPAN> Mai_520<I>(2003-12-17 17:25:40)</I> </TD></TR>
<TR>
<TD width=532 bgColor=#ffffff colSpan=3><BR>public class ExampleAction
extends Action <BR>是不是应该写成<BR>public class ExampleAction extends
ActionServlet<BR><BR></TD></TR></TBODY></TABLE>
<TABLE cellSpacing=1 cellPadding=2 width=770 align=center bgColor=#666666
border=0>
<TBODY>
<TR>
<TD bgColor=#cccccc colSpan=3><SPAN style="COLOR: #990000"><IMG height=16
hspace=1 src="CSDN_Struts原理与应用(二).files/ico_pencil.gif" width=16>
</SPAN> icecloud<I>(2003-12-16 15:44:46)</I>
</TD></TR>
<TR>
<TD width=532 bgColor=#ffffff
colSpan=3><BR>唉,我确实是设置了的。<BR>但是是用了&lt;style&gt;&lt;/style&gt;定义<BR><BR>发上来之后显示不出来,不影响阅读也懒得改了<BR></TD></TR></TBODY></TABLE>
<TABLE cellSpacing=1 cellPadding=2 width=770 align=center bgColor=#666666
border=0>
<TBODY>
<TR>
<TD bgColor=#cccccc colSpan=3><SPAN style="COLOR: #990000"><IMG height=16
hspace=1 src="CSDN_Struts原理与应用(二).files/ico_pencil.gif" width=16>
</SPAN> agui<I>(2003-12-16 13:55:13)</I> </TD></TR>
<TR>
<TD width=532 bgColor=#ffffff colSpan=3><BR>icecloud,
写得好!多谢!提个建议:TextArea是可以有readonly属性的,且可以通过设置样式属性将厚边框去掉。具体做法参考论坛,那里每一个回复都是一个TEXTAREA。如:<BR><BR>&lt;textarea
class=code readonly style="WIDTH: 489px; overflow: hidden; border: 1 solid
#E0E0E0 " name=textarea2 rows=15
cols=59&gt;<BR><BR></TD></TR></TBODY></TABLE>
<SCRIPT language=javascript>
<!--
function isEmpty(s)
{
return ((s == null) || (s.length == 0))
}
function submit1()
{
if (document.add_critique.csdnpassword.value != '_xxx_no')
{
if (isEmpty(document.add_critique.csdnname.value) || isEmpty(document.add_critique.csdnpassword.value) || isEmpty(document.add_critique.critique_content.value))
{
alert('登陆名,密码,评论不能为空!!!!') ;
return false;
}
}
else
{
if ( isEmpty(document.add_critique.critique_content.value))
{
alert('评论不能为空!!!!') ;
return false;
}
}
add_critique.ubmit.disabled =true;
document.add_critique.submit();
}
//-->
</SCRIPT>
<BR><BR>
<TABLE cellSpacing=1 cellPadding=2 width=770 align=center bgColor=#cccccc
border=0>
<TBODY>
<TR>
<TH id=white bgColor=#006699><FONT
color=#ffffff>发表评论</FONT></TH></TR></TBODY></TABLE>
<TABLE cellSpacing=1 cellPadding=2 width=770 align=center bgColor=#ffffff
border=0>
<TBODY>
<TR>
<TD>
<FORM name=add_critique action=/develop/critique_Sql.asp
method=post><INPUT type=hidden value=add name=critique_add>
<SCRIPT>
function getcookieval(offset) {
var endstr = document.cookie.indexOf(';',offset);
if (endstr == -1)
endstr = document.cookie.length;
return unescape (document.cookie.substring(offset,endstr));
}
function getcookie(name) {
var arg = name + '=';
var alen = arg.length;
var clen = document.cookie.length;
var j = 0;
while (j< clen) {
var k = j + alen;
if(document.cookie.substring(j,k) == arg) return getcookieval(k);
j = document.cookie.indexOf('',j)+1;
if (j ==0 ) break;
}
return null;
}
var ss = getcookie('userid');
if ((ss==null) | (ss=='2'))
{
document.write('<font color=red><B>你还没有登录:</B></font>昵称:<input type=text name=csdnname size=10 class=from1> 密码:<input type=password name=csdnpassword size=10 class=from1> <a href=http://www.csdn.net/expert/zc.asp><B>免费注册</B></a>');
}
else
{
document.write('<input type=hidden name=csdnname size=10 value=><input type=hidden name=csdnpassword size=10 value=_xxx_no>')
}
</SCRIPT>
评论:<BR> <TEXTAREA name=critique_content rows=8 cols=100></TEXTAREA><BR>
<INPUT onclick=javascript:submit1(); type=button value=发表评论 name=ubmit>
<INPUT type=hidden value=22572 name=Topic_id> <INPUT type=hidden
value=/Develop/Build_Article.asp?id=22572 name=From>
</FORM></TD></TR></TBODY></TABLE>
<CENTER></CENTER><BR>
<HR width=770 noShade SIZE=1>
<TABLE cellSpacing=0 cellPadding=0 width=500 border=0>
<TBODY>
<TR align=middle>
<TD vAlign=bottom height=10><A
href="http://www.csdn.net/intro/intro.asp?id=2">网站简介</A> - <A
href="http://www.csdn.net/intro/intro.asp?id=5">广告服务</A> - <A
href="http://www.csdn.net/map/map.shtm">网站地图</A> - <A
href="http://www.csdn.net/help/help.asp">帮助信息</A> - <A
href="http://www.csdn.net/intro/intro.asp?id=2">联系方式</A> - <A
href="http://www.csdn.net/english">English</A> </TD>
<TD align=middle rowSpan=3><A
href="http://www.hd315.gov.cn/beian/view.asp?bianhao=010202001032100010"><IMG
height=48 src="CSDN_Struts原理与应用(二).files/biaoshi.gif" width=40
border=0></A></TD></TR>
<TR align=middle>
<TD vAlign=top>百联美达美公司 版权所有 京ICP证020026号</TD></TR>
<TR align=middle>
<TD vAlign=top><FONT face=Verdana>Copyright © CSDN.net, Inc. All rights
reserved</FONT></TD></TR>
<TR>
<TD height=15></TD>
<TD></TD></TR></TBODY></TABLE></DIV><!--211.138.108.194//--></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -