2013-01-05 180 views
3

我有方法處理返回值的問題,在Spring MVC中 我有一個網格,當一排我必須表明行的詳細信息,用戶點擊。 我不想送行ID的URL,當我使用@PathVariable一切工作完美發送一個Ajax請求的Spring MVC

我使用的jqGrid在我的JSP中獲得所選行的id

onSelectRow: function(id) { 
    document.location.href = "/customer/" + id; 
} 

我的控制器:

@RequestMapping("/customer") 
@Controller 
public class CustomerController { 

    final Logger logger = LoggerFactory.getLogger(CustomerController.class); 

    @Autowired 
    private CustomerService customerService; 

    @Autowired 
    MessageSource messageSource; 


@RequestMapping(value = "/{id}", method = RequestMethod.GET) 

公共字符串顯示(驗證認證,@PathVariable( 「ID」)INT ID,型號uiModel,語言環境的語言環境){

User user = (User) authentication.getPrincipal(); 
String result = null; 
try { 
    if (user != null) { 
     if (user.getRole().getRoleType().equalsIgnoreCase("ADMIN")) { 
      Customer customer = customerService.getCustomerById(id); 
      uiModel.addAttribute("customer", customer); 
      result = "customer/show"; 
     } else 
      result = "/customer/all"; 
    } 
} catch (Exception e) { 
    uiModel.addAttribute("message", new Message("error", 
      messageSource.getMessage("customer_get_all_fail", new Object[]{}, locale))); 
    e.getStackTrace(); 

} 
return result; 

}

我的Apache配置瓦負載詳細信息頁面:

<definition extends="default" name="customer/show"> 
<put-attribute name="body" 
    value="/WEB-INF/views/customer/showCustomerData.jspx" /> 

和我的servelt-context.xml的

 <mvc:annotation-driven/> 
    <mvc:resources location="/, classpath:/META-INF/web-resources/" mapping="/resources/**"/> 

    <tx:annotation-driven/> 
    <context:annotation-config/> 
    <mvc:default-servlet-handler/> 

    <mvc:interceptors> 
     <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" p:paramName="lang"/> 
    </mvc:interceptors> 

    <bean class="org.springframework.context.support.ReloadableResourceBundleMessageSource" id="messageSource" 
      p:basenames="WEB-INF/i18n/messages,WEB-INF/i18n/application" p:fallbackToSystemLocale="false"/> 


    <bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"> 
     <property name="validationMessageSource" ref="messageSource"/> 
    </bean> 


    <context:component-scan base-package="com.sefryek.sywf.web.controller"/> 

    <!-- Enable file upload functionality --> 
    <!--<bean class="org.springframework.web.multipart.support.StandardServletMultipartResolver" id="multipartResolver"/>--> 

    <!-- Configure the multipart resolver --> 
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> 
     <!-- one of the properties available; the maximum file size in bytes --> 
     <property name="maxUploadSize" value="10000000"/> 
    </bean> 

    <bean id="contentNegotiatingResolver" 
      class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver"> 
     <property name="order" 
        value="#{T(org.springframework.core.Ordered).HIGHEST_PRECEDENCE}"/> 
     <property name="mediaTypes"> 
      <map> 
       <entry key="html" value="text/html"/> 
       <entry key="pdf" value="application/pdf"/> 
       <entry key="xsl" value="application/vnd.ms-excel"/> 
       <entry key="xml" value="application/xml"/> 
       <entry key="json" value="application/json"/> 
       <entry key="js" value="application/javascript"/> 
      </map> 
     </property> 
    </bean> 

    <!--<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"--> 
    <!--p:prefix="/WEB-INF/views/" p:suffix=".jsp" p:order="#{contentNegotiatingResolver.order+0}"/>--> 

    <bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver" 
      p:cookieName="locale" p:cookieMaxAge="11"/> 

    <!-- Tiles Configuration --> 
    <bean class="org.springframework.web.servlet.view.UrlBasedViewResolver" id="tilesViewResolver"> 
     <property name="viewClass" value="org.springframework.web.servlet.view.tiles2.TilesView"/> 
    </bean> 

    <bean class="org.springframework.web.servlet.view.tiles2.TilesConfigurer" id="tilesConfigurer"> 
     <property name="definitions"> 
      <list> 
       <value>/WEB-INF/layouts/layouts.xml</value> 
       <!-- Scan views directory for Tiles configurations --> 
       <value>/WEB-INF/views/**/views.xml</value> 
      </list> 
     </property> 
    </bean> 

    <bean id="themeResolver" class="org.springframework.web.servlet.theme.CookieThemeResolver" 
      p:cookieName="theme" p:defaultThemeName="standard"/> 

    <bean id="themeSource" class="org.springframework.ui.context.support.ResourceBundleThemeSource"/> 
</beans> 

和任何工作完美,但我有在請求對象的URL發送給我的數據,而不是

我的代碼 在JSP:

onSelectRow: function(id) { 
    $.ajax({ 
     url : '${showSelectedCustomer}', 
     dataType: 'JSON', 
     data:{ id: id }, 
     type:'POST', 
     success: function(response) { 
     } , 
     error : function(r) { 
     } 
    }); 
} 

和我的方法處理:

@RequestMapping(value = "/showSelectedCustomer", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE) 

// @ ResponseBody 公共字符串showSelectedCustomer( 認證的認證, @RequestParam串ID, 型號uiModel,語言環境區域設置){

User user = (User) authentication.getPrincipal(); 
Customer customer = null; 
Map<String, Object> map = new HashMap<String, Object>(); 
try { 
    if (user != null) { 
     if (user.getRole().getRoleType().equalsIgnoreCase("ADMIN")) { 
      customer = customerService.getCustomerById(Integer.valueOf(id)); 
      uiModel.addAttribute("customer", customer); 
     } 
    } 
} catch (Exception e) { 
    uiModel.addAttribute("message", new Message("error", 
      messageSource.getMessage("customer_get_all_fail", new Object[]{}, locale))); 
    e.getStackTrace(); 

} 
return "customer/show"; 

}

我的方法處理程序被調用但在這之後任何反應。 我的觀點是showCustomerData.jspx,顯示客戶的細節沒有被加載。

請讓我知道我該怎麼做,我的問題是什麼? 當我使用我的數據與Ajax時,我應該如何返回我的返回值以查看詳細信息頁(showCustomerData.jspx) 當我更改我的方法處理程序以返回具有map.put(「message」,「success」)數據的地圖,我的ajax成功函數被調用,但我不知道我怎麼可以把它重定向顯示我的詳細信息頁面

謝謝

+1

我不明白爲什麼你需要在AJAX中做到這一點。這很奇怪,你想做一個AJAX調用,將用戶重定向到另一個頁面......「但我必須在請求對象而不是URL中發送我的數據」爲什麼? –

+0

你的控制器方法應該返回JSON而不是一個jsp視圖。然後在您的Ajax成功回調中,您應該閱讀該JSON並使用JavaScript通過新值填充頁面。 – nickdos

+1

我只想給我的方法處理程序發送一個json,如果enything沒問題,那麼tile會向客戶顯示另一個頁面 – farhad

回答

0

您首先需要創建CustomerInput POJO像下面

class CustomerInput{ 
private Integer id; 
//setter getter 
} 

然後創建像控制器方法下面

@ResponseBody public String showSelectedCustomer(Authentication authentication, @RequestBody CustomerInput customerInput, Model uiModel, Locale locale) { 

}