フォームを送信SpringMVC--

AbstractCommandControllerは、サンプル提出フォームを作るために今日使用しました

(1)まず、User.javaを設定します

パッケージcom.zk.domain。

輸入java.util.Date; 

パブリッククラスユーザー{ 
	プライベート整数ID。
	プライベート文字列名; 
	プライベート文字列の年齢; 
	プライベート文字列の誕生日。
	公共の整数のgetId(){ 
		IDを返します。
	} 
	公共ボイドSETID(整数ID){ 
		this.id = ID。
	} 
	パブリック文字列のgetName(){ 
		戻り名。
	} 
	公共ボイドのsetName(文字列名){ 
		this.name =名。
	} 
	パブリック文字列getAge(){ 
		戻り年齢; 
	} 
	公共ボイドsetAge(文字列の年齢){ 
		this.age =年齢。
	} 
	パブリック文字列getBirthday(){ 
		誕生日を返します。 
	} 
	公共ボイドsetBirthday(文字列の誕生日){ 
		this.birthday =誕生日。
	} 
}

(2)第2のステップは、実施ToAddController.javaは、add.jspにジャンプし、

パッケージcom.zk.UserController; 

インポートのjavax.servlet.http.HttpServletRequest; 
インポートのjavax.servlet.http.HttpServletResponse; 

輸入org.springframework.web.servlet.ModelAndView。
輸入org.springframework.web.servlet.mvc.Controller。

パブリッククラスToAddControllerがコントローラ{実装

	公共のModelAndViewのhandleRequest(HttpServletRequestのリクエスト、
			HttpServletResponseの応答)が例外をスロー{ 
		// TODO自動生成方法スタブ
		のModelAndViewのMV =新規のModelAndView()。
		mv.setViewName( "追加")。
		MVを返します。
	} 
} 

CommandController.javaを達成するために、(3)第三工程

パッケージcom.zk.UserController; 

インポートのjavax.servlet.http.HttpServletRequest; 
インポートのjavax.servlet.http.HttpServletResponse; 

輸入org.springframework.validation.BindException。
輸入org.springframework.web.servlet.ModelAndView。
輸入org.springframework.web.servlet.mvc.AbstractCommandController。

輸入com.zk.domain.User。

//从页面接受参数、封装たjavaBean的ユーザは对象
パブリッククラスCommandControllerはAbstractCommandController {延びている
	。//指定把参数封装到那个对象
	パブリックCommandController(){ 
		this.setCommandClass(User.class)を、
	} 
	@Overrideは
	のModelAndViewハンドルを保護(HttpServletRequestのリクエスト、
			HttpServletResponseの応答、オブジェクト・コマンドはBindExceptionエラー) 
			例外がスロー{ 
		// TODO自動生成方法スタブを 
		//指令値は、オブジェクトをパッケージ化され
		、ユーザーユーザー=(ユーザー)コマンド
		
		のModelAndView音楽動画新しい新しいのModelAndView =(); 
		//セットモデル
		mv.addObject(「ユーザー」、ユーザー); 
		//指定されたインターフェイスのビューを返します
		mv.setViewNameを( "インデックス"); 
		ミュージックビデオを返します; 
	} 
}

 (4)第四工程、構成SpringMVC.xml

<?xml version = "1.0"エンコード= "UTF-8"?> 
<豆のxmlns = "http://www.springframework.org/schema/beans" 
	のxmlns:XSI = "http://www.w3.org / 2001 / XMLスキーマ・インスタンス"のxmlns:AOP = "http://www.springframework.org/schema/aop" 
	のxsi:schemaLocationの=" http://www.springframework.org/schema/beans 
         のhttp:// WWW。 springframework.org/schema/beans/spring-beans.xsd 
         http://www.springframework.org/schema/aop 
         http://www.springframework.org/schema/aop/spring-aop.xsd 
         のhttp:// WWW .springframework.org /スキーマ/ TX 
         http://www.springframework.org/schema/tx/spring-tx-3.2.xsd "> 
         <! -配置处理器映射器- > 
         <beanクラス=" ORG。 springframework.web。servlet.handler.BeanNameUrlHandlerMapping "> </豆>
     	 <beanクラス= "org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"> </豆> 
     	 <beanクラス= "org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter"> </豆> 
         <! -命令对象- > 
         <豆ID = "/ command.do"クラス= "com.zk.UserController.CommandController"> </豆> 
         <! -跳转到追加页面- > 
         <豆ID = "/ toAdd.do "クラス=" com.zk.UserController.ToAddController "> </ビーン>   
         <ビーンID =" viewResolver "クラス= "org.springframework.web.servlet.view.InternalResourceViewResolver"> 
         <プロパティ名= "プレフィックス"値=" / WEB-INF / JSP / "> </ property>の
         <プロパティ名= "サフィックス"値= "JSP"> </ property>の
         </豆> 
         
