1.package用来解决action重名的情况,类似java中的包名
2.result标签默认名字为success
3.若package中的namespace没写,则默认namespace为”“
- url发现一个action时,先找匹配的namespace对应的package中是否有该action,如果没有,最后查找namespace为空的package中是否有该action存在
4.复制一个新的web项目
- 右键项目–ctrl+c–ctrl+v–右键新项目–properties–MyEclipse–Web–修改Web Context-root中的内容
5.action找到result的流程
- action中有一个class属性,如果不填写时,默认class值为ActionSupport这个类
- 访问到action时,先找到其class属性对应的类,调用该类的execute方法,获取一个String类型的返回值,通过该值匹配result的name属性,返回对应result的内容
- 自己写action中的class类的三种方式
a.直接写一个类,里面含有execute方法,里面包含有execute方法,返回值为String
b.implements Action(函数式接口) 重写其execute方法
c.extends ActionSupport - 真正开发只用第三种,因为ActionSupport已经封装了一堆可以直接调用的方法,比较方便
6.修改jsp文件编码格式
- Window–Preferences–jsp–encoding改为Chinese,National Standard–IANA改为GB18030
7.路径问题的说明
- 如果服务器根据url无法找到正确的namespace,会返回web.xml中的welcome-file-list中的welcome-file对应的页面,默认为index.jsp
- jsp文件中的相对路径,不是相对该jsp文件的路径,而是相对当前的action的路径
例:
<?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>
<constant name="struts.devMode" value="true" />
<package name="user" namespace="/user" extends="struts-default">
<action name="hello">
<result>
/Hello.jsp
</result>
</action>
</package>
</struts>
如果此时在/Hello.jsp中有连接
<a href="index.jsp">index.jsp</a>
则此时路径为http://localhost:8080/Struts2_0100_Package/user/index.jsp,而不是http://localhost:8080/Struts2_0100_Package/index.jsp
- 所以jsp文件中应使用绝对路径,但jsp中的”/”表示的为”http://wusihan:8080/“而不是“http://wusihan:8080/Struts2_0100_Introduction/”,所以该链接也不可写做
<a href="/index.jsp">index.jsp</a>
8.jsp文件中路径的正确写法
在jsp文件中添加如下代码
<% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %>
basePath =request.getScheme()(“http”)+”://”+request.getServerName()(“localhost”)+”:”+request.getServerPort()(“8080”)+request.getContextPath()(“Struts2_0100_Package”)+”/”;
即basePath为:“http://localhost:8080/Struts2_0100_Introduction/”。修改方式1
<a href="<%=basePath %>index.jsp">index.jsp</a>
此时该路径意思为:“http://localhost:8080/Struts2_0100_Introduction/index.jsp”
- 修改方式2
在head标签内添加代码
<base href="<%=basePath%>">
链接处代码不变,此时下面左右的相对路径都是相对”basePath”的路径
9.动态方法调用(dmi)
- 当url匹配到对应的action时,不一定非执行action对应的class的execute方法,可以在action中添加method属性,则调用该指定方法返回result的名字
- 可以使用动态方法调用,user为namespace,hello为action的name,executewsh为action对应类中的一个方法例:”http://localhost:8080/Struts2_0100_Introduction/user/hello!executewsh“即可以直接调用该方法而不需在action中添加method属性
但要注意的是必须先开启动态调用选项,即在struts.xml中添加如下代码
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
- 好处为可以不用改变配置文件直接改变调用的方法
10.通配符的配置
- 例
<package name="default" namespace="/user" extends="struts-default">
<action name="student*" method="student{1}" class="StartMethod">
<result>
/student{1}_success.jsp
</result>
</package>
- 当url路径为:“http://localhost:8080/Struts2_0100_Introduction/user/studentAdd/”时,struts2匹配到namespace为”/user”,进而匹配其action,发现student*,即此时“”代替Add,此时{1}就代表第一个代表的字符串,即此时系统调用类”StartMethod”的studentAdd方法来获取result的值,若studentAdd方法的返回值为success,此时匹配到对应的result,返回页面studentAdd_success.jsp
11.通配符比动态调用的优势
- 虽然动态调用可以调用指定方法,但是比如我想输入studentAdd时返回studentAdd.jsp,输入studentDel时返回studentDel.jsp这是做不到的,通配符处理方式只要约定好url地址,可以大大简化struts.xml中的配置
12.url与action的匹配顺序
- 如果package中的action有多个,而且url中的action哪个都可以匹配,优先匹配最接近的,带*的都属于同级,同级比较,谁的代码在前先匹配谁
13.eclipse上发布web项目和myeclipse上发布的区别
- Myeclipse发布web工程时,会将工程发布到tomcat文件夹下的webapps文件夹下。而eclipse发布web工程时,它默认不是发布在tomcat下面的,所以在tomcat文件夹下的webapps里没有相应的工程。其实eclipse有自己的tomcat配置文件server.xml,其中定义了工程发布的位置,不是在webapps下,这其中的原理跟tomcat的虚拟路径类似。
- eclipse的tomcat相关配置如下
- 如果你想在eclipse中将工程直接发布到tomcat安装目录之下,那么就可以按照下面的方式来修改