struts就是servlet來實現的,只是封裝了某些功能,例如上傳(如果用servlet寫比較麻煩,struts直接就封裝好了),使編程者用起來方便
具體來說作用如下:
1.獲取表單內容,並組織生成參數對象
2.根據請求的參數轉發請求給適當的控制器
3.在控制器中調用業務接口
4.將業務接口返回的結果包裝起來發送給指定的視圖,並由視圖完成處理結果的展現
5.做一些簡單的校驗或是國際化工作
要實現一個Structs項目,先要導包
接着配置Structs的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>
<constant name="struts.devMode" value="true" />
<package name="default" namespace="/" extends="struts-default">
<action name="hell">
<result>
/Hello.jsp
</result>
</action>
</package>
</struts>
再隨便寫個頁面,即可完成Structs的最基本功能
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>HelloStruts2</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
Hello Struts2 <br>
</body>
</html>
整個項目目錄如下:
Struct2所依賴的JAR包
http://download.csdn.net/download/weixin_38894231/10103862