struts学习——框架搭建

来源:互联网 发布:淘宝上新程丰是正品吗 编辑:程序博客网 时间:2024/05/16 06:30

struts框架的搭建:

1.导入jar包

我所用的struts版本为struts2.3版本,导入相关包

相关包说明commons-fileupload-1.2.2.jar文件上传相关包commons-io-2.0.1.jar文件上传相关包commons-lang3-3.1.jarstruts对java的lang包进行的扩展包freemarker-2.3.19.jarstruts的标签模板库的文件javassist-3.11.0.GA.jarstruts对字节码处理的相关jarognl-3.0.5.jarstruts的ognl表达式功能支持包struts2-core-2.3.4.1.jarstruts的核心功能包xwork-core-2.3.4.1.jarxwork的核心功能包

2.配置web.xml文件

struts的功能初始化是通过过滤器来完成的
将struts的核心过滤器配置到项目的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>



filter-class在如下的位置



不同版本的struts的核心过滤器是不一样的

3.编写action



action的代码如下:
public class TestAction extends ActionSupport {@Overridepublic String execute() throws Exception {return super.execute();}public String test() throws Exception {return "test";}}

action代替servlet处理用户的请求。action一般集成ActionSupport类。
action中的处理请求的出入口方法返回值类型都为String,而且方法中不能有参数。

4.配置struts.xml文件




struts.xml文件配置如下:
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE struts PUBLIC          "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"          "http://struts.apache.org/dtds/struts-2.0.dtd"><struts><package name="xxx" extends="struts-default" namespace="/user"><action name="test" class="cn.xyz.test.TestAction" method="test"><result name="test">/index.jsp</result></action></package></struts>


5.访问

index.jsp的body中随意键入几个字。
启动服务器。
打开浏览器,地址栏键入:localhost:8080/struts_test/user/test
如果出现body中的字的话,表示struts框架搭建成功.

0 0