3

I have a problem with method handler return value in spring mvc I have a grid and when user click on a row I have to show the details of row. I don't want to send row Id in URL,when I use @PathVariable everything work perfect

I used jqGrid in my jsp to get the Id of selected row

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

my controller is :

    @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)

public String show(Authentication authentication, @PathVariable("id") int id, Model uiModel, Locale locale) {

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;

}

my apache tiles config for load detail page:

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

and my 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>

and anything work perfect,but I have to send my data in request object instead of in URL

my code in jsp :

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

and my method handler:

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

//@ResponseBody public String showSelectedCustomer( Authentication authentication, @RequestParam String id, Model uiModel, Locale locale) {

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";

}

my method handler is called but after that nothing is happened. my point is showCustomerData.jspx that show a details of customer isn't loaded.

please let me know what should I do and what my problem is? when I use my data with Ajax,how should I return my return value to see detail page (showCustomerData.jspx) when I change my method handler to return a map with map.put("message", "success") data,my ajax success function is called but I don't know how can I redirect it to show my details page

thank you

4

1 回答 1

0

您首先需要创建 CustomerInput pojo,如下所示

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

然后创建如下控制器方法

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

}
于 2015-03-05T08:56:48.827 回答