现在的位置: 首页 > 综合 > 正文

spring页面转发和参数传递

2013年08月20日 ⁄ 综合 ⁄ 共 3676字 ⁄ 字号 评论关闭
                               
1.最简单的action页面提交和参数p的传递
     

       <form:form name="batchForm" method="post" action="/video/list.html">
       
<input type="hidden" name="p" value="<c:out value="${videoList.p}"/>"/>//从action取得p的值
     </form:form>
      
public ModelAndView list(HttpServletRequest request,HttpServletResponse response) {
        
int page = ServletRequestUtils.getIntParameter(request, "p"1);//通过超连接取得p的参数值
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put(
"p", page);
        
return new ModelAndView(listView, "videoList", map);
     }

2.在javascript里面实现不同连接的表单提交
 

<form id="messageForm" name="messageForm" method="post" onSubmit="return
      onMessageFormSubmit(<c:out  value=
"${param.type}"/>)"> 
       
<input type="hidden" name="method" value="delete"/>
       
<input type="hidden" name="type" value="<c:out value="${param.type}"/>" />
    
</form>
    
<script type="text/javascript">
        function onMessageFormSubmit(type) 
{
            
if(type == 0{
                document.messageForm.action 
= '/commend/special/list.html';
            }

            
if(type == 1){
                document.messageForm.action 
= '/special/list.html';
            }

         }

         Form.focusFirstElement($(
'messageForm'));
  
</script>

3.通过编辑页面的提交给action="/special/edit.html"既对应的onsubmit方法返回给前一页面,注意删除按钮不应提交
 

<c:set var="buttons">
<input type="submit" class="button" name="audited" onclick="bCancel=false" value="<fmt:message key
    =
"button.audited"/>"/>
<input type="button class="button" onclick="location.href='/special/list.html?method=find&from=edit'"
     value="<fmt:message   key="button.delete"/>"/>
<input type="submit" class="button" name="cancel" onclick="bCancel=true" value="<fmt:message
      key=
"button.back"/>"/>
 
</c:set>
 
<form:form commandName="special" method="post" action="/special/edit.html" id="specialForm" >
    
<input type="hidden" name="type" value="<c:out value="${param.type}" />" />
    
<class="buttonBar bottom">
        
<c:out value="${buttons}" escapeXml="false" />
    
</p>
  
</form:form>

4.jsp使用request.getAttribute()方法接收传递过来的special参数对象
  

  public ModelAndView find(HttpServletRequest request,
            HttpServletResponse response) 
{
        
if (log.isDebugEnabled()) {
            log.debug(
"entering 'find' method...");
        }

        
int specialId = ServletRequestUtils.getIntParameter(request,
                
"specialId"-1);
        Special special 
= specialManager.findBySpecialId(specialId);
        
return new ModelAndView(findView, "special", special);
  }

    
<%
      
if(null == request.getAttribute("special") )
        
{
    
%>
    
<script type="text/javascript">
    alert(
'<fmt:message key="special.status.conflict"/>');
     location.href
='/special/list.html?type=<%=ServletRequestUtils.getIntParameter(request, "type",0)%>
     &p=<%=ServletRequestUtils.getIntParameter(request, "p", 1)%>
';
    
</script>
     
<%     
        
return ;
    }
%>

5.此时的param是有controller传递过来的参数别名,即spring自带的参数.
   

<form:form commandName="special"  method="post" id="specialForm" name="specialForm"
     onsubmit
="return  validateSpecial(this)">
     
<input type="hidden" name="type" value="<c:out value="${param.type}" />" />
      
<input type="hidden" name="p" value="<c:out value="${param.p}" />" />
</form:form>

6.通过controller传递参数,得到hashMap所封装的对象.
  

public ModelAndView list(HttpServletRequest req, HttpServletResponse res) {
    HashMap
<String,Object> videoRelationsMap = new HashMap<String,Object>();
        videoRelationsMap.put(
"video", video);
       
return new ModelAndView(view,"videoRelationsList",videoRelationsMap);
}

<% Videos videos = (Videos)((HashMap)request.getAttribute("videoRelationsList")).get("video");%>

抱歉!评论已关闭.