求助一个springmvc返回json字符串的问题
展开全部
我的springmvc工程中,有的action想让返回到一个jsp页面,有的action是ajax发送的请求,想让返回一个json字符串。
但是现在有下面几个问题:
1、post请求,使用ajax发请求,返回不了json字符串,总是返回一个jsp页面内容(get请求可以)
2、使用ajaxfileupload.js中提供的上传文件ajaxFileUpload方法上传一个图片,想让返回个json字符串来标识结果以及图片ID。但是总是返回一个jsp的页面内容。
我的spring配置文件如下:
Plain Text code?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="mediaTypes">
<map>
<entry key="html" value="text/html"/>
<entry key="spring" value="text/html"/>
<entry key="json" value="application/json"/>
</map>
</property>
<property name="viewResolvers">
<list>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</list>
</property>
<property name="defaultViews">
<list>
<bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView"/>
</list>
</property>
</bean>
action中的写法:
Java code?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
@RequestMapping(value = "uploadPreviewPhoto", method=RequestMethod.POST)
public ModelAndView uploadPreviewPhoto (HttpSession session, HttpServletRequest request){
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
MultipartFile photo = multipartRequest.getFile("uploadFileName");
InputStream is = null;
ModelAndView mv = new ModelAndView();
if (photo.getSize() > 1048576)
{
mv.addObject("result", "failed");
mv.addObject("reason", "上传图片必须小于1M.");
return mv;
}
byte[] photoData = new byte[(int) photo.getSize()];
try {
is = photo.getInputStream();
is.read(photoData);
} catch (IOException e) {
mv.addObject("result", "failed");
mv.addObject("reason", "文件读取失败");
return mv;
}
finally
{
if (is != null)
{
try {
is.close();
} catch (IOException e) {
mv.addObject("result", "failed");
mv.addObject("reason", "服务异常");
return mv;
}
}
}
mv.setViewName("login/uploadAvatar");
mv.addObject("result", "success");
session.setAttribute("photoData", photoData);
return mv;
}
js中的写法:
JavaScript code?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
function ajaxFileUpload (){
$.ajaxFileUpload ({
url :'photo/uploadPreviewPhoto.do',
type:"POST",
secureuri :false,
accept:"application/json",
fileElementId :'uploadFileName',
success : function (data, status){
// 此处的data是个HTMLObject,我其实想要个json字符串
document.getElementById("previewimg").src=path + "photo/getPreviewPhoto.do?width=0&height=0";
},
error: function (data, status, e){
alert(e);
}
})
return false;
}
我把请求后缀换成了.json,这样spring就知道是要解析成json了。
其他地方继续用.do
但是现在有下面几个问题:
1、post请求,使用ajax发请求,返回不了json字符串,总是返回一个jsp页面内容(get请求可以)
2、使用ajaxfileupload.js中提供的上传文件ajaxFileUpload方法上传一个图片,想让返回个json字符串来标识结果以及图片ID。但是总是返回一个jsp的页面内容。
我的spring配置文件如下:
Plain Text code?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="mediaTypes">
<map>
<entry key="html" value="text/html"/>
<entry key="spring" value="text/html"/>
<entry key="json" value="application/json"/>
</map>
</property>
<property name="viewResolvers">
<list>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</list>
</property>
<property name="defaultViews">
<list>
<bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView"/>
</list>
</property>
</bean>
action中的写法:
Java code?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
@RequestMapping(value = "uploadPreviewPhoto", method=RequestMethod.POST)
public ModelAndView uploadPreviewPhoto (HttpSession session, HttpServletRequest request){
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
MultipartFile photo = multipartRequest.getFile("uploadFileName");
InputStream is = null;
ModelAndView mv = new ModelAndView();
if (photo.getSize() > 1048576)
{
mv.addObject("result", "failed");
mv.addObject("reason", "上传图片必须小于1M.");
return mv;
}
byte[] photoData = new byte[(int) photo.getSize()];
try {
is = photo.getInputStream();
is.read(photoData);
} catch (IOException e) {
mv.addObject("result", "failed");
mv.addObject("reason", "文件读取失败");
return mv;
}
finally
{
if (is != null)
{
try {
is.close();
} catch (IOException e) {
mv.addObject("result", "failed");
mv.addObject("reason", "服务异常");
return mv;
}
}
}
mv.setViewName("login/uploadAvatar");
mv.addObject("result", "success");
session.setAttribute("photoData", photoData);
return mv;
}
js中的写法:
JavaScript code?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
function ajaxFileUpload (){
$.ajaxFileUpload ({
url :'photo/uploadPreviewPhoto.do',
type:"POST",
secureuri :false,
accept:"application/json",
fileElementId :'uploadFileName',
success : function (data, status){
// 此处的data是个HTMLObject,我其实想要个json字符串
document.getElementById("previewimg").src=path + "photo/getPreviewPhoto.do?width=0&height=0";
},
error: function (data, status, e){
alert(e);
}
})
return false;
}
我把请求后缀换成了.json,这样spring就知道是要解析成json了。
其他地方继续用.do
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询