【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.jar的javadoc文件:
鼠标放在该.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">//没有指定action的class的话,实际执行的是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
分享到:
相关推荐
Struts2笔记 本人工作中积累的,故做成笔记以供大家一起学习进步. 模块分的非常详细,各个细节都会关注到,只挑选其精华,工作中用得到的技术!!! 不要看资源小,起到的作用却非常大!!!
本笔记主要涵盖了Struts2的基础知识,包括但不限于配置、Action、拦截器、结果类型以及异常处理等内容。 1. **Struts2框架简介** Struts2是Apache软件基金会的一个开源项目,它是Struts1的升级版,弥补了Struts1在...
Struts2是一个流行的Java web框架,...以上就是"马士兵Struts2笔记2013"中涉及的主要知识点,这些内容涵盖了Struts2的基础配置、数据处理、验证、调试以及与视图层的交互等方面,对于理解和掌握Struts2框架非常有帮助。
### Struts2框架核心概念与工作原理 #### 一、框架概述 Struts2是Apache组织维护的一个开源项目,它是Struts1的升级版本,在设计理念和技术实现上都有较大的改进。Struts2是一个基于MVC(Model-View-Controller)...