Strtus和Servlet的区别

Struts

首先将struts所需要的jar包,导入到web项目的lib下,将struts.xml文件导入到src下。

 

Strtus和Servlet的区别

Servlet:

首先会去web.xml文件中,从<url-pattern>位置开始读取,找到项目名称,然后是class路径,在后台我们是通过request.getparamenter()方法来获取form中提交过来的参数的,然后通过if判断来跳转界面的,当然,执行过程是通过doGet()和doPost()方法

Strtus:

首先加载的是strtus.xml文件

    <package name="default" extends="struts-default">

    <!-- 通配符 -->

        <action name="hello_*_*"  class="com.struts.Action.ActionDemo" method="{1}">

            <result name="success">hello.jsp</result>

            <result name="abc">hello.jsp</result>

   <result name="{2}">hello.jsp</result>

            <result name="tutorial"  type="redirect">/tutorial/test.action</result>

        </action>

</package>

然后就是 <action name="hello_*_*" 指里面的name就像当与Servlet中界面中传递过来的参数,class="com.struts.Action.ActionDemo" class相当于当你输入的namehello_*_*就是跳转到class这个路径中,method是在ActionDemo中写的方法。

name="hello_*" method="{1}" method对应的是心号的位数.

name="hello_*_*" method="{2}" method对应的是心号的位数.

<result name="{2}">hello.jsp</result>指后台方法中的返回的参数值必须与name中的值相同,比如说ActionDemo类中有一个fun()方法

 

public String fun(){

String s = "你好";

if("你".equals(s)){

s = "1";

}else{

s = "abc";

}

return s;

}

这个方法的结果返回值是1或者abc,如果返回的值是abc,那么resultname的值必须是abc(<result name="abc">hello.jsp</result>),至于jsp页面他是直接镶嵌在<result></result>一对标签中的。

 

posted @ 2015-11-04 15:31  孤鸿丿丿  阅读(164)  评论(1)    收藏  举报