spring和gwt整合,让gwt直接调用spring bean的前端servlet(二)
("An IncompatibleRemoteServiceException was thrown while processing this call.", ex);
return RPC.encodeResponseForFailure(null, ex);
}
}
protected Object getBean(HttpServletRequest request) {
String service = getService(request);
Object bean = getBean(service);
if (!(bean instanceof RemoteService)) {
throw new IllegalArgumentException(
"Spring bean is not a GWT RemoteService: " + service + " (" + bean + ")");
}
if (LOG.isDebugEnabled()) {
LOG.debug("Bean for service " + service + " is " + bean);
}
return bean;
}
protected String getService(HttpServletRequest request) {
String url = request.getRequestURI();
String service = url.substring(url.lastIndexOf("/") + 1);
if (LOG.isDebugEnabled()) {
LOG.debug("Service for URL " + url + " is " + service);
}
return service;
}
protected Object getBean(String name) {
WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
if (applicationContext == null) {
throw new IllegalStateException("No Spring web application context found");
}
if (!applicationContext.containsBean(name)) {
throw new IllegalArgumentException("Spring bean not found: " + name);
}
return applicationContext.getBean(name);
}
}
4、定义spring的bean,实现服务层接口。
[java]
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import javax.inject.Inject;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;
import com.vteba.test.client.GreetingService;
import com.vteba.test.model.User;
import com.vteba.test.rpc.SpringGwtRemoteServiceServlet;
import com.vteba.test.shared.FieldVerifier;
/**
* The server side implementation of the RPC service.
*/
@Service
public class GreetingServiceImpl implements GreetingService {
private static final long serialVersionUID = 1L;
@Inject
private JdbcTemplate jdbcTemplate;
public String greetServer(String input) throws IllegalArgumentException {
// Verify that the input is valid.
if (!FieldVerifier.isValidName(input)) {
// If the input is not valid, throw an IllegalArgumentException back
// to
// the client.
throw new IllegalArgumentException(
"Name must be at least 4 characters long");
}
String sql = "select * from users";
List userList = jdbcTemplate.query(sql, new RowMapper() {
public User mapRow(ResultSet rs, int row) {
User user = new User();
try {
user.setId(rs.getString("id"));
user.setCompany(rs.getString("company"));
user.setCreateDate(rs.getDate("create_date"));
user.setMobilePhone(rs.getString("mobile_phone"));
user.setState(rs.getInt("state"));
} catch (SQLException e) {
e.