web11 Struts处理表单数据
電影網站:www.aikan66.com?
項目網站:www.aikan66.com?
游戲網站:www.aikan66.com?
圖片網站:www.aikan66.com?
書籍網站:www.aikan66.com?
學習網站:www.aikan66.com?
Java網站:www.aikan66.com?
iOS網站:www.aikan66.com
----
Struts框架中通過Action的結果映射配置返回視圖,Action對象是Struts2框架中的請求處理對象那,針對不同的業務請求及處理結果,Action將返回一個字符串,這個字符串就是Action處理結果的邏輯名,Struts2框架將更加邏輯視圖名稱,到配置文件struts.xml中查找邏輯視圖名稱匹配的視圖,找到之后將這個視圖回應給瀏覽器。
----
要求:編寫Action對象,處理對表單提交的數據,模擬實現對指定用戶的問候。
----
1、創建web項目,jwrm04-helloToSB,把包添加到lib,web.xml中注冊過濾器。(詳見web08)。
----
2、創建類GreetingAction的Action對象。
package dog;import com.opensymphony.xwork2.ActionSupport;public class GreetingAction extends ActionSupport{private static final long serialVersionUID=1L;//用戶名private String username;//處理請求 @Overridepublic 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進行處理。
----
3、配置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類對請求作出處理。
----
4、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>
----
5、創建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>
----
6、部署,訪問:http://localhost:8080/jwrm04-helloToSB/index.jsp
點擊“提交”
輸入框為空時點擊“提交”
----
完畢
轉載于:https://www.cnblogs.com/zhaixing/p/5716154.html
總結
以上是生活随笔為你收集整理的web11 Struts处理表单数据的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 易买网的一些增删改查
- 下一篇: NYOJ--811--变态最大值