接受我的初步教训django ModelForm,我想给用户,能够在博客中编辑条目.BlogEntry有一个date,postedTime, title and content.我想向用户显示一个editform,显示所有这些字段,但只有title and content as editable.的date and postedTime should be shown as uneditable.
class BlogEntry(models.Model):
title = models.CharField(unique=True,max_length=50)
description = models.TextField(blank=True)
date = models.DateField(default=datetime.date.today)
postedTime = models.TimeField(null=True)
...
Run Code Online (Sandbox Code Playgroud)
为了添加条目,我以正常方式使用ModelForm.
class BlogEntryAddForm(ModelForm):
class Meta:
model = BlogEntry
...
Run Code Online (Sandbox Code Playgroud)
但是我如何创建编辑表单?我希望它show the date,postedTime as uneditable(但仍然在表单上显示)并让用户编辑title and description.
如果我使用,exclude in class Meta对于date和postedTime,这将导致它们不会出现在窗体上.那么,我怎样才能将它们显示为不可编辑的?
class BlogEntryEditForm(ModelForm):
class Meta:
model = BlogEntry
...?...
Run Code Online (Sandbox Code Playgroud) 在spring3上课时,我编写了一个教程中的示例.我创建了一个控制器如下
package my.spring.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import my.spring.form.Contact;
@Controller
public class ContactController {
@RequestMapping(value ="/addContact",method =RequestMethod.POST)
public String addContact(@ModelAttribute("contact") Contact ct){
System.out.println("First Name:" + contact.getFirstname() + "Last Name:" + contact.getLastname());
return "redirect:contacts.htm";
}
@RequestMapping("/contacts")
public ModelAndView showContacts() {
System.out.println("showing contacts");
return new ModelAndView("contact", "userEntries", new Contact());
}
}
Run Code Online (Sandbox Code Playgroud)
然后我决定玩它并修改方法参数中的@ModelAttribute
public String addContact(@ModelAttribute("contact") Contact ct)
Run Code Online (Sandbox Code Playgroud)
至
public String addContact(@ModelAttribute("somevalue") Contact ct)
Run Code Online (Sandbox Code Playgroud)
我仍然无法找到应用程序行为的任何变化.这对我来说有点意外.据我所知,表单中的数据是在Contact对象中收集的,并使用@ModelAttribute将该对象绑定到参数ct.然后使用此参数在方法内部进行处理.是不是@ModelAttribute()中使用的实际字符串无关紧要?
这是WEB-INF/jsp/contact.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML …Run Code Online (Sandbox Code Playgroud) 假设我想为我的使用 servlet 的 web 应用程序创建一个服务层,我应该怎么做?(我没有使用 web 应用程序框架..所以,请耐心等待)。我应该将它实现为侦听器吗?该服务旨在进行数据库访问。也就是说,我应该能够从我的 servlet 调用
class MyServlet{
...
doPost(...){
...
MyEntity entity = dbAccessService.getMyEntity(someId);
...
}
}
Run Code Online (Sandbox Code Playgroud)
dbAccessService 应该在哪里处理休眠会话、事务等。以前我曾经在 dao 方法中完成所有这些,但我被告知这不是一个好主意。
欢迎任何建议
谢谢
标记
下面给出了示例代码片段
class DBAccessServiceImpl{
...
private MyEntity getMyEntity(Long id){
Transaction tx = null;
MyEntity me = null;
Session session = HibernateUtil.getCurrentSession();
try{
tx = session.beginTransaction();
return entitydao.findEntityById(id);
}catch(RuntimeException e){
logger.info("problem occurred while calling findEntityById()");
throw e;
}
}
...
}
Run Code Online (Sandbox Code Playgroud)
然后创建一个监听器来实例化DBAccessService
class MyAppListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent ctxEvent) {
ServletContext …Run Code Online (Sandbox Code Playgroud) 在我使用servlet和hibernate的web应用程序中.我需要对输入密码的客户进行身份验证.
如果他已经在数据库中,我需要检查他的密码是否与db中的记录匹配.对于新客户,我想获取密码并为他创建记录.我尝试以这种方式为场景做到这一点.
现有客户输入电子邮件地址和密码
String email = req.getParameter("emailAddress");
String password = req.getParameter("password");
Customer cust = dao.findByEmailAddress(email);
Run Code Online (Sandbox Code Playgroud)
现在,如何检查此cust对象是否与密码相关联并且与用户输入的内容相匹配?Manning的hibernate book示例将密码存储为Customer类中的String.这是一个好主意吗?如何将其存储在数据库中?
使用休眠时,如何处理?我听说有人提到将密码存储为哈希.但我不确定如何在我的应用程序中执行此操作.
有人能告诉我如何解决这个问题吗?
我需要为一个Item实现equals()方法,该方法可以放在它的Maker的hashset中.Item可以有如下字段
class Item{
private String isbn;
private String name;
private double price;
...
}
class Maker{
private String name;
private Set<Item> items;
public Maker() {
super();
items = new HashSet<Item>();
}
...
}
Run Code Online (Sandbox Code Playgroud)
如果我通过比较三个字段来实现equals并根据这些字段写一个hashCode(),那么我会得到错误的结果
1.add item to hashset
2.modify the price of item
3.try to find if item exists in hashset
@Override
public boolean equals(Object o){
if(o == this){
return true;
}
if (!(o instanceof Item)){
return false;
}
Item a = (Item)o;
if(hasSameName(a) && hasSameIsbn(a) && hasSamePrice(a)){
return true; …Run Code Online (Sandbox Code Playgroud) 可能重复:
在Java中,最终会返回特朗普吗?
我在dao实现中遇到了一个java代码片段.它返回一个List,如下所示.
执行'return'语句后,finally块尝试关闭会话.这会有效吗?会议还会开放吗?
谢谢
标记
import org.hibernate.SessionFactory;
import org.hibernate.Criteria;
...
public List<Item> findItems(String name) {
SessionFactory factory = HibernateUtil.getSessionFactory();
Session session = factory.openSession();
try{
Criteria cri = session.createCriteria(Item.class);
return (List<Item>) cri.add(Restrictions.eq("name", name)).list();
} finally {
session.close();
}
}
Run Code Online (Sandbox Code Playgroud)