</豆>

(5)は、2つのインタフェースのJSP

add.jsp

<%@ページ言語= "javaの"インポート= "java.utilの*" pageEncodingは= "UTF-8" %> 
<%
文字列のパス= request.getContextPath(); 
ストリングbasePathを= request.getScheme()+ "://" + request.getServerName()+ ":" + request.getServerPort()+ + "/"パス。
%> 
<!DOCTYPE HTML PUBLIC " - // W3C // DTD HTML 4.01移行// EN"> 
<HTML> 
  <HEAD> 
    <基本のhref = "<%= basePathを%>"> 
    <タイトル>マイJSP「を追加。 JSPの」開始ページ</ TITLE> 
	<META HTTP-当量= "プラグマ"コンテンツ= "キャッシュなし"> 
	<META HTTP-当量= "キャッシュ制御"コンテンツ= "キャッシュなし"> 
	<META HTTP-当量= "    
	<! - 
	<リンクのrel = "スタイルシート"タイプ= "テキスト/ cssの"のhref = "Styles.cssを"> 
	- > 
  </ HEAD> 
  <BODY> 
  <フォームアクション= "$ {} pageContext.request.contextPath / command.do "> 
   イド:の<input type =" text」NAME = "ID" ID = "ID" /> <BR> 
   名:の<input type = "text" NAME = "名前" ID = "名前" /> <BR> 
   年齢:の<input type = "text"の名= "年齢" ID = "年齢" /> <BR> 
   誕生日:の<input type = "text"の名= "誕生日" ID = "誕生日" /> <BR > 
  の<input type = "提出"値= "提交" /> 
  </ FORM> 
  </ BODY>
</ HTML>

  index.jspの

<%@ページ言語= "javaの"インポート= "java.utilの*" pageEncodingは= "UTF-8" %> 
<%
文字列のパス= request.getContextPath(); 
ストリングbasePathを= request.getScheme()+ "://" + request.getServerName()+ ":" + request.getServerPort()+ + "/"パス。
%> 
<!DOCTYPE HTML PUBLIC " - // W3C // DTD HTML 4.01移行// EN"> 
<HTML> 
  <HEAD> 
    <基本のhref = "<%= basePathを%>"> 
    <タイトル>マイJSP「インデックス。 JSPの」開始ページ</ TITLE> 
    <META HTTP-当量= "Content-Typeの"コンテンツ= "text / htmlの;のcharset = UTF-8" /> 
	<メタHTTP-当量= "プラグマ"コンテンツ= "キャッシュなし" > 
	<
	<META HTTP-当量= "キーワード"コンテンツ= "KEYWORD1、KEYWORD2、KEYWORD3"> 
	<META HTTP-当量= "説明"コンテンツ= "これは私のページです"> 
	<! - 
	<リンクのrel = "スタイルシート"タイプ= "テキスト/ cssの"のhref = "Styles.cssを"> 
	- > 
  </ HEAD>   
  <BODY> 
  <! - $ {こんにちは} - > 
  <H1> $ {user.name} || $ {user.id } || $ {user.age} || $ {user.birthday} </ H1> 
  </ body> 
</ HTML>

  結果は以下の通りであります:

http:// MS-20170731tkkc:8080 / SpringMVC_004 / command.do ID = 1&名前= ZK&年齢= 24&誕生日= 1995年3月4日?

持ち込まれたパラメータ

 

おすすめ

転載: www.cnblogs.com/longlyseul/p/11403186.html