ModelAndView返回json,Extjs无法找到success。

qq_17077003 2015-08-11 11:46:52
viewspace-servlet.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:annotation-config />
<context:component-scan base-package="com.my.pms.action"></context:component-scan>

<!-- 避免IE执行AJAX时,返回JSON出现下载文件 -->
<bean id="mappingJacksonHttpMessageConverter"
class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
</list>
</property>
</bean>

<!-- ContentNegotiatingViewResolver视图解析器,利用他就可以配置多种返回值 -->
<bean
class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">

<!-- 这里是否忽略掉accept header,默认就是false -->
<property name="ignoreAcceptHeader" value="true" />

<!-- 如果所有的mediaType都没匹配上,就会使用defaultContentType -->
<property name="defaultContentType" value="text/html" />

<property name="mediaTypes">
<map>
<entry key="json" value="application/json" />
</map>
</property>
<!-- 默认使用MappingJacksonJsonView生成jsonview -->
<property name="defaultViews">
<list>
<bean
class="org.springframework.web.servlet.view.json.MappingJacksonJsonView">
<property name="extractValueFromSingleKeyModel" value="true" />
</bean>
</list>
</property>
</bean>

<!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="mappingJacksonHttpMessageConverter" />
</list>
</property>
</bean>

<!-- 配置视图解析器,将ModelAndView及字符串解析为具体的页面 如视图逻辑名为"login"将解析为/WEB-INF/jsp/login.jsp -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:viewClass="org.springframework.web.servlet.view.JstlView" p:prefix="/pages/"
p:suffix=".jsp" />
</beans>


LoginAction.java
import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.my.pms.model.users;
import com.my.pms.service.UsersService;

@Controller
@RequestMapping(value = "/loginAction")
public class LoginAction {
@RequestMapping(value = "/index.action")
@ResponseBody
public ModelAndView indexAction(HttpServletRequest request,
HttpServletResponse response) throws IOException {
ModelAndView mv = new ModelAndView();
System.out.println("登陆");
String loginname = request.getParameter("username");
String password = request.getParameter("password");
List<users> list = usersService.findByLogin(loginname, password);
if(list.size()==1){
users u = list.get(0);
HttpSession session = request.getSession();
session.setAttribute("user", u);
mv.addObject("success", true);
mv.addObject("msg", "成功");
}else{
mv.addObject("success", false);
mv.addObject("msg", "失败");
}
return mv;
}
@Resource(name = "usersService")
private UsersService usersService;

}


login.js
this.up('form').submit({
url:'loginAction/index.action',
waitMsg : '正在提交数据',
waitTitle : '提示',
success : function(form, action) {
alert('success');
},
failure : function(form, action) {
alert('failure');
}
});

返回的json:
{"view":null,"model":{"success":true,"msg":"成功"},"empty":false,"viewName":"../app","reference":true,"modelMap":{"success":true,"msg":"成功"}}


在js接收的时候找不到success。

是不是我配置出问题了,请大神帮忙解决!
...全文
101 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
伊Men 2015-08-11
  • 打赏
  • 举报
回复
@Controller @RequestMapping(value = "/loginAction") public class LoginAction { @RequestMapping(value = "/index.action") /loginAction/index.action 加上完整域名试试吧……
伊Men 2015-08-11
  • 打赏
  • 举报
回复
@ResponseBody 把这个注解去掉试试吧……

87,954

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