一般的に使用されるWeb要素には、リクエスト、セッション、アプリケーションなどが含まれます。通常、セッションをさらに使用します。 Struts2のWeb要素にアクセスする方法は?これは、プログラムの背景とユーザーの間のデータ相互作用を完了できるため、非常に重要です。以下は、そのプロセスを実証するための登録例です。
1。INDEX.JSPファイル
<%@ページ言語= "java" contentType = "text/html; charset = utf-8" 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" "http://www.w3.org/tr/html4/loose.dtd"> <html> <head> <meta http-equiv = "content-type" contep-type "/"/"/utpe"/ <base href="<%=basePath %>"/> <title>Insert title here</title> </head> <body> <h1>Demo</h1> <form action="user/user02!register" method="post"> Name: <input type="text" name="user.name"></input> <br/> Password: <input type="text" name="user.password"></input> <br/> <入力型= "submit" value = "Register"/> </form> </body> </html>
関数は非常にシンプルです。つまり、ユーザーはユーザー名とパスワードを入力し、バックグラウンドで取得でき、登録が成功した後にユーザーに表示されます。
2。Struts.xml構成
<?xml version = "1.0" encoding = "utf-8"?> <!doctype struts public " - // apacheソフトウェア財団// dtd struts configuration 2.0 // en" "http://struts.apache.org/dtds/struts-2.0.dtd" name = "front" namespace = "/user" extends = "struts-default"> <action name = "user*"> <resuld>/success.jsp </result> <result name = "error">/error.jsp </result> </action> </package> </struts>
この関数を完了するには、2つの方法があります
3。最初のタイプ(useraction01)
パッケージcom.myservice.web; java.util.mapをインポートします。 com.opensymphony.xwork2.actioncontextをインポートします。 com.opensymphony.xwork2.actionsupportをインポートします。 public class useraction01はactionsupport { / ** * * / private static final long serialversionuid = 1l;を拡張します。プライベートユーザーユーザー;プライベートマップリクエスト。プライベートマップセッション。プライベートマップアプリケーション。 public useraction01(){request =(map)ActionContext.getContext()。get( "request"); session = actioncontext.getContext()。getSession(); Application = actionContext.getContext()。getApplication(); } public String Register(){request.put( "name"、user.getName()); request.put( "password"、user.getPassWord());成功を返す; } public user getUser(){return user; } public void setuser(user user){this.user = user; }}この方法は、actioncontext.getContext()メソッドを使用してコンテキストを取得し、リクエスト、セッション、アプリケーションを取得することです。
4.別の方法(useraction02)は非常に一般的で非常に有名です - IOC(コントロール反転)およびDI(依存関係噴射)。次のように、3つのインターフェイスの実装が必要です。
パッケージcom.myservice.web; java.util.mapをインポートします。 org.apache.struts2.interceptor.applicationawareをインポートします。 org.apache.struts2.interceptor.requestawareをインポートします。 Import org.apache.struts2.interceptor.sessionaware; com.opensymphony.xwork2.actionsupportをインポートします。 public class useraction02 extends ActionSupport RequestAware、SessionAware、ApplicationAware {private Map <String、object> request;プライベートマップ<文字列、オブジェクト>セッション;プライベートマップ<文字列、オブジェクト>アプリケーション;プライベートユーザーユーザー; public user getUser(){return user; } public void setuser(user user){this.user = user; } public String Register(){request.put( "name"、user.getName()); request.put( "password"、user.getPassWord());成功を返す; } @Override public void setApplication(map <string、object> application){// todo auto-enerated method stub this.application = application; } @Override public void setSession(map <string、object> session){// todo auto-enerated method stub this.session = session; } @Override public void setRequest(map <string、object> request){// todo auto-fenated method stub this.request = request; }}これにより、関数が実装されます - ユーザーの名前とパスワードをリクエストに入れ、それを使用するときは、それを取り出す必要があります。
5。success.jspはリクエストの内容を取り出して表示します
<%@ page Language = "Java" contentType = "text/html; charset = utf-8" pageencoding = "utf-8"%> <%@ taglib uri = "/struts-tags" prefix = "s"%> <!doctype html public " - // "http://www.w3.org/tr/html4/loose.dtd"> <html> <head> <meta http-equiv = "content-type" content = "text/html; charset = utf-8"> <タイトル>タイトル</head < value = "#request.name"/>パスワードで正常に登録されています:<s:property value = "#request.password"/> </body> </html>
結果は次のように表示されます。
上記は、struts2のWeb要素にアクセスするすべてのコンテンツです。参照を提供できることを願っています。wulin.comをもっとサポートできることを願っています。