【发布时间】:2015-08-13 14:30:21
【问题描述】:
我是 spring mvc 的新手,我正在从事一个项目,该项目之前在 servlet 上代替 spring。在我的代码中,我使用 DispatcherServlet 处理两个 ajax 调用,其中一个工作正常,另一个在我启动服务器时第一次工作,但在我刷新页面时不会再次点击控制器。这意味着问题不在 spring 环境中,但在第二个控制器如何与 spring 交互(因为它在 servlet 中工作得非常好)。
这是我的 web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:web="http://java.sun.com/xml/ns/javaee" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>final</display-name>
<welcome-file-list>
<welcome-file>datapage.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
这是我的 Dispatcher-servlet 文件
<?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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-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/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"
>
<context:component-scan
base-package="raga.controller" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver" >
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix">
<value>/WEB-INF/jsp/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>
这是我的 /CompanyGsonObjects 控制器
@Controller
public class sping {
@RequestMapping(value="/CompanyGsonObjects", method = RequestMethod.GET)
public void CompanyGsonObjects(HttpServletRequest request, HttpServletResponse response) throws IOException, SQLException{
System.out.println("abc");
JQueryDataTableParamModel param = DataTablesParamUtility.getParam(request);
String sEcho = param.sEcho;
int iTotalRecords; // total number of records (unfiltered)
int iTotalDisplayRecords; //value will be set when code filters companies by keyword
iTotalRecords = DataRepository.GetCompanies().size();
List<Company> companies = new LinkedList<Company>();
for(Company c : DataRepository.GetCompanies()){
if( c.getName().toLowerCase().contains(param.sSearch.toLowerCase())
||
c.getAddress().toLowerCase().contains(param.sSearch.toLowerCase())
||
c.getProxy().toLowerCase().contains(param.sSearch.toLowerCase())
||
c.getTown().toLowerCase().contains(param.sSearch.toLowerCase()))
{
companies.add(c); // add company that matches given search criterion
}
}
iTotalDisplayRecords = companies.size();// number of companies that match search criterion should be returned
final int sortColumnIndex = param.iSortColumnIndex;
final int sortDirection = param.sSortDirection.equals("asc") ? -1 : 1;
Collections.sort(companies, new Comparator<Company>(){
@Override
public int compare(Company c1, Company c2) {
switch(sortColumnIndex){
case 0:
return c1.getName().compareTo(c2.getName()) * sortDirection;
case 1:
return c1.getAddress().compareTo(c2.getAddress()) * sortDirection;
case 2:
return c1.getTown().compareTo(c2.getTown()) * sortDirection;
case 3:
return c1.getProxy().compareTo(c2.getProxy()) * sortDirection;
}
return 0;
}
});
if(companies.size()< param.iDisplayStart + param.iDisplayLength) {
companies = companies.subList(param.iDisplayStart, companies.size());
} else {
companies = companies.subList(param.iDisplayStart, param.iDisplayStart + param.iDisplayLength);
}
try {
JsonObject jsonResponse = new JsonObject();
jsonResponse.addProperty("sEcho", sEcho);
jsonResponse.addProperty("iTotalRecords", iTotalRecords);
jsonResponse.addProperty("iTotalDisplayRecords", iTotalDisplayRecords);
Gson gson = new Gson();
jsonResponse.add("aaData", gson.toJsonTree(companies));
response.setContentType("application/Json");
response.getWriter().print(jsonResponse.toString());
} catch (JsonIOException e) {
e.printStackTrace();
response.setContentType("text/html");
response.getWriter().print(e.getMessage());
}
}
}
现在,正如我告诉你的那样,这是行不通的,但我发现还有一些有趣的事情,那就是如果我在 servlet 映射中给出完整的 url 模式,就可以正常工作。例如,如果我写
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/CompanyGsonObjects</url-pattern> // the call which does not work when i use / instead of CompanyGsonObjects
</servlet-mapping>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/spring</url-pattern> // the call which works
</servlet-mapping>
正如我告诉你的那样,无论我使用“/”还是“/spring”,带有 url /spring 的请求都可以正常工作,但是当我使用“/”时,请求 url /CompanyGsonObjects 不起作用。 如果有人可以提出建议,请提供帮助。感谢您的阅读。
【问题讨论】:
-
如果您能说出所请求的 URL(第一次和第二次)是什么样的,那就太好了。很抱歉,我的水晶球目前无法使用......
标签: java spring spring-mvc servlets