当我尝试向服务器发送消息时,出现错误404。
请帮我解决这个问题。
向服务器发送请求:1名称Sname作业服务器回复:An
尝试联系服务器时发生错误。请检查
您的网络连接,然后重试。错误是:
com.google.gwt.user.client.rpc.StatusCodeException:404 HTTP错误:
404
未找到
RequestURI = / gwtspringtestnomaven / springGwtServices / employeeService
由Jetty://提供技术支持
我的代码:
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app 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_2_5.xsd"
version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<servlet>
<servlet-name>springGwtRemoteServiceServlet</servlet-name>
<servlet-class>org.spring4gwt.server.SpringGwtRemoteServiceServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>springGwtRemoteServiceServlet</servlet-name>
<url-pattern>/com.javacodegeeks.Gwtspringtestnomaven/springGwtServices/*</url-pattern>
</servlet-mapping>
<!-- Default page to serve -->
<welcome-file-list>
<welcome-file>Gwtspringtestnomaven.html</welcome-file>
</welcome-file-list>
</web-app>
Gwtspringtestnomaven.gwt.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!--
When updating your version of GWT, you should also update this DTD reference,
so that your app can take advantage of the latest GWT module capabilities.
-->
<!DOCTYPE module PUBLIC "-//Google Inc.//DTD Google Web Toolkit 2.5.1//EN"
"http://google-web-toolkit.googlecode.com/svn/tags/2.5.1/distro-source/core/src/gwt-module.dtd">
<module rename-to='gwtspringtestnomaven'>
<!-- Inherit the core Web Toolkit stuff. -->
<inherits name='com.google.gwt.user.User'/>
<!-- Inherit the default GWT style sheet. You can change -->
<!-- the theme of your GWT application by uncommenting -->
<!-- any one of the following lines. -->
<inherits name='com.google.gwt.user.theme.clean.Clean'/>
<!-- <inherits name='com.google.gwt.user.theme.standard.Standard'/> -->
<!-- <inherits name='com.google.gwt.user.theme.chrome.Chrome'/> -->
<!-- <inherits name='com.google.gwt.user.theme.dark.Dark'/> -->
<!-- Other module inherits -->
<!-- Specify the app entry point class. -->
<entry-point class='com.javacodegeeks.client.Gwtspringtestnomaven'/>
<!-- Specify the paths for translatable code -->
<source path='client'/>
<source path='shared'/>
</module>
EmployeeServiceAsync.java:
package com.javacodegeeks.shared.services;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.rpc.ServiceDefTarget;
import com.javacodegeeks.shared.dto.EmployeeDTO;
public interface EmployeeServiceAsync {
void deleteEmployee(long employeeId, AsyncCallback<Void> callback);
void findEmployee(long employeeId, AsyncCallback<EmployeeDTO> callback);
void saveEmployee(long employeeId, String name, String surname,
String jobDescription, AsyncCallback<Void> callback);
void saveOrUpdateEmployee(long employeeId, String name, String surname,
String jobDescription, AsyncCallback<Void> callback);
void updateEmployee(long employeeId, String name, String surname,
String jobDescription, AsyncCallback<Void> callback);
public static final class Util
{
private static EmployeeServiceAsync instance;
public static final EmployeeServiceAsync getInstance()
{
if ( instance == null )
{
instance = (EmployeeServiceAsync) GWT.create( EmployeeService.class );
ServiceDefTarget target = (ServiceDefTarget) instance;
target.setServiceEntryPoint( GWT.getModuleBaseURL() + "springGwtServices/employeeService " );
}
return instance;
}
private Util()
{
// Utility class should not be instanciated
}
}
}
EmployeeServiceImpl.java:
package com.javacodegeeks.server.services;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.javacodegeeks.server.dao.EmployeeDAO;
import com.javacodegeeks.shared.dto.EmployeeDTO;
import com.javacodegeeks.shared.services.EmployeeService;
@Service("employeeService")
public class EmployeeServiceImpl implements EmployeeService {
@Autowired
private EmployeeDAO employeeDAO;
@PostConstruct
public void init() throws Exception {
}
@PreDestroy
public void destroy() {
}
public EmployeeDTO findEmployee(long employeeId) {
return employeeDAO.findById(employeeId);
}
@Transactional(propagation=Propagation.REQUIRED, rollbackFor=Exception.class)
public void saveEmployee(long employeeId, String name, String surname, String jobDescription) throws Exception {
EmployeeDTO employeeDTO = employeeDAO.findById(employeeId);
if(employeeDTO == null) {
employeeDTO = new EmployeeDTO(employeeId, name,surname, jobDescription);
employeeDAO.persist(employeeDTO);
}
}
@Transactional(propagation=Propagation.REQUIRED, rollbackFor=Exception.class)
public void updateEmployee(long employeeId, String name, String surname, String jobDescription) throws Exception {
EmployeeDTO employeeDTO = employeeDAO.findById(employeeId);
if(employeeDTO != null) {
employeeDTO.setEmployeeName(name);
employeeDTO.setEmployeeSurname(surname);
employeeDTO.setJob(jobDescription);
}
}
@Transactional(propagation=Propagation.REQUIRED, rollbackFor=Exception.class)
public void deleteEmployee(long employeeId) throws Exception {
EmployeeDTO employeeDTO = employeeDAO.findById(employeeId);
if(employeeDTO != null)
employeeDAO.remove(employeeDTO);
}
@Transactional(propagation=Propagation.REQUIRED, rollbackFor=Exception.class)
public void saveOrUpdateEmployee(long employeeId, String name, String surname, String jobDescription) throws Exception {
System.out.println("into SaveOrUpdate");
EmployeeDTO employeeDTO = new EmployeeDTO(employeeId, name,surname, jobDescription);
System.out.println("SaveOrUpdate good");
employeeDAO.merge(employeeDTO);
System.out.println(" SaveOrUpdate out");
}
}
Gwtspringtestnomaven.html:
<!doctype html>
<!-- The DOCTYPE declaration above will set the -->
<!-- browser's rendering engine into -->
<!-- "Standards Mode". Replacing this declaration -->
<!-- with a "Quirks Mode" doctype is not supported. -->
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<!-- -->
<!-- Consider inlining CSS to reduce the number of requested files -->
<!-- -->
<link type="text/css" rel="stylesheet" href="Gwtspringtestnomaven.css">
<!-- -->
<!-- Any title is fine -->
<!-- -->
<title>Web Application Starter Project</title>
<!-- -->
<!-- This script loads your compiled module. -->
<!-- If you add any GWT meta tags, they must -->
<!-- be added before this line. -->
<!-- -->
<script type="text/javascript"language="javascript" <!-- здесь кст пишет :Attribute (language) is obsolete. Its use is discouraged in HTML5 documents. -->
src="gwtspringtestnomaven/gwtspringtestnomaven.nocache.js"></script>
</head>
<!-- -->
<!-- The body can have arbitrary html, or -->
<!-- you can leave the body empty if you want -->
<!-- to create a completely dynamic UI. -->
<!-- -->
<body>
<!-- OPTIONAL: include this if you want history support -->
<iframe src="javascript:''" id="__gwt_historyFrame" tabIndex='-1' style="position:absolute;width:0;height:0;border:0"></iframe>
<!-- RECOMMENDED if your web app will not function without JavaScript enabled -->
<noscript>
<div style="width: 22em; position: absolute; left: 50%; margin-left: -11em; color: red; background-color: white; border: 1px solid red; padding: 4px; font-family: sans-serif">
Your web browser must have JavaScript enabled
in order for this application to display correctly.
</div>
</noscript>
<h1>Web Application Starter Project</h1>
<table align="center">
<tr>
<td colspan="2" style="font-weight:bold;">Please enter employee info (id name surname job):</td>
</tr>
<tr>
<td id="employeeInfoFieldContainer"></td>
<td id="updateEmployeeButtonContainer"></td>
</tr>
<tr>
<tr>
<td colspan="2" style="font-weight:bold;">Please enter employee id:</td>
</tr>
<tr>
<td id="employeeIdFieldContainer"></td>
<td id="retrieveEmployeeButtonContainer"></td>
</tr>
<tr>
<td colspan="2" style="color:red;" id="errorLabelContainer"></td>
</tr>
</table>
</body>
</html>
最佳答案
您的Spring服务Servlet映射包括一个包名称:<url-pattern>/com.javacodegeeks.Gwtspringtestnomaven/springGwtServices/*</url-pattern>
。您的请求URI使用相对于Webb应用程序名称的简称:RequestURI=/gwtspringtestnomaven/springGwtServices/employeeService
。解决方案:修复Servlet映射,使其指向正确的位置。