现在的问题:前段发送请求到对应的controller,可以获取到参数,我想把返回数据放到modelMap中来返回给前台。但是前台一直报404,因为它并没有返回数据,而是去跳转页面了。
希望实现的目的:只返回数据给前台
spring-mvc配置文件
<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->
<mvc:annotation-driven />
<!-- 静态资源(js、image等)的访问 -->
<mvc:default-servlet-handler/>
<!-- 使用注解的包,包括子集 -->
<context:component-scan base-package="com.sky.controller" />
<!-- 简单的异常处理 -->
<bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="exceptionMappings">
<props>
<!-- 映射目录为/WEB-INF/jsp/error/upload_error.jsp -->
<prop key="org.springframework.web.multipart.MaxUploadSizeExceededException">/error/upload_error</prop>
</props>
</property>
</bean>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/pages/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!-- springmvc文件上传需要配置的节点-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="20971500"/>
<property name="defaultEncoding" value="UTF-8"/>
<property name="resolveLazily" value="true"/>
</bean>
</beans>
web.xml配置文件
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<display-name>Archetype Created Web Application</display-name>
<description>skyCredit</description>
<!-- 加载Spring配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/config/applicationContext.xml</param-value>
</context-param>
<!-- 编码过滤器 -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<async-supported>true</async-supported>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 日志记录 -->
<context-param>
<!-- 日志配置文件路径 -->
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:config/log4j.properties</param-value>
</context-param>
<context-param>
<!-- 日志页面的刷新间隔 -->
<param-name>log4jRefreshInterval</param-name>
<param-value>6000</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<!-- Spring监听 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Spring MVC配置 -->
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 自定义spring mvc的配置文件名称和路径 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:config/spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- spring mvc 请求后缀 -->
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
@RequestMapping("/userList")
@ResponseBody
@ResponseBody:加上这个注解就可以返回数据到页面,不加这个注解的话就代表是接下来跳转到页面。
您这里url绑定的方法没有返回值啊,void怎么能行,给你一个参考
上面这个empSee是一个jsp页面,直接在empSee.jsp用el表达式取值的啊,你这个又没用ajax,也没说明最终返回的视图,服务器怎么知道你想到哪里,当然报404啦
因为我在spring里加了这个就是下面代码
<!-- 增加前缀和后缀 Model和ModelAndView一起用时,也是支持JSP JSTL的解析器 -->
<!-- 前面自动加斜线 -->
<!-- 后面自动加点jsp -->
所以,我的return 后面直接省略jsp了用empSee,就跳转到修改员工页面了
举个例子:
js:
$.ajax({
type: "POST",
url: '/CIC/csCollectionList/queryCreateDateImp.do',
data : "record_id="+record_ids+"&&loan_no="+loans_no,
async: false,//同步提交
success: function(data){
var result= $.parseJSON(data);
if(result.flag=="Y"){
$.messager.alert('提示',"成功");
}
}
});
java代码中:
@RequestMapping("/queryCreateDateImp")
@ResponseBody
public String queryCreateDateImp(String loan_no,String record_id){
Map<String,Object> map=new HashMap<String,Object>();
String flag = "Y";
try {
CsRecordImportantList cr = collectionListServiceImp.queryOverdueDetailImpNo(loan_no,record_id);
if(cr!=null){
flag = "Y";
}
map.put("cr", cr);
} catch (Exception e) {
e.printStackTrace();
}
map.put("flag", flag);
return JSON.toJSONString(map);
}