@Entity @Table(name = "jforum_categories") @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE) @Component @PrototypeScoped public class Category implements Serializable { @Id @SequenceGenerator(name = "sequence", sequenceName = "jforum_categories_seq") @GeneratedValue(strategy = GenerationType.AUTO, generator = "sequence") @Column(name = "category_id") private int id; @Column(name = "category_order") private int displayOrder; @Column(name = "category_moderated") private boolean moderated; @Column(name = "category_title") private String name; @Transient private CategoryRepository repository; public Category() {} @Autowired public Category(CategoryRepository repository) { this.repository = repository; } public List<Forum> getForums() { return this.repository.getForums(this); } …… }
public class SpringInterceptor extends EmptyInterceptor { private final SessionFactory sessionFactory; private final ApplicationContext beanRegistry; public SpringInterceptor(ApplicationContext beanRegistry, SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; this.beanRegistry = beanRegistry; } @Override public Object instantiate(String entityName, EntityMode entityMode, Serializable id) { if (!EntityMode.POJO.equals(entityMode)) { return null; } Class<?> c = getClassByName(entityName); Object instance = this.beanRegistry.getBean(c); sessionFactory.getClassMetadata(c).setIdentifier(instance, id, EntityMode.POJO); return instance; } private Class<?> getClassByName(String name) { try { return Class.forName(name); } catch (ClassNotFoundException e) { throw new ForumException(e); } } }
二、具体问题
1.好好看一下Vraptor3.5的文档(http://vraptor.caelum.com.br/en/docs/one-minute-guide/)会节约你很多时间,文档精简的很,没多少。
2.包的问题,用Vraptor3.5里的包,大部分都有了。
3.从Hibernate3.2升级后,有一些小问题要改,最多的是uniqueResult()的结果是Long型,不能再转Integer了。
4.JForumTag里需要自己从springContext里拿Bean:
springContext = WebApplicationContextUtils.getRequiredWebApplicationContext(this.pageContext().getServletContext());
5.页面有些小问题,改改链接就好了,zh_CN.properties里有些字段没有,补齐即可。
6.上传文件大小的问题,Vraptor3.5默认上传文件的大小是两兆,需要重新配置(用代码):
@Component @ApplicationScoped public class CustomMultipartConfig extends DefaultMultipartConfig{ public long getSizeLimit() { return 50 * 1024 * 1024; // 50MB } }