今天和同学一起说要做一个项目,使用ssh框架,那就开始学习呗。
什么是strut2:
Struts 2是Struts的下一代产品,是在 struts 1和WebWork的技术基础上进行了合并的全新的Struts 2框架。其全新的Struts 2的体系结构与Struts 1的体系结构差别巨大。Struts 2以WebWork为核心,采用拦截器的机制来处理用户的请求,这样的设计也使得业务逻辑控制器能够与ServletAPI完全脱离开,所以Struts 2可以理解为WebWork的更新产品。虽然从Struts 1到Struts 2有着太大的变化,但是相对于WebWork,Struts 2的变化很小。
在我认为这个框架就是基于MVC的模式,只不过是现在这个框架我们不需要手动的搭建,struts中的好多文件都已经帮助我们将这个框架搭建完成,我们要做的就是学会怎么想这个已经搭建好的框架中添加我们想要的东西。
废话不多说,下面就来配置我的strut2框架:
1、下载strut2的jar包,离开这个,那就别谈什么搭建了。http://struts.apache.org/download.cgi
2、解压后出现这个目录。
apps中是自带的demo,docs文档,lib就是各种包了。我用的是strut2 2.3.14,需要向myeclipse中导入commons-fileupload-1.2.2.jar,commons-io-2.0.1.jar,commons-lang-2.4.jar,commons-lang3-3.1.jar,commons-logging-1.1.1.jar,commons-logging-api-1.1.jar,freemarker-2.3.19.jar,javassist-3.11.0.GA.jar,ognl-3.0.6.jar,
struts2-core-2.3.14.jar,xwork-core-2.3.14.jar,由于版本差异,所以比一定完全使用。
3、配置web.xml文件:
<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>
当然我们可以直接从自带的demo中找到web.xml文件,拷贝一份
4、配置strut.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>
</struts>
<struts>中用来定义自己的包文件</struts>
这是我的第一个strut2
strut2.xml文件
<struts>
<package name="itcast" namespace="/test" extends="struts-default">
<action name="helloworld" class="cn.itcast.action.HelloWorldAction" method="execute">
<result name="success">/WEB-INF/page/helloworld.jsp</result>
</action>
</package>
</struts>
建项目如上图所示:
HelloWorldAction:
package cn.itcast.action;
public class HelloWorldAction {
private String message;
public String getMessage() {
return message;
}
public String execute(){
message="This is my first strut2 !";
return "success";
}
}
page/helloworld.jsp:
<body>
${message}
</body>
都定义完了我们该访问了:
http://localhost:8080/test/helloworld