2016-05-15 5 views
0

JSPとサーブレットとNetBeans IDEの使用。JSTLとhref渡しパラメータ

私は以下のようにhrefを通してパラメータを渡します。

<a href="coursematerial.jsp?param1=${row.COURSEID}"><c:out value="${row.COURSEID}"/> 

は今、次のJSPでは、私はそれを行うと、フォームの隠し入力に保存されたパラメータを送信することができ、私はpassed.howパラメータを保存したいですか? iが

<input type="hidden" name="courseid" value=<%= request.getParameter("param1")%> > 

を使用して試みたが、フォームが充填される前に実行される別のHREFがあるため、値がNULLとして取得されます。

必要なパラメータをセッション(スクリプトなしでJSTLを使用する)として保存し、hrefとして渡さずに取得する方法はありますか?

+0

あなたはの sawyinwaimon

答えて

0

jstlを使用しているかどうかはわかりません。しかし、あなたはフラッシュスコープを使用することができます。その属性は、あなたの要件に応じてまだ次のJSPになります。 この簡単なコードを使用して、フラッシュ範囲を実装します。例えば

、単に要求を追加するための定期的な構文を使用し、スコープを点滅するメッセージを追加するには、属性をスコープとfiterはそれの世話をしてみましょう:

request.setAttribute("flash.message", "Here is the news..."); 

上記の属性が、その後にアクセスすることができますリダイレクト先JSPは( 'フラッシュ。' プレフィックスを省略)ELまたはスクリプトレットの構文を使用して:

import javax.servlet.*; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpSession; 
import java.io.IOException; 
import java.util.Enumeration; 
import java.util.HashMap; 
import java.util.Map; 

/** 
* Ensures that any request parameters whose names start 
* with 'flash.' are available for the next request too. 
*/ 
public class FlashScopeFilter implements Filter { 

    private static final String FLASH_SESSION_KEY = "FLASH_SESSION_KEY"; 

    @SuppressWarnings("unchecked") 
    public void doFilter(ServletRequest request, ServletResponse response, 
        FilterChain chain) throws IOException, ServletException { 

     //reinstate any flash scoped params from the users session 
     //and clear the session 
     if (request instanceof HttpServletRequest) { 
      HttpServletRequest httpRequest = (HttpServletRequest) request; 
      HttpSession session = httpRequest.getSession(false); 
      if (session != null) { 
       Map<String, Object> flashParams = (Map<String, Object>) 
            session.getAttribute(FLASH_SESSION_KEY); 
       if (flashParams != null) { 
        for (Map.Entry<String, Object> flashEntry : flashParams.entrySet()) { 
         request.setAttribute(flashEntry.getKey(), flashEntry.getValue()); 
        } 
        session.removeAttribute(FLASH_SESSION_KEY); 
       } 
      } 
     } 

     //process the chain 
     chain.doFilter(request, response); 

     //store any flash scoped params in the user's session for the 
     //next request 
     if (request instanceof HttpServletRequest) { 
      HttpServletRequest httpRequest = (HttpServletRequest) request; 
      Map<String, Object> flashParams = new HashMap(); 
      Enumeration e = httpRequest.getAttributeNames(); 
      while (e.hasMoreElements()) { 
       String paramName = (String) e.nextElement(); 
       if (paramName.startsWith("flash.")) { 
        Object value = request.getAttribute(paramName); 
        paramName = paramName.substring(6, paramName.length()); 
        flashParams.put(paramName, value); 
       } 
      } 
      if (flashParams.size() > 0) { 
       HttpSession session = httpRequest.getSession(false); 
       session.setAttribute(FLASH_SESSION_KEY, flashParams); 
      } 
     } 
    } 

    public void init(FilterConfig filterConfig) throws ServletException { 
     //no-op 
    } 

    public void destroy() { 
     //no-op 
    } 
} 

${message} 
or 
<%= request.getAttribute("message") %> 

ここでフィルタのコードですparamの名前= "PARAM" 値= "<%= request.getParameter( 'のparam1')%> JSPのforwardアクションで:

http://smartkey.co.uk/development/implementing-flash-scope-in-java-web-applications/

関連する問題