一、struts2是什么
struts2是J2EE的比较流行的框架,对JSP/servlet进行了封装,应用于面向WEB开发的项目。struts是MVC设计模式的一种解决方案.
二、struts2与struts1的区别
struts1在2001年诞生,是web开发中MVC设计模式的一种解决方案.webwork随后诞生,是更为灵活合理的MVC框架。struts2是在webwork的基础上的升级,只是命名沿用。
1.Action实现:struts1继承抽象的父类,struts2不需要。
2.Action实例化:struts1中Action是单例模式,struts2为多实例。
3.与jsp/Servlet耦合:struts1调用传统的jsp/servletAPI,struts2重新封装了API(与jsp/servlet无关),实现解耦。
4.测试方面:因为struts1仍然调用servlet的API,因此测试必须依赖于web服务器,但struts2脱离服务器测试。
5.表达式语言:struts1整合了JSTL,struts2则整合了更为强大的OGNL表达式。
总之struts2与struts1除了命名之外,其实没有多大关系。
三、下载
地址:http://struts.apache.org/
保存到自己想要保存的位置即可。
四、介绍
1、解压
2、jar包介绍
3、必须包
五、开发环境搭建
1、创建Dynamic Web Project
2、tomcat和jre的配置
3、拷贝jar包
4、配置web.xml的过滤器
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0">
<display-name>struts2</display-name>
<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>
<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>
</web-app>
5、写HelloAction
package com.tong.action;
public class HelloAction {
public String execute() {
System.out.println("HelloAction.execute()");
return "success";
}
}
6、src下写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>
<constant name="struts.devMode" value="true" />
<package name="test" namespace="/" extends="struts-default">
<action name="hello" class="com.tong.action" method="execute">
<result name="success">/HelloWorld.jsp</result>
</action>
</package>
</struts>
7、在WebContent下写HelloWorld.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<base href="${pageContext.request.contextPath}/">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
hello World jsp
</body>
</html>
执行即可