Struct2框架搭建

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个例子搭建完毕

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值