項目網站:www.aikan66.com
遊戲網站:www.aikan66.com
圖片網站:www.aikan66.com
書籍網站:www.aikan66.com
學習網站:www.aikan66.com
Java網站:www.aikan66.com
iOS網站:www.aikan66.comhtml
----java
Struts框架中經過Action的結果映射配置返回視圖,Action對象是Struts2框架中的請求處理對象那,針對不一樣的業務請求及處理結果,Action將返回一個字符串,這個字符串就是Action處理結果的邏輯名,Struts2框架將更加邏輯視圖名稱,到配置文件struts.xml中查找邏輯視圖名稱匹配的視圖,找到以後將這個視圖迴應給瀏覽器。web
----apache
要求:編寫Action對象,處理對錶單提交的數據,模擬實現對指定用戶的問候。瀏覽器
----框架
一、建立web項目,jwrm04-helloToSB,把包添加到lib,web.xml中註冊過濾器。(詳見web08)。jsp
----ide
二、建立類GreetingAction的Action對象。post
package dog; import com.opensymphony.xwork2.ActionSupport; public class GreetingAction extends ActionSupport{ private static final long serialVersionUID=1L; //用戶名 private String username; //處理請求 @Override public String execute() throws Exception{ //判斷用戶名是否有效 if(username==null||"".equals(username)){ //返回到錯誤頁面 return ERROR; }else{ //返回到成功界面 return SUCCESS; } } //username的getter方法 public String getUsername(){ return username; } //username的setter方法 public void setUsername(String username){ this.username=username; } }
GreetingAction類用於對錶單提交的username進行處理。
----
三、配置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> <!-- 聲明包 --> <package name="myPackage" extends="struts-default"> <!-- 定義action --> <action name="greeting" class="dog.GreetingAction"> <!-- 定義成功的映射頁面 --> <result name="success">success.jsp</result> <!-- 定義失敗的映射頁面 --> <result name="error">error.jsp</result> </action> </package> </struts>
就是說,當web應用訪問目錄下「greeting」時,將有GreetingAction類對請求做出處理。
----
四、index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% 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>My JSP 'index.jsp' starting page</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> <form action="greeting.action" method="post"> 請輸入您的姓名:<input type="text" name="username"> <input type="submit" value="提交"> </form> </body> </html>
----
五、建立success.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <%@ taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'success.jsp' starting page</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> <font color="red"> <s:property value="username"/> </font> ,您好!歡迎來到本站。 </body> </html>
相似,建立error.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <%@ taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'error.jsp' starting page</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> <font color="red"> 錯誤,您沒有輸入用戶名! </font> </body> </html>
----
六、部署,訪問:http://localhost:8080/jwrm04-helloToSB/index.jsp
點擊「提交」
輸入框爲空時點擊「提交」
----
完畢