一、搭建环境
1.下载Struts2
2.环境搭建
新建项目
解压下载的Struts2,到目录struts2-rest-showcase.war\WEB-INF\lib里面找到需要导入的jar包。
把jar包复制到对应工程的目录下面。
这不是strust2的最小jar包,最小依赖包以后再查看。
在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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
加入struts2的配置文件。
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
</struts>
配置源代码提示,window->property
二、实例测试
1.准备默认访问页面,启动服务器,经测试框架已经搭好
2.框架使用初探
struts.xml文件的配置内同如下:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<constant name="struts.action.extension" value="action,do"></constant>
<package name="helloWorld" namespace="/" extends="struts-default">
<action name="product-input">
<result name="success" type="dispatcher">/WEB-INF/pages/input.jsp</result>
</action>
<action name="product-save" class="com.atguigu.struts2.helloworld.Product"
method="save">
<result name="details">/WEB-INF/pages/details.jsp</result>
</action>
</package>
</struts>
index.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>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="product-input.action">Product Input</a>
<br><br>
<a href="test.action">Test</a>
</body>
</html>
product.jsp代码如下:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="product-save.action" method="post">
ProductName: <input type="text" name="productName"/>
<br><br>
ProductDesc: <input type="text" name="productDesc"/>
<br><br>
ProductPrice: <input type="text" name="productPrice" />
<br><br>
<input type="submit" value="Submit"/>
</form>
</body>
</html>
details.jsp代码如下:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<br><br>
ProductName:${productName }
<br><br>
ProductDesc: ${productDesc }
<br><br>
ProductPrice: ${productPrice }
<br><br>
productId: ${productId }
</body>
</html>
三、Struts2基本功能说明
Struts2基本功能说明
1.Struts2使用了一个过滤器作为控制器
2.Struts2中,HTML表单将被直接映射到一个POJO.
3.Struts2中的验证逻辑编写在Action中.
4.Struts2中任何一个POJO都可以是一个Action类.
5.Struts2在页面里使用OGNL来显示各种对象模型