gpt4 book ai didi

ajax - 向 Spring MVC 发送 Ajax 请求

转载 作者:行者123 更新时间:2023-12-02 05:03:23 24 4
gpt4 key购买 nike

我在 spring mvc 中遇到方法处理程序返回值的问题我有一个网格,当用户单击一行时,我必须显示该行的详细信息。我不想在 URL 中发送行 ID,当我使用 @PathVariable 时一切正常

我在我的 jsp 中使用 jqGrid 来获取所选行的 Id

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

我的 Controller 是:

    @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 tiles 配置:

<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)字符串 showSelectedCustomer( 认证认证, @RequestParam 字符串id, 模型 uiModel,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)当我更改我的方法处理程序以返回带有 map.put("message", "success") 数据的 map 时,我的 ajax 成功函数被调用,但我不知道如何重定向它以显示我的详细信息页面

谢谢

最佳答案

您首先需要像下面这样创建 CustomerInput pojo

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

然后像下面这样创建 Controller 方法

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

}

关于ajax - 向 Spring MVC 发送 Ajax 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14174103/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com