いくつかの応答0025SpringMVC

この記事は、主にいくつかの方法で低応答に関連します:

図1に示すように、文字列を返し
空隙返すこと、2
。3、のModelAndViewを返す
。4、転送およびリダイレクトキーワードリダイレクトフォワードキー戻り列
5、AJAX呼び出しが戻るのJSON

特定の実装コードを参照してください。

responseTest.jsp

<% - 
のIntelliJ IDEAによって作成されます。
ユーザー:管理者
日付:2019年12月7日
時間:午前8時41
変更し、このテンプレートファイルを使用するには|設定|ファイルのテンプレート。
- %>
<%@ページのcontentType =「テキスト/ HTML ;のcharset = UTF-8 "LANGUAGE =" Javaの"%>
<HTML>
<HEAD>
<TITLE>タイトル</ TITLE>
<スクリプトタイプ="テキスト/ JavaScriptを"SRC =" JS / jquery.min.js「> < /スクリプト>
<SCRIPT>
//ページがロードされ、単一の結合イベント
$(関数(){
$( "#のBTN")。クリック(関数(){
//ボタンが正しくハローポップにボタンをクリックするか、[いいえ]をクリックを見つけていないテスト反応
//リソースファイルjquery.min.jsもフロントコントローラ傍受導入ので
//必要性springmvcを。
XMLフロントエンドコントローラにはどのような静的リソースファイルインターセプトしないように構成されて //警告(「こんにちは」);
アヤックス$({
//書き込みJSONフォーマット、プロパティと値
URL: "応答/ testAjax"、
//要求タイプ
のcontentType: "ファイルアプリケーション/ JSON;のcharset = UTF-8"、
//リクエストデータの
データ:「{ "名前「:」「」zhangsan年齢『:』 18「}」、
//戻り値の型
データ型:」JSON 『
ポスト方法を指定するか、または結腸・エラーなどの特殊文字含まれるように//必要性
タイプのを:』ポスト」、
//コールバック関数の戻りが正常にデータが返されたデータであり、
成功:関数(データ){
アラート(data.name);
アラート(data.age)。
}
})。
});
});
</スクリプト>
</ HEAD>
<BODY>
<a href="response/testString">たTestString </a>の<BR/>
<a href="response/testVoid"> testVoid </a>の<BR/>
< A HREF = "応答/ testModelAndView"> testModelAndView </a>の<BR/>
<a href="response/testForwardOrRedirect"> testForwardOrRedirect </a>の<BR/>
<ボタンID = "BTN">发送AJAX请求</ボタン>
</ BODY>
</ HTML>

ResponseController.java

パッケージcom.example.controller。

輸入com.example.bean.User;
輸入org.springframework.stereotype.Controller;
輸入org.springframework.ui.Model;
輸入org.springframework.web.bind.annotation.RequestBody。
輸入org.springframework.web.bind.annotation.RequestMapping。
輸入org.springframework.web.bind.annotation.ResponseBody。
輸入org.springframework.web.servlet.ModelAndView。

輸入javax.servlet.ServletException;
インポートのjavax.servlet.http.HttpServletRequest;
インポートのjavax.servlet.http.HttpServletResponse;
インポートにjava.io.IOException;

@Controller
@RequestMapping( "/応答")
パブリッククラスResponseController {
最終的な静的な文字列= SUCCESS公開"成功";
/ * 1、* /文字列を返す
/ * 2、*戻り無効/
/ * 3、のModelAndViewは、* /返します。
/ * 4、および前方フォワーディングキーワードキーワードリダイレクトリターンリダイレクト。文字列* /
/ * 5、* AJAX呼び出しが戻るJSON /
  / *リターン文字列と、パーサビューでフォームへの復帰をのModelAndViewを返し、ビューリゾルバ* /によって解決されていないキーワードのボイドを使用します

/ * 1、戻り文字列* /
/ *このビューは、パーサーの仕方を渡します、スプライシング接頭辞と接尾辞* /
@RequestMapping(「/たTestString」)
公共の文字列たTestString(モデルモデル){
ユーザーユーザー新しい新しい=ユーザー();
ユーザー。 setName( "ジョン・ドウ");
user.setAge(18である);
model.addAttribute( "ユーザー"、ユーザー)
のSystem.out.println( "試験戻り列");
戻りSUCCESS;
}

/ * 2、戻り空隙* /
@ RequestMapping( "testVoid「)
ボイドtestVoid公開(HttpServletRequestのリクエスト、HttpServletResponseの応答){
するSystem.out.println(「ボイド試験戻り、前方またはユーザに直接出力のリダイレクト、または書き込み利用することができる」);
//フォワード、ビューリゾルバを介して解決されません、しかし、サーバ内のジャンプのために、プロジェクトの名前を書きますが、完全なパス転送記述する必要はありません
/ *試し{
request.getRequestDispatcher(「/ WEB-INF /ページおよび/ success.jsp」)、前方(リクエスト、レスポンス);.
}キャッチ(ServletExceptionがE){
e.printStackTrace();
}キャッチ(IOExceptionをE){
e.printStackTrace();
} * /
//リダイレクト、再送要求がクライアントであることから、それはプロジェクト名を持つ必要があり、直接することはできませんWEB下のコンテンツ-INFへのアクセス
のアクセスに//ので、ここでのindex.jspページwebappの下に、例えば、アドレスバーのアドレスが変更されます
/ *試し{
response.sendRedirect(request.getContextPath()+「/index.jsp」 );
キャッチ}(IOExceptionをE){
e.printStackTrace();
} * /
//クライアントが直接アドレス、すなわち、書かれた内容は新しいページとして表示されます
歪み中国を防止するためには、//、必要な設定は、コードに関連しますコード
試み{
( "UTF-8")response.setCharacterEncoding
response.setContentType( "テキスト/ HTML;のcharset = UTF-8");
。; response.getWriter()印刷( "こんにちは")
にIOException E(キャッチ} ){
e.printStackTrace();
}
}
/ * 3、リターン*のModelAndView /。
//直接リクエストスコープの書き込み可能な文字列の基礎となるビュー名を設定するにオブジェクトを配置します、呼び出し、パーサビュー
! @RequestMapping( "testModelAndView")
公衆のModelAndView testModelAndView(){
System.out.printlnは( "試験戻りのModelAndView")。
ユーザーユーザー新規新しい=ユーザー();
user.setName( "ジョン・ドウ");
user.setAge(18である);
のModelAndViewのModelAndView新しい新規のModelAndView =();
modelAndView.addObject(ユーザ);
modelAndView.setViewName(SUCCESS);
戻りのModelAndView。
}
/ * 4、前方に前進キーとリダイレクトキーワードリダイレクトは、文字列* /返します
//ビューリゾルバがこのように使用されることはありません、彼は正しいパスビュー記述する必要があります
(「testForwardOrRedirect」)をRequestMapping @を
公共の文字列testForwardOrRedirect (){
System.out.printlnは( "テストtestForwardOrRedirect");
//テストフォワード
/ *リターン"フォワード:/WEB-INF/pages/success.jsp"; * /
//テストリダイレクト、コール応答。 sendRedirect()メソッドは、プロジェクト名を追加しない、異なっている
/インデックス:リターン」リダイレクトを。JSP ";
}
/ * 5、* Ajax呼び出しが戻るJSON /
/ * A、組み込まjquery.min.js JSファイルフォルダ、及びファイルがresponseTest.jspに導入されている
* Bは、JSはのために効果的でない試験導入した文書フロントコントローラが遮断され、
<MVC:リソースマッピング= "/ JS /" LOCATION = "/ JS / **" />のようなブロックされないspringmvc.xml内のリソースの静的割り当て*溶液、
* C、 responseTest。非同期メソッドのAJAXの方法に正常にJSPのメソッド呼び出し、
* D、対応する書き込み要求ResponseController方法は、受信@RequestBody場合、文字列パラメータタイプは、必要とされる
受信されたユーザのJSON形式のタイプを使用します*文字列、および関連するJARパッケージ* /の導入のために、JSONページ形式で受信されたメソッド戻り型ユーザデータ
@RequestMapping(「/ testAjax」)
公衆@ResponseBody testAjaxユーザ(ユーザ@RequestBody){
System.out.printlnは( "テストtestAjax ");
System.out.printlnは(ユーザー);
user.setName("ジョン・ドウ「);
user.setAge(50);
戻り値のユーザー。


success.jsp

<% - 
のIntelliJ IDEAによって作成された
ユーザー:管理者
日付:2019年12月4日
時間:夜02時07分
。変更し、このテンプレートファイルを使用するには|設定|ファイルテンプレート
- %>
<% - EL式発効を作るために、 、書き込み= "falseに" isELIgnoredに、このニーズ- %>
<%@ページのcontentType = "テキスト/ HTML;のcharset = UTF-8" LANGUAGE = "Javaの" isELIgnored =%> "偽の"
<HTML>
<HEAD>
<タイトル>タイトル</ TITLE>
</ HEAD>
<BODY>
<H3>成功したエントリ</ H3>
<% - @SessionAttributesコメントをテストするために使用、以下の2つのだけのライン- %>
<% - $ { requestScope.get( "MSG")} <BR>
$ {sessionScope.get( "MSG")} - %>
<% -リターンのModelAndView試験- %>
ユーザー名:$ {user.name} <BR>
パスワード:user.age $ {}
</ BODY>
</ HTML>

springmvc.xml

<?xmlのバージョン= "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"
のxmlns:C = "http://www.springframework.org/schema/c"
のxmlns:キャッシュ=" http://www.springframework.org/schema/cache "
のxmlns:コンテキスト= "http://www.springframework.org/schema/context"
のxmlns:JDBC =" http://www.springframework.org/schema/ JDBC "
のxmlns:JEE = "http://www.springframework.org/schema/jee"
のxmlns:LANG = "http://www.springframework.org/schema/lang"
のxmlns:MVC ="のhttp:// WWW 。springframework.org/schema/mvc」
のxmlns:P = "http://www.springframework.org/schema/p"
xmlns:タスク= "http://www.springframework.org/schema/task"
のxmlns:TX = "http://www.springframework.org/schema/tx"
のxmlns:UTIL = "のhttp://www.springframework .ORG /スキーマ/ utilの"
XSI:のschemaLocation =" http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.3.xsd
のhttp:// http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd www.springframework.org/schema/mvc
http://www.springframework.org/schema/contextのhttp://www.springframework .ORG /スキーマ/コンテキスト/スプリングコンテキスト4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/util/spring-util-4.3.xsd http://www.springframework.org/schema/util
http://www.springframework.org/schema/jdbcのhttp:/ /www.springframework.org/schema/jdbc/spring-jdbc-4.3.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.3 .xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.3.xsd
http://www.springframework.org/schema/beans HTTP ://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-4.3 .xsd
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd http://www.springframework.org/schema/tx「>
<! -オープンコメントスキャン- >
<コンテキスト:コンポーネントベース・パッケージ= -scan "com.example" />
<! - > -ビューリゾルバは、コントローラがファイルを返し、対応する文字列を見つける
の<beanのID = "InternalResourceViewResolver"クラス=「org.springframework.webを。 servlet.view.InternalResourceViewResolver ">
<! -ファイルパス- >
<プロパティ名="接頭辞"値=" / WEB-INF /ページ/ "/>
<! -ファイル拡張子- >
<プロパティ名="サフィックス"値=" JSP "/>。
</豆>
<! -のconfigureカスタム型コンバータ- >
<豆ID =" conversionServiceFactoryBean "クラス=" ORG。springframework.context.support.ConversionServiceFactoryBean ">
<プロパティ名="コンバーター">
<SET>
<beanクラス= "com.example.util.StingToDateConvertr" />
</ SET>
</プロパティ>
</豆>

<! - 。1、フレームspringmvcアノテーションをサポートするオープン- >
<! - 2一緒に変換サービスとの効果にのconfigureカスタム型コンバータと主張するが、必要な属性- >  
<MVCは:アノテーション駆動型変換サービス=「conversionServiceFactoryBean」/>

<! - JSファイル、JSPページもご紹介します切片に構成された静的リソースファイルではありません傍受、フロントコントローラ- >
<MVC:リソースマッピング= "/ JS /" LOCATION = "/ JS / **" />
<MVC:リソースマッピング= "/ CSS /" LOCATION = "/ CSS / **" />
</豆>

pom.xml

<?xmlのバージョン= "1.0"エンコード= "UTF-8"> 

<プロジェクトのxmlns = "http://maven.apache.org/POM/4.0.0"のxmlns:XSI = "のhttp://www.w3 .ORG / 2001 / XMLスキーマ・インスタンス」
のxsi:のschemaLocation = "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion> 4.0.0 </ modelVersion>

<groupIdを> com.example </ groupIdを>
<たartifactId> springmvc_day01_01_start </たartifactId>
<バージョン> 1.0-SNAPSHOT </バージョン>
<パッケージ>戦争</パッケージ>

<名前> springmvc_day01_01_start MavenののWebapp </名前>
<! -プロジェクトのウェブサイトにFIXME変更それを- >
<URL> http://www.example.com </ URL>

<プロパティ>
<project.build.sourceEncoding> UTF-8 </project.build.sourceEncoding>
<maven.compiler.source> 1.7 </maven.compiler.source>
<maven.compiler.target> 1.7 </maven.compiler.target>
<spring.version> 5.0.2.RELEASE </spring.version>
</プロパティ>

<依存性>
<! -添加春、springmvc依赖- >
<! - AOP - >
<依存>
<groupIdを> org.springframework </ groupIdを>
<たartifactId>春-AOP </たartifactId>
<バージョン> $ {spring.version} </バージョン>
</依存関係>

<! -側面- >
<依存>
<groupIdを> org.springframework </ groupIdを>
<たartifactId>春-側面</たartifactId>
<バージョン> $ {spring.version} </バージョン>
</依存関係>
<! -豆- >
<依存>
<groupIdを> org.springframework </ groupIdを>
<たartifactId>春豆</たartifactId>
<バージョン> $ {spring.version} </バージョン>
</依存関係>
<! -コンテキスト- >
<依存関係>
<のgroupId> org.springframework </のgroupId>
<たartifactId>ばねコンテキスト</たartifactId>
<バージョン> $ {spring.version} </バージョン>
</依存>
<! -コア- >
<依存性>
< groupId> org.springframework </のgroupId>
<たartifactId>ばねコア</たartifactId>
<バージョン> $ {spring.version} </バージョン>
</依存関係>
<! -表現- >
<依存>
<groupIdを> org.springframework </ groupIdを>
<たartifactId>春表現</たartifactId>
<バージョン> $ {spring.version} </バージョン>
</依存関係>
<! - ORM - >
<依存>
<groupIdを> org.springframework </ groupIdを>
<たartifactId>春-ORM </たartifactId>
<バージョン> $ {spring.version} </バージョン>
</依存関係>
<! - JDBC - >
<依存>
<groupIdを>組織.springframework </ groupIdを>
<たartifactId>春-JDBC </たartifactId>
<バージョン> $ {spring.version} </バージョン>
</依存関係>
<!-テスト- >
<依存>
<groupIdを> org.springframework </ groupIdを>
<たartifactId>ばね試験</たartifactId>
<バージョン> $ {spring.version} </バージョン>
</依存>
<! - TX - >
<依存性>
<のgroupId> org.springframework </のgroupId>
<たartifactId >春-TX </たartifactId>
<バージョン> $ {spring.version} </バージョン>
</依存関係>
<! -ウェブ- >
<依存>
<groupIdを> org.springframework </ groupIdを>
<たartifactId>春-web </たartifactId>
<バージョン> $ {spring.version} </バージョン>
</依存関係>
<! - webmvc - >
<依存>
<groupId> org.springframework </のgroupId>
<たartifactId>ばねwebmvc </たartifactId>
<バージョン> $ {spring.version} </バージョン>
</依存関係>
<! -添加春、springmvc依赖- >

<! -用于热部署- >
<依存>
<groupIdを> org.springframework。ブート</ groupIdを>
<たartifactId>春・ブート・デベロッパーツール</たartifactId>
</依存関係>
<! -引入ジャクソン相关的3个核心包- >
<依存>
<groupIdを> com.fasterxml.jackson.core < /のgroupId>
<たartifactId>ジャクソンデータバインド</たartifactId>
<バージョン> 2.9.0 </バージョン>
</依存>
<依存性>
<のgroupId> com.fasterxml.jackson.core </のgroupId>
<たartifactId>ジャクソン・コア</たartifactId>
<バージョン> 2.9.0 </バージョン>
</依存関係>
<依存>
<groupIdを> com.fasterxml.jackson.core </ groupIdを>
<たartifactId>ジャクソンの注釈</たartifactId>
<バージョン> 2.9.0 </バージョン>
</依存関係>


</依存関係>

<ビルド>
<finalName> springmvc_day01_01_start < / finalName>
<pluginManagement> <! - Mavenのデフォルトを使用しないようにするプラグインのバージョンダウンロック(親POMに移動することができる) - >
<プラグイン>
<プラグイン>
<たartifactId>のmaven-クリーン・プラグイン</たartifactId>
<バージョン> 3.1.0 </バージョン>
</プラグイン>
<! - http://maven.apache.org/ref/current/maven-core/default-bindingsを参照してください。htmlの#Plugin_bindings_for_war_packaging - >
<プラグイン>
<たartifactId>のmaven-リソース・プラグイン</たartifactId>
<バージョン> 3.0.2 </バージョン>
</プラグイン>
<プラグイン>
<たartifactId>のmaven-コンパイラプラグイン</たartifactId>
<バージョン> 3.8.0 </バージョン>
</プラグイン>
<プラグイン>
<たartifactId>のmaven-確実な-プラグイン</たartifactId>
<バージョン> 2.22.1 </バージョン>
</プラグイン>
<プラグイン>
<たartifactId> Mavenの戦争-プラグイン</たartifactId>
<バージョン> 3.2.2 </バージョン>
</プラグイン>
<プラグイン>
<たartifactId>Mavenのインストール-プラグイン</たartifactId>
<バージョン> 2.5.2 </バージョン>
</プラグイン>
<プラグイン>
<たartifactId>のmaven-展開-プラグイン</たartifactId>
<バージョン> 2.8.2 </バージョン>
</プラグイン>
</プラグイン>
</ pluginManagement>
</ビルド>
</プロジェクト>

 

それを理解していない場合は、正しい私を願っています!



おすすめ

転載: www.cnblogs.com/xiao1572662/p/12002873.html