Struts2之路第一天

打个广告java1234资源分享

1.Struts2的核心是将请求与视图分开,只需要改配置文件就可以改变视图。

2.jsp文件报错:Can not find the tag library descriptor for "/struts-tags"。

<!-- <%@ taglib prefix="c" uri="/struts-tags" %>会报错,错误提示为: Can not find
the tag library descriptor for "/struts-tags" struts-core-xxx.jar包不是struts2的需要在web中配置:
-->

 1     <jsp-config>
 2         <taglib>
 3             <taglib-uri>/struts-tags</taglib-uri>
 4             <taglib-location>/WEB-INF/lib/struts2-core-2.1.6.jar</taglib-location>
 5         </taglib>
 6     </jsp-config>

我之后又启动服务器时,报错org xml sax saxparseexception前言中不允许有内容,当我把加进去的这段代码删了之后,两个错误都没了。

3.修改默认编码 Preferences->搜索jsp->JSP Files 将Encoding改为 ISO 10646/Unicode(UTF-8),IANA就会变为UTF-8.

4.Struts1里面action对象只有一个,Struts2里面每次访问都会创建一个对象

5.路径问题,当在struts.xml里面没有找到对应的namespace时,会返回到web.xml,找到welcome file,将默认的欢迎界面返回到客户端。struts2中的路径问题是根据action的路径而不是jsp路径来确定,所以尽量不要使用相对路径。可以在jsp页面如加入

<%
String Path = request,getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

再在head标签中加入

<base href="<%=basePath%>"/> 

这样,该文档中所有href链接前面均加上了http://站点名:端口号/项目名/,如http://localhost:8080/Struts2_0100/

6.Action 执行的时候不一定要执行execute方法。可以使用动态方法调用DMI,有两种方式

   a.通过配置文件struts.xml中action的method属性指定调用方法,如:
         <action   method="调用方法名">;
     b.在URL中动态指定调用方法,使用感叹号“!”将action名和调用方法名分割开,如:
         http://localhost:8080/Struts2_DMI/user!add
7.通配符*

<package name="actions" extends="struts-default" namespace="/actions">
        <action name="Student*" class="com.bjsxt.struts2.action.StudentAction" method="{1}">
            <result>/Student{1}_success.jsp</result>
        </action>
        
        <action name="*_*" class="com.bjsxt.struts2.action.{1}Action" method="{2}">
            <result>/{1}_{2}_success.jsp</result>
            <!-- {0}_success.jsp -->
        </action>
    </package>

通配符*表示所有,{1}表示第一个*代表的字符串,{2}表示第二个*代表的字符串。如果使用通配符*,要先约定好命名规则,“约定优于配置” 。 

8.jdk1.5和jdk1.6的区别在jdk1.5中,如果实现的是一个接口,就不能写@override,在jdk1.6中可以写。设置jdk,proporties --> Java Compiler:在Compiler compliance lever中选择合适的jdk版本,注意Tomcat里面也弄成一样的。

9.用Action 的属性接收参数,Action里面的成员变量和URL地址里面的参数可以是一一对应的。Struts2会自动调用方法把参数值传递到成员变量里,所以getter方法名要与变量名一致。

10.DomainModel接收参数。DomainModel是域模型,是真正存在的实体概念。执行action时会自动生成DomainModel对象,接收参数。当参数个数与DomainModel对象的属性不一致时,我们可以创建VO。action执行时,参数会先与VO对象匹配,然后可以用new DomainModel(VO),将VO整体的交到DoainModel里面.

11.使用ModelDriver接收参数,实现ModelDriver接口,调用getModel方法,返回一个DomainModel实体对象

12.刚刚新建一个struts2的项目时,用的是2.3.15的版本,只导入七个jar包时报错java.lang.RuntimeException: java.lang.reflect.InvocationTargetException和java.lang.NoClassDefFoundError:org/apache/commons/lang/StringUtils这是因为缺少commons-lang3-3.1.jar和javassist-3.11.0.GA.jar这两个jar包造成的。

原文地址:https://www.cnblogs.com/ligui989/p/3170053.html