读书人

【struts2】赵雅智_struts2开发环境筹

发布时间: 2013-02-28 11:33:09 作者: rapoo

【struts2】赵雅智_struts2开发环境搭建

struts2是MVC框架

struts2的开发环境搭建

1.找到开发Struts2应用需要使用到的jar文件.

需要下载struts2

官网:
http://struts.apache.org

下载地址 :

http://struts.apache.org/download.cgi#struts238 版本号:struts2.3.8

【struts2】赵雅智_struts2开发环境筹建

说明:

【struts2】赵雅智_struts2开发环境筹建

包含:

【struts2】赵雅智_struts2开发环境筹建

Apps 应用

Lib 类库文件

Docs 帮助文档

Src 源代码

找它需要开发的jar文件

需要解压【struts2】赵雅智_struts2开发环境筹建:或者【struts2】赵雅智_struts2开发环境筹建找到struts-2.3.8-all\struts-2.3.8\apps 下面的文件如下:

【struts2】赵雅智_struts2开发环境筹建

提供了一个空白的项目struts2-blank 我们就可以在这个项目上进行开发struts2.

如果开发的过程中需要其他的jar的文件的支持 怎么办?

需要到\struts-2.3.8-all\struts-2.3.8\lib 中找相应的jar文件。

struts2-blank.war项目 怎么解压
复制到tomcat中的webapps中 启动tomcat即可

在apache-tomcat\bin 找到【struts2】赵雅智_struts2开发环境筹建 点击启动

观察:

【struts2】赵雅智_struts2开发环境筹建

可以打开浏览器IE

输入 http://localhost:8080/struts2-blank

找到解压后的jar文件 :apache-tomcat\webapps\struts2-blank\WEB-INF\lib

【struts2】赵雅智_struts2开发环境筹建

2.编写Struts2的配置文件

对比servlet :

新建一个登陆的项目 :

http://localhost:8080/demo/servlet/demo.do?username=&userpass=

? 参数 get请求方式

? 没有后缀 post请求方式

web.xml文件

<servlet>

<servlet-name>DemoServlet</servlet-name>

<servlet-class>www.csdn.demo.serlvet.DemoServlet</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>DemoServlet</servlet-name>

<url-pattern>/servlet/demo.do</url-pattern>

</servlet-mapping>

过滤: 过滤器

监听 :监听器

都需要在web.xml文件中配置

客户端 ---->web容器

Web.xml文件---->控制器 servlet---->Model--->数据库

<----------------------------------



Struts2 独有的配置文件

struts.xml文件

存放在什么位置? 配置文件内容是什么?

1、必须位于classes路径下面

开发项目的过程中 我们把它放到src路径下面 编译后就会存放到classes下面

客户端 ---->web容器

Web.xml文件---->控制器 servlet---->Model--->数据库

<----------------------------------

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

web.xml文件配置 读取struts.xml文件

2.1.3之前:

org.apache.struts2.dispatcher.FilterDispatcher

配置如下: 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>

注意:

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

Struts1(struts-config.xml) 在web.xml文件中配置是一个servlet

分析第一个struts2的程序

启动 :


013-2-26 11:07:38 com.opensymphony.xwork2.util.logging.jdk.JdkLogger info

信息: Parsing configuration file [struts-default.xml]

2013-2-26 11:07:38 com.opensymphony.xwork2.util.logging.jdk.JdkLogger info

信息: Unable to locate configuration files of the name struts-plugin.xml, skipping

2013-2-26 11:07:38 com.opensymphony.xwork2.util.logging.jdk.JdkLogger info

信息: Parsing configuration file [struts-plugin.xml]

2013-2-26 11:07:38 com.opensymphony.xwork2.util.logging.jdk.JdkLogger info

信息: Parsing configuration file [struts.xml]

struts-default.xml

struts-plugin.xml

struts.xml

三个文件启动的过程中 如果有配置相同的 那么后边的会覆盖前面的。

Bug记录:

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

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

解决方法:关闭 已经启动的tomcat即可。

2、【struts2】赵雅智_struts2开发环境筹建

分析:找不到你请求的 url既是:servlet/demo.do

解决方法: 看你的url是否存在? 如果不存在 根据业务添加。

3、【struts2】赵雅智_struts2开发环境筹建

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

Demo.action

/csdn/demo.action

只提示 :demo没有找到

/csdn下面没有找到 demo action

4、Bug 【struts2】赵雅智_struts2开发环境筹建

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

分析:No result 没有结果

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

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

Execute 方法名称写的不匹配

读书人网 >移动开发

热点推荐