【struts2】赵雅智_struts2开发环境搭建
struts2是MVC框架
struts2的开发环境搭建
1.找到开发Struts2应用需要使用到的jar文件.
- 需要下载struts2
下载地址:
http://struts.apache.org/download.cgi#struts238版本号:struts2.3.8
说明:
包含:
Apps应用
Lib类库文件
Docs帮助文档
Src源代码
找它需要开发的jar文件
需要解压:或者找到struts-2.3.8-all\struts-2.3.8\apps下面的文件如下:
提供了一个空白的项目struts2-blank我们就可以在这个项目上进行开发struts2.
如果开发的过程中需要其他的jar的文件的支持怎么办?
需要到\struts-2.3.8-all\struts-2.3.8\lib中找相应的jar文件。
struts2-blank.war项目怎么解压
复制到tomcat中的webapps中启动tomcat即可
在apache-tomcat\bin找到点击启动
观察:
可以打开浏览器IE
输入http://localhost:8080/struts2-blank
找到解压后的jar文件:apache-tomcat\webapps\struts2-blank\WEB-INF\lib
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中加入Struts2MVC框架启动配置
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-2611:07:38com.opensymphony.xwork2.util.logging.jdk.JdkLoggerinfo
信息:Parsingconfigurationfile[struts-default.xml]
2013-2-2611:07:38com.opensymphony.xwork2.util.logging.jdk.JdkLoggerinfo
信息:Unabletolocateconfigurationfilesofthenamestruts-plugin.xml,skipping
2013-2-2611:07:38com.opensymphony.xwork2.util.logging.jdk.JdkLoggerinfo
信息:Parsingconfigurationfile[struts-plugin.xml]
2013-2-2611:07:38com.opensymphony.xwork2.util.logging.jdk.JdkLoggerinfo
信息:Parsingconfigurationfile[struts.xml]
struts-default.xml
struts-plugin.xml
struts.xml
三个文件启动的过程中如果有配置相同的那么后边的会覆盖前面的。
Bug记录:
1、java.net.BindException:Addressalreadyinuse:JVM_Bind
分析:你使用的tomcat被第二次或者是n次启动以前没有关闭导致的一个bug
解决方法:关闭已经启动的tomcat即可。
2、
分析:找不到你请求的url既是:servlet/demo.do
解决方法: 看你的url是否存在?如果不存在根据业务添加。
3、
分析:在服务器端找不到对应的demo的action
Demo.action
/csdn/demo.action
只提示:demo没有找到
/csdn下面没有找到demoaction
4、Bug
Noresultdefinedforactioncom.opensymphony.xwork2.ActionSupportandresultsuccess
分析:Noresult没有结果
om.opensymphony.xwork2.ActionSupportstruts2所有action基类
5、java.lang.NoSuchMethodException:www.csdn.helloworld.action.DemoAction.execute()
Execute方法名称写的不匹配