Struts2
在项目中,分为:WEB层、Service层、Dao层
而当使用框架时:Struts2、JavaBean、Hibernate或MyBatis
使用Struts2的优势:自动封装参数、参数校验、结果处理(转发、重定向)、国际化、显示等待页面、防止表单的重复提交
Struts1:基于Servlet开发(有线程安全问题)
Struts2:基于WebWork理念,使用Filter技术(彻底解决Struct1的问题)
----------------------------------------------------------------------------------------------------------------
框架搭建:
1、在Struts2/apps/struts2-blank.war中,有WEB-INF/lib中必备的jar文件,复制到Web项目的lib中
(struts2-blank.war是Struts2的一个范例)
2、书写Action类
在src/cn.huang包中
package cn.huang;
public class HelloAction {
public String hello() {
System.out.println("hello strut2!");
return "success";
}
}3、书写struts.xml文件
(在struts2-core-2.3.24.jar中,把struts-2.3.dtd文件复制到某个文件夹中,
把Window/Preferences搜cata:Location:某个文件
URI:http://struts.apache.org/dtds/struts-2.3.dtd)
// <!DOCTYPE>用于定义文档类型。
把<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">复制到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="hello" namespace="/hello" extends="struts-default">
<action name="HelloAction" class="cn.huang.HelloAction" method="hello">
<result name="success">/hello.jsp</result>
</action>
</package>
</struts>4、将struts2核心过滤器配置到web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>struct2_first</display-name>
<filter>
<filter-name>struts2F</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2F</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>----------------------------------------------------------------------------------------------------------------
Struts2的第1个例子搭建完毕
本文介绍了Struts2框架的基本使用方法,包括项目搭建步骤、Action类编写、struts.xml配置及核心过滤器设置等关键内容。
229

被折叠的 条评论
为什么被折叠?



