我正在尝试使用数据表的Spring MVC 4来发布前端数据。
我的项目名称是NewEDXUI。
情景: - index.jsp将从Controller / Rest Controller获取数据,以表格格式从数据库发布数据。
我面临的问题 - 我创建了web.xml和servlet.xml
1>在我的jsp中,如果我从资源文件夹中提供Js文件的路径 - 那么在加载时它正在寻找http://localhost:8080/NewEDXUI/Views/resources/js/client.js 如果不是" resources / js / client.js"我使用" ../ resources / js / client.js"然后正确加载client.js文件。
2 - ; Jsp页面有id =" tableClient"它应该从client.js var tableClient获取数据,这是在控制器包MainController.java中调用getAllPartners服务定义 ..但是当它正在调用时我正在接受 获取http://localhost:8080/NewEDXUI/Views/getAllPartners?_=1497263560865 404(未找到)
请提供一些指导如何解决这些问题并完成我的第一个Spring项目。
项目结构
Web.xml中
<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" metadata-complete="true">
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
用SpringMVC-servlet.xml中
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config />
<tx:annotation-driven />
<mvc:annotation-driven />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/Views/" />
<property name="suffix" value=".jsp" />
</bean>
<mvc:resources mapping="/resources/**" location="/resources/" />
<context:property-placeholder location="classpath:hibernate.properties" />
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${database.driverClass}" />
<property name="url" value="${database.url}" />
<property name="username" value="${database.username}" />
<property name="password" value="${database.password}" />
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
</props>
</property>
<property name="packagesToScan">
<list>
<value>com.edx.model</value>
</list>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<context:component-scan base-package="com.edx.controller" />
<context:component-scan base-package="com.edx.dao" />
<context:component-scan base-package="com.edx.service" />
<context:component-scan base-package="com.edx.util" />
Client.js
$(document).ready(
function() {
var tableClient = $('#tableClient').DataTable(
{
"autoWidth" : false,
"columnDefs" : [{
"targets" : [ 0 ],
"visible" : false,
"searchable" : false
}],
"ajax" : {
"url" : "/NewEDXUI/Views/getAllPartners",
"type" : "GET",
"success" : function(data) {
$.each(data, function(ind, obj) {
tableClient.row.add(
[ obj.partner_code,
obj.partner_name,
obj.status_code,
obj.default_group_code,
obj.partner_type,
obj.network_type ]).draw();
});
}
},
})
$(window).on('load',function() {
});
$("#buttonSearch").click(function() {
tableClient.clear().draw();
tableClient.ajax.reload();
});
});
MainController.java
package com.edx.controller;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.edx.model.Partner;
import com.edx.service.PartnerService;
@RestController
public class MainController {
@Autowired
private PartnerService partnerService;
@RequestMapping(value="/getAllPartners", method=RequestMethod.GET, produces="application/json")
public List<Partner> getAllPartners(HttpServletRequest req,HttpServletResponse resp){
System.out.println("Inside Controller");
List<Partner> partnerlist = partnerService.getAllPartners();
return partnerlist;
}
}
答案 0 :(得分:0)
为什么你使用Spring 4和xml文件配置?有关最佳实践,请在Spring 4上使用注释Config。
您提出要求也有误,请删除观看次数:
http://localhost:8080/NewEDXUI/getAllPartners