struts2(上)

来源:互联网 发布:mysql 中的timestamp 编辑:程序博客网 时间:2024/04/28 04:06

Struts2

新的一学期开始了,开始的一星期学习的是struts2,那下面就是一些关于struts2的一些相关的知识介绍。

一、什么是struts2?

1、struts2是struts1的第二代,是apache的开源框架,即MVC框架。

2、是在struts和webwork的基础上整合的全新的框架。

二、struts2的开发环境

1、找到struts2开发所依赖的jar包;(可以自己在网上进行下载)

http://struts.apache.org/download.cgi#struts237 下载 struts2 2.3.8

解压

apps  存放struts2 应用案例代码

docs  存放struts2 使用文档

lib   存放struts2 jar包文件 

src   存放struts2 源码文件




2、编写struts2的配置文件;

struts2的配置文件是在src下的,名字为:struts.xml;该配置文件的名称不能修改;

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts PUBLIC

"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"

"http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>

    <package name="test" namespace="/csdn" extends="struts-default">

     <action name="demo" class="www.csdn.helloworld.action.DemoAction" method="login">

     <result name="login">/sc.jsp</result>

     </action>

    

    </package>

</struts>

3、在web.xml中加入Struts2 MVC框架启动配置;

在web.xml中添加对struts2 MVC启动的过滤器;

注意:struts2读取到struts.xml的内容后,以javabean形式存放在内存中,以后struts2对用户的每次请求处理将使用内存中的数据,而不是每次都读取struts.xml文件

 配置如下: 2.1.3版本后 如下 配置:

  <filter>

        <filter-name>struts2</filter-name>

        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>

    </filter>

    <filter-mapping>

        <filter-name>struts2</filter-name>

        <url-pattern>/*</url-pattern>

    </filter-mapping>

三、各种bug的分析与处理

1、java.net.BindException: Address already in use: JVM_Bind

     分析:  你使用的tomcat被第二次 或者是n次 启动    以前没有关闭 导致的一个bug

解决方法:

   关闭 已经启动的tomcat即可。

2

 分析:在服务器端找不到对应的demoaction

Demo.action

  

 /csdn/demo.action

只提示 :demo没有找到

/csdn下面没有找到 demo action 

3

No result defined for action com.opensymphony.xwork2.ActionSupport and result success

分析:

   No result 没有结果

om.opensymphony.xwork2.ActionSupport  struts2所有action基类

4、java.lang.NoSuchMethodException: www.csdn.helloworld.action.DemoAction.execute()

Execute 方法名称写的不匹配