1.设置struts的开发模式(struts.xml):<constantname="struts.devMode"value="true"></constant>
2.Xml中设置提示struts: 在struts.xml中的地址 在window->preference->catalog中
Add:
Key为在struts.xml中的地址
Key type为URL
location为struts的jar包中core包中的struts-2.0.dtd
3.struts原理:请求通过HTTP协议发送给tomcat,收到请求后叫HHTP交给对应的程序去处理,该application去读配置,读web.xml配置了filter会过滤所有的请求,这个地址就会被org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter 处理, 首先看Namespace,从 再处理对应的action.
4.namespace:决定了action的访问路径,默认为“” 可以接受所有路径的action。,namespace可以写为/,或者/xx,对应访问路径为/index.action,/xx/index.action
Package:打包避免重名。例如有两个相同的action,可用package区分。
Namespace为空:表示无法找到精确配置的action
5.路径使用:struts2中路径是铜鼓偶 action的路径来确定的,尽量不要使用相对路径,使用绝对路径,通过:
Request.getContentRoot来拿到webapp的路径,或者指定basePath
<%
String path =request.getContextPath();
String basePath =request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
例如: <ahref="<%=basePath%>hello.jsp">hello</a>
或者 <base href="<%=basePath%>">
6.在action执行时设置执行方法:不建议在action指定使用方法
推荐Dmi(动态调用方法):在调用地址后直接添加方法名字
7 通配符: * *_*
<actionname="student*"class="com.swu.First" method="{1}">
<result>/student{1}.jsp</result>
</action>
<actionname="*_*"class="com.swu.{1}action" >
<result>/{1}_{2}.jsp</result>
</action>
8 action中接受参数的三种方式: 常用第二种
1.用属性接受 eg: user!add?name=aaa&age=9
2.用一个动态model接收
3.modeldrivenparaminput
9.jsp页面中文显示处理: <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>