浏览 2944 次
锁定老帖子 主题:体验GAE springMVC
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2009-04-13
最后修改:2009-04-15
把gae java的教程例子改了一下,用springMVC写的,目前只是个半成品,现在bug是点击submit才能看到所有留言,点击两次submit才能看到自己发的留言。
因为是springMVC是第一次接触,写的不好,东西是今天上悄悄改的,页面也没仔细弄,下午还得去忙工作。有时间再改吧。 怎么配置gae spring 参考的是这篇帖子http://sikeh.iteye.com/blog/364043
由于东西bug比较多,就贴几个主要代码。具体配置参考上面给的链接,写的很详细了 呵呵、 bean package com.fish.gae.entity; import java.util.Date; import javax.jdo.annotations.IdGeneratorStrategy; import javax.jdo.annotations.IdentityType; import javax.jdo.annotations.PersistenceCapable; import javax.jdo.annotations.Persistent; import javax.jdo.annotations.PrimaryKey; import com.google.appengine.api.users.User; @PersistenceCapable(identityType = IdentityType.APPLICATION) public class Comment { @PrimaryKey @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY) private Long id; @Persistent private User author; @Persistent private String content; @Persistent private Date date; @Persistent private String commentDate; public Comment(User author, Date date, String content,String commentDate) { this.author = author; this.content = content; this.date = date; this.commentDate =commentDate; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public User getAuthor() { return author; } public void setAuthor(User author) { this.author = author; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } public Date getDate() { return date; } public void setDate(Date date) { this.date = date; } public String getCommentDate(){ return commentDate; } public void setCommentdate(String commentDate){ this.commentDate = commentDate; } }
controller package com.fish.gae.controller; import java.text.SimpleDateFormat; import java.util.Date; import java.util.List; import java.util.logging.Logger; import javax.jdo.PersistenceManager; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import com.fish.gae.entity.Comment; import com.fish.gae.entity.PMF; import com.google.appengine.api.users.User; import com.google.appengine.api.users.UserService; import com.google.appengine.api.users.UserServiceFactory; @Controller @RequestMapping(value = "/commentAdd.htm") public class CommentController { private static final Logger log = Logger.getLogger(CommentController.class .getName()); @RequestMapping(method = RequestMethod.POST) public String doPost(String content, Model model) { UserService userService = UserServiceFactory.getUserService(); User user = userService.getCurrentUser(); Date date = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm"); String commentDate = sdf.format(new Date()); model.addAttribute("content", content); Comment comment = new Comment(user, date, content,commentDate); PersistenceManager pm = PMF.get().getPersistenceManager(); try { pm.makePersistent(comment); } finally { pm.close(); } return "comment"; } @ModelAttribute("comments") public List<Comment> doGet(Model model) { UserService userService = UserServiceFactory.getUserService(); User user = userService.getCurrentUser(); PersistenceManager pm = PMF.get().getPersistenceManager(); String query = "select from " + Comment.class.getName(); List<Comment> comments = (List<Comment>)pm.newQuery(query).execute(); if(comments.isEmpty()){ String message= "没有留言"; System.out.print(message); }else{ for(Comment co : comments){ if(co.getAuthor()==null){ String author = "火星人"; }else{ co.getAuthor().getNickname(); } co.getContent(); } } return comments; } } 页面 comment.jsp <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <%@ page isELIgnored="false"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title></title> </head> <body> <c:forEach items="${comments}" var="comments" varStatus="status"> <c:out value="${comments.content}"></c:out> <c:out value="${comments.commentDate}"></c:out> </c:forEach> <form action="commentAdd.htm" method="post"> <table> <tr> <td>comment</td> <td><textarea rows="3" cols="10" name="content"></textarea></td> </tr> <tr> <td><input type="submit" value="Submit" /></td> </tr> </table> </form> </body> </html>
声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |