3

spring mvc のメソッド ハンドラの戻り値に問題があります。グリッドがあり、ユーザーが行をクリックすると、行の詳細を表示する必要があります。@PathVariable を使用すると、URL で行 ID を送信したくありません。すべてが完璧に機能します。

jspでjqGridを使用して、選択した行の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)

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;

}

ロード詳細ページの私の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 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";

}

メソッド ハンドラが呼び出されますが、その後は何も起こりません。私のポイントは、顧客の詳細が読み込まれていないことを示す showCustomerData.jspx です。

どうすればいいのか、何が問題なのか教えてください。データを Ajax で使用する場合、戻り値を返して詳細ページ (showCustomerData.jspx) を表示する方法ajax 成功関数が呼び出されましたが、それをリダイレクトして詳細ページを表示する方法がわかりません

ありがとうございました

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 に答える