欢迎来到容一博的博客

扩大
缩小
「一言」· 

【struts2】struts2的使用

1.使用步骤

1) 导入struts2的支持jar包

名称 说明
struts2-core-2.3.4.1.jar Structs2的核心类库
xwork-core-2.3.4.1.jar xwork的核心类库,webwork框架的支持,struts2的前身就是webwork
ognl-3.0.5.jar OGNL表达式语言类库
freemarker-2.3.19.jar Freemarker模板语言支持类库,视图技术
commons-beanutils-1.8.0.jar 封装javabean的工具类库
commons-fileupload-1.2.2.jar 文件上传支持类库
commons-io-2.0.1.jar 处理IO操作的工具类库
commons-lang3-3.1.jar 包含了一些数据类型工具类,是java.lang.*的扩展
javassist-3.11.0.GA.jar (JAVA编程助手)使Java字节码操纵简单。一个编辑Java字节码的类库

2) web.xml配置启动的全局的过滤器(Filter)。和ActionServlet类似。。。

  <!-- 配置启动struts2的全局过滤器 -->
  <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>

3)编写不同的业务Action类 实现Action类,编写不同的业务方法,默认execute()方法

/**
 * 登录逻辑的Action
 * @author Administrator
 *
 */
public class LoginAction implements Action {
	// 默认调用方法
	@Override
	public String execute() throws Exception {
		System.out.println("执行了LoginAction的execute方法");
		// 返回标记
		return "success";
	}
}

4)在src目录下(类路径的根目录下),新建一个struts.xml,配置Action对象。

<?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="xxx" extends="struts-default">
		<!-- 配置Action http://localhost:8080/day28_struts/login.action -->
		<action name="login" class="com.chromer.action.LoginAction">
			<result name="success" type="redirect">/index.jsp</result>
		</action>
	</package>
</struts>

5)访问Action的业务方法

http://localhost:8080/day28_struts/login.action
posted @ 2018-12-18 18:14  容一博  阅读(229)  评论(0编辑  收藏  举报
Live2D

「一言·」