一、struts2的作用:实现视图和请求分开;
二、在浏览器中任意输入后面路径的地址时,为了防止找不到页面或出现错误,则可以设置一个默认页面,比如首页,可以在
Struts.xml中配置:
<default-action-ref name="index"></default-action-ref>
<action name="index">
<result>/index.jsp</result>
</action>
三、调用Action中某个方法时可以有两种方法:
a)通过Action标签中的method方法,如:
<action name="hello" class="com.liyang.action.StudentAction" method="myExecute">
b)通过DMI即动态方法调用,在浏览器中指定要调用的方法,比如:
http://user-pc:8080/Struts2Test/hello!myExecute
则默认调用myExecute方法。
但不能这样调用加上.action:
http://user-pc:8080/Struts2Test/hello.action!myExecute
注意:当两种情况都使用时:DMI调用优先,即调用DMI指定的方法.
四、在配置Action时通配符的使用:
<action name="*_*" class="com.liyang.action.{2}Action" method="{1}{2}">
其中每个*用来匹配从浏览器传来的值,这样可以动态指定Action和Action中的方法。
{1}代表第一个*的值。
注意:在做项目时一定要进行命名规范和相应的约定,这样可以使配置文件大大简化,
即:约定优于配置。
五、struts.xml中的<constant name="" value=""></constant>其中的name都在default.properties已经定义了并设置了初始值,但可以通过此标签进行更改。
比如解决字符编码问题:struts.i18n.encoding=UTF-8
开发模式:struts.devMode = false
六.struts路径问题:
应该使用绝对路径,通过request对象可以得到当前路径,如:
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
这样在任意界面返回首页时可以通过此变量可以轻松返回。
七、当jsp编码为utf-8时,如果是get方法进行中文传参,会出现乱码,但当进行post方法进行中文传参时不会出现乱码。