您的位置:首页 > 编程语言 > Java开发

85-002-5 Struts环境的搭建

2016-06-29 12:20 369 查看
图文版:http://note.youdao.com/yws/public/redirect/share?id=abc20790e48da5130e227d9390e4df92&type=false
资源文件下载 https://yunpan.cn/OcRaTMHR3NWIIf  访问密码
4b2c

16. 搭建玩hibernate与spring之后现在就开始搭建struts环境了

17. 首先也是添加配置文件与相应的jar包,这一步可以通过myEclipse工具自动实现一部分的功能免去手动编写的麻烦,比如web.xml中的struts过滤器等。
18. 现在通过jsp来实现action的功能,如果成功表示单独的struts环境是没有问题的。之后就可以与spring整合了。

下面创建一个

    CategoryAction.java
public class CategoryAction extends ActionSupport {
    private CategoryService categoryService ;

    public void setCategoryService(CategoryService categoryService){
        this.categoryService = categoryService ;
    }
   public String update(){
        System.out.println("==update==") ;
        System.out.println(categoryService) ; //因为还没有与spring整合,所以输出为null
        return "index" ;
    }
    public String save(){
        System.out.println("==save==") ;
        System.out.println(categoryService) ; //因为还没有与spring整合,所以输出为null
        return "index" ;
    }
}
19. 然后配置struts.xml文件

    struts.xml
<struts>
    <package name="shop" extends="struts-default">
        <!--访问地址 category_update.action等这样的配置具有更大的灵活性 -->
        <action name="category_*" class="cn.it.shop.action.CategoryAction" method="{1}">
            <result name="index">/index.jsp</result>
        </action>
    </package>
</struts>    
20. 然后定义一个jsp页面
    index.jsp

<html>
  <head>
    <title>My JSP 'index.jsp' starting page</title>
  </head>
  <body>
      <a href="${pageContext.request.contextPath}/categroy_update.action">update</a>
     <a href="category_save.action">save</a>
  </body>
</html>

21. 现在就通过tomcat容器进行访问,发现了异常
ClassNotFoundException: org.springframework.web.context.ContextLoaderListene

 21.1 通过上网查找首先在stackoverflow查找原因,是IDE的原因,结果没有解决,然后在mkYONG上找到了同样的问题
The ContextLoaderListener is used to integrate Spring with other web application.
 21.2 通过上面的说明ContextLoaderListener的作用是整合spring和其他框架或者工具的,这是他的配置
    web.xml
<context-param>
 <param-name>contextConfigLocation</param-name>
 <param-value>/WEB-INF/Spring/applicationContext.xml</param-value>
  </context-param>

  <listener>
    <listener-class>
          org.springframework.web.context.ContextLoaderListener
    </listener-class>
  </listener>
 21.3 And the common error message is, your server can not find this Spring ContextLoaderListener class during the server start up.意思是说服务器在启动的时候找不到ContextLoaderListener这个类。This
is always happened in deployment and debugging environment. 这个问题在部署或项目调试时经常出现。In deployment environment, just make sure your server classpath has included the Spring jar library (e.g spring-2.5.6.jar).
For Spring3,
ContextLoaderListener is moved to spring-web.jar, you can get the library from Maven central repository.意思是说如果问题出现在部署上面的话,那么你的项目有可能是缺少Spring的相关jar包,如果你的spring是3.x的话,则应该缺少spring-web.jar包,可以通过maven仓库获得,当然也可以自己本地配置jar包。
 21.4 经过多次的实验排查,终于消除了这个异常,原因只是tomcat启动时找不到那个类或者是jar包,因为我的jar包都是通过buildpath配置的,也就是说myEclipse工具只是配置相关jar包的一个引用。myEclipse找的到,但是tomcat就不干了,毕竟当初配置这些类的时候是你myEclipse配置的与我tomcat没有毛关系,所以我tomcat当然罢工说找不到,解决的办法,把所有之前配置的jar包通通放到项目的WebRoot/WEB-INF/lib目录下,这下tomcat就变乖了。
 22. 现在可以打开网页了,但是通过网页点击链接请求action时又报出异常
 There is no Action mapped for namespace [/] and action name [categroy_update] associated with context path [/shop].
 22.1 很明显是路径的问题,经检查是jsp页面的跳转路径写错了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: