`
gg19861207
  • 浏览: 179871 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

struts2笔记

    博客分类:
  • ssh
阅读更多

struts2笔记09-10-31】:

 

struts.xml中:

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts PUBLIC

    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

    "http://struts.apache.org/dtds/struts-2.0.dtd">

 

<struts>

 

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

        <action name="hello">

               <result>/hello.jsp</result>

        </action>//通过配置文件产生一个action

    </package>

</struts>

IE浏览器访问:

http://localhost:8888/struts2_0100_introduciton/hello.action

或者:

http://localhost:8888/struts2_0100_introduciton/hello不加.action也可以访问hello这个action)

 

 

 

 

<constant name="struts.devMode" value="true"/>

struts.xml里添加这句话以后,以后在struts.xml中修改过内容后,就不用再重新部署或重新启动服务器了。

 


 

 

查看struts2-core-2.1.6.jar的源文件:

鼠标放在该.jar上,点击右键-àproperties-àjava source attachment

值是:F:/开源软件/struts-2.1.6-allFullDistribution/struts-2.1.6/src/core/src/main/java

 

查看struts2-core-2.1.6.jarjavadoc文件:

鼠标放在该.jar上,点击右键-àproperties-àjavadoc Location

值是:F:/开源软件/struts-2.1.6-allFullDistribution/struts-2.1.6/src/core/src/main/java

 

以后在写一个类的时候,选中这个类,点F1就可以在MyEclipse里查看该类的API帮助文档了。

 

 

 

 

.xml文件里如果没有自动提示的话:

WindowàPrefenrence->XML catalog,点击ADD

Location: F:\开源软件\struts-2.1.6-allFullDistribution\struts-2.1.6\lib\struts2-core-2.1.6\struts-2.0.dtd

 

Key Type:URI

 

Keys: http://struts.apache.org/dtds/struts-2.0.dtd

 

 

客户端访问服务器的流程图:

插入图片:

 

 

 

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

        <action name="hellp">//没有指定actionclass的话,实际执行的是ActionSupport

               <result>/hello.jsp</result>

        </action>//通过配置文件产生一个action

    </package>

</struts>

 

" namespace="/" 默认的情况是namespace=””.

namespace=”” 或者" namespace="/":客户端访问的时候:

http://localhost:8888/struts2_0100_introduciton/ttt.mmmm/pp/hellp,都可以进行访问

 

但是当" namespace="/ttt"的时候,必须是这么访问:

http://localhost:8888/struts2_0100_introduciton/ttt /hellp

 

 

struts2中路径的设置

struts2中直接使用绝对路径就能保证肯定是没错的。

格式是:

例如客户端访问是:

http://localhost:8888/struts2_0100_introduciton/path/index.jsp

 

<%

String path = request.getContextPath();//path的值是:struts2_0100_introduciton(只可能是项目名,不可能是项目名/文件名)

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

 

%>

<head>

    <base href=”<%=basePath%>”/>//这里的意思就是说这个jsp页面里所有的<a href/>连接都会在前面加上basePath,<a href=”test.jsp”>,则实际的是<a href=” http://localhost:8888/struts2_0100_introduciton/test.jsp”/>;假如说是<a href=”/index.jsp”的话,那么访问的将是tomcat的根目录,而不是应用项目的根目录)

</head>

request.getScheme()值是:http

request.getServerName()的值是:localhost

request.getServerPort()的值是:8888

path的值是: struts2_0100_introduciton

 

 

struts2动态方法调用(DMI)

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

        <action name="hello" class=”org.oristand.Hello”>

               <result>/hello.jsp</result>

        </action>//通过配置文件产生一个action

    </package>

 

Hello.java

 

Public class Hello extends ActionSupport{

       Public String add(){

              Return SUCCESS;

}

}

 

那么客户端访问的时候就应该是:

http://localhost:8080/webapplication/hello!add

 

 

struts2中的通配符配置:

 

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

        <action name="Student*" class="com.oristand.test.Student">

        <result>/student_{1}.jsp</result>

        </action>

 </package>

 

客户端访问:

http://localhost:8888/struts2_0100_ActionConfig/Studentadd其中add就是*,所以服务器就会返回student_add.jsp

通过通配符的配置,在struts.xml文件中可以配置的只有一句话:

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

        <action name="*_*" class="com.oristand.test.{1}Action" method=”{2}”>

        <result>/{1}_{2}.jsp</result>

       </action>

   

 </package>

 

这充分体现了struts2的智能性,但是要注意预定好。

客户端访问:

http://localhost:8888/struts2_0100_ActionConfig/Teacher_delete

注意:这样的话就相当于

  <action name="Teacher_delete" class="com.oristand.test.TeacherAction" method=”delete”>

        <result>/Teacher_ delete.jsp</result>

       </action>

 

所以对应的jsp页面必须名字约定好,并且注意相应的Action当中要有execute()方法。

 

 

 

Struts2中接受参数(一):

 

StrudentAction.java:

package com.oristand.test;

 

import com.opensymphony.xwork2.ActionSupport;

 

public class StudentAction extends ActionSupport {

    private String name;

    private int age;

 

    public String getName() {

       return name;

    }

 

 

    public void setName(String name) {

       this.name = name;

    }

 

 

    public int getAge() {

       return age;

    }

 

 

    public void setAge(int age) {

       this.age = age;

    }

 

 

    @Override

    public String execute() throws Exception {

       // TODO Auto-generated method stub

       return SUCCESS;

    }

   

   

<span styl

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics