我在添加图像时遇到问题。当我按下按钮时,出现错误"开机自检 http://localhost:8080/image/addImage 500(内部服务器错误("。我认为对于我的控制器和类,一切都很好,但我只知道 angularJS 的基本知识。我在谷歌上找不到好的解决方案,我在这里写。感谢您的帮助!
.JS
var ImageController = function($scope, $routeParams, $http) {
$scope.name = $routeParams.name;
$scope.url = "i.imgur.com/" + $scope.name;
$scope.image = {};
$scope.addNewImage = function(image) {
$http.post('image/addImage', image).success(function() {
$scope.image.name = '';
})
};}
托勒
@RequestMapping("/image")
@Controller
public class ImageController {
@RequestMapping(value = "/addImage", method = RequestMethod.POST)
public @ResponseBody void addImage(@RequestBody Image image) {imageService.addImage(image); }
图像类
@Entity
@Table(name = "IMAGE")
public class Image {
@Id
@GeneratedValue
private Long id;
@NotEmpty
@Column(name="URL", nullable=false)
private String url;
public Image(Long id, String url) {
this.id = id;
this.url = url;
}
.HTMLhttp://pastebin.com/73MExyJ8
谢谢你的回答大卫,我在 Java 中没有任何堆栈跟踪。我检查了数据库,没有任何新记录。也许我将对象保存到数据库的代码不正确。
public interface ImageService {
public void addImage(Image image);}
实现
@Service("imageService")
public class ImageServiceImpl implements ImageService {
@Override
public void addImage(Image image)
{
HibernateUtil hibernateUtil = new HibernateUtil();
SessionFactory sessFact = hibernateUtil.getSessionFactory();
Session session = sessFact.getCurrentSession();
org.hibernate.Transaction tr = session.beginTransaction();
System.out.println(tr.isActive());
session.save(image);
tr.commit();
}}