微信公衆號:compassblog
html歡迎關注、轉發,互相學習,共同進步!前端
有任何問題,請後臺留言聯java
(1)、Struts2默認常量配置文件路徑,以下圖:web
(2)、Struts2常量配置方式:以配置國際化字節編碼UTF-8爲例apache
方式1:在struts.xml文件中配置瀏覽器
<constant name="struts.i18n.encoding" value="UTF-8"></constant>
複製代碼
方式2:在src下建立struts.properties文件配置bash
文件具體路徑以下圖:微信
文件配置代碼爲:框架
struts.i18n.encoding=UTF8
複製代碼
方式3:在項目的web.xml文件中配置dom
<context-param>
<param-name>struts.i18n.encoding</param-name>
<param-value>UTF-8</param-value>
</context-param>
複製代碼
(3)、Struts2配置文件加載順序:
defalut.properties:首先加載默認配置
struts.xml:其次加載Action及本身的常量配置
struts.properties:第三加載文件中的常量配置
web.xml:最後加載核心過濾器及本身的常量的配置
方式1:直接建立一個類
package com.struts2.action;
//建立一個POJO類:不繼承任何父類,不實現任何接口
public class Demo1 {
//具體的方法
}
複製代碼
方式2:建立一個類實現Action接口
package com.struts2.action;
import com.opensymphony.xwork2.Action;
//實現一個Action接口
// 覆寫excute方法,提供action方法的規範
// Action接口預置了一些字符串,以在返回結果時使用
public class Demo2 implements Action {
@Override
public String execute() throws Exception {
//具體代碼實現
return null;
}
}
複製代碼
方式3:建立一個類繼承ActionSupport類
package com.struts2.action;
import com.opensymphony.xwork2.ActionSupport;
//該類已經實現了Validateable, ValidationAware, TextProvider, LocaleProvider接口
//當項目須要使用這些接口時,不須要本身來實現
public class Demo3 extends ActionSupport{
//具體方法
}
複製代碼
(1)、新建項目ExamplePeoject,導入所須要的jar包,搭建Struts2開發環境,以下圖:
(2)、在WebContent目錄下分別建一個index.jsp訪問文件、success.jsp跳轉文件、failure.jsp跳轉文件,具體代碼以下:
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>首頁</title>
</head>
<body>
<h1>Struts2項目測試實例</h1>
<form action="${pageContext.request.contextPath}/example" method="post">
<table>
<tr>
<td>輸入測試內容:<input type="text" name="content" id="content"></td>
<td><input type="submit" value="點擊測試"></td>
</tr>
</table>
</form>
</body>
</html>
複製代碼
success.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>跳轉頁</title>
</head>
<body>
<h1>頁面成功跳轉,Struts2實例項目測試成功!</h1>
</body>
</html>
複製代碼
failure.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>跳轉頁</title>
</head>
<body>
<h1>頁面跳轉成功,但輸入內容不正確!</h1>
</body>
</html>
複製代碼
(3)、配置struts.xml文件,具體代碼以下:
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.i18n.encoding" value="UTF-8"></constant>
<package name="test" namespace="/" extends="struts-default" >
<action name="example" class="com.struts2.action.ExampleAction" method="test" >
<result name="success">/success.jsp</result>
<result name="failure">/failure.jsp</result>
</action>
</package>
</struts>
複製代碼
(4)、新建Action類ExampleAction.java,具體代碼以下:
ExampleAction.java
package com.struts2.action;
import com.struts2.domain.Content;
public class ExampleAction {
//定義內容屬性
private String content;
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String test(){
//獲取客戶端內容,並進行內容判斷
if(getContent()!=null){
if(getContent().equals("Struts2"))
return "success";
}
return "failure";
}
}
複製代碼
(5)、發佈項目到Tomcat容器,而後到瀏覽器地址欄測試,結果以下:
跳轉到首頁:
輸入Struts2進行測試:
輸入Struts進行測試:
(6)、該實例項目訪問流程,如圖所示:
本項目運行環境:jdk1.七、Tomcat7.0
關注微信公衆號compassblog,後臺回覆 「Struts2系列二」 獲取本項目源碼
本系列後期仍會持續更新,歡迎關注!
若是你認爲這篇文章有用,歡迎轉發分享給你的好友!
本號文章能夠任意轉載,轉載請註明出處!
您可能還喜歡: