httpServletRequest.setAttribute这个方法是啥意思,请详细解释下

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了httpServletRequest.setAttribute这个方法是啥意思,请详细解释下相关的知识,希望对你有一定的参考价值。

setAttribute这个方法,在JSP内置对象session和request都有这个方法,这个方法作用就是保存数据,然后还可以用getAttribute方法来取出。
比如现在又个User对象,User curruser = new User("zhangsan", 20, "男");
1,request.setAttribute(“curruser”, curruser)这个方法是将curruser这个对象保存在request作用域中,然后在转发进入的页面就可以获取到你的值,如果你会一些框架的话,那些框架标签也可以获取到,比如struts标签,还有jstl。如果这你都不会的话,那么你可以在jsp页面编写java小脚本来获取:<% User myuser = (User)request.getAttribute("curruser")%>,在jsp页面显示值:<%=myuser.getName()%>。
2,session.setAttribute("curruser", curruser)。这个方法和上面唯一的区别就是作用域,就是在你整个程序启动的时候,如果在session中保存了数据,那么在你这个无论你在哪个页面,在什么时候都可以获取到这个值,全局的,只要你的这个程序是启动的。session默认的过期时间是30分钟,过期无效,可以去修改这个值。
参考技术A httpServletRequest也就是我们常说的request,httpServletRequest.setAttribute意思就是在request范围内设置一个属性主要用来存值供其他页面操作,setAttribute("name",value)有两个参数第一个是由你定义的名称,第二个是要存入的值,在相邻页面你可以用 httpServletRequest.getAttribute("name")获取到value本回答被提问者和网友采纳 参考技术B void setAttribute(String name,Object obj) 以指定名称保存请求中指定对象的引用 参考技术C 就向request里面增加一个键值对保存下来呗。作用域就是一次request请求。 参考技术D 是个网站

django model:auto_now_add 和 auto_now

创建django的model时,有DateTimeField、DateField和TimeField三种类型可以用来创建日期字段,其值分别对应着datetime()、date()、time()三中对象。这三个field有着相同的参数auto_now和auto_now_add,并且在实际使用中很容易出错,下面是一些注意点。

DateTimeField.auto_now

auto_now = True   # 这个参数的默认值为false,设置为true时,能够在保存该字段时,将其值设置为当前时间,并且每次修改model,都会自动更新。因此这个参数在需要存储“最后修改时间”的场景下,十分方便。需要注意的是,设置该参数为true时,并不简单地意味着字段的默认值为当前时间,而是指字段会被“强制”更新到当前时间,你无法程序中手动为字段赋值;如果使用django再带的admin管理器,那么该字段在admin中是只读的

DateTimeField.auto_now_add

auto_now_add = True  # 这个参数的默认值也为False,设置为True时,会在model对象第一次被创建时,将字段的值设置为创建时的时间,以后修改对象时,字段的值不会再更新。该属性通常被用在存储“创建时间”的场景下。与auto_now类似,auto_now_add也具有强制性,一旦被设置为True,就无法在程序中手动为字段赋值,在admin中字段也会成为只读的。

 

admin中的日期时间字段

auto_now和auto_now_add被设置为True后,这样做会导致字段成为editable=False和blank=True的状态。editable=False将导致字段不会被呈现在admin中,blank=Ture表示允许在表单中不输入值。此时,如果在admin的fields或fieldset中强行加入该日期时间字段,那么程序会报错,admin无法打开;如果在admin中修改对象时,想要看到日期和时间,可以将日期时间字段添加到admin类的readonly_fields中:

class YourAdmin(admin.ModelAdmin):
    readonly_fields = (save_date, mod_date,)
admin.site.register(Tag, YourAdmin)

如何将创建时间设置为“默认当前”并且可修改

那么问题来了。实际场景中,往往既希望在对象的创建时间默认被设置为当前值,又希望能在日后修改它。怎么实现这种需求呢?

django中所有的model字段都拥有一个default参数,用来给字段设置默认值。可以用default=timezone.now来替换auto_now=True或auto_now_add=True。timezone.now对应着django.utils.timezone.now(),因此需要写成类似下面的形式:

from django.db import models
import django.utils.timezone as timezone
class Doc(models.Model):
    add_date = models.DateTimeField(保存日期,default = timezone.now)
    mod_date = models.DateTimeField(最后修改日期, auto_now = True)

html页面从数据库中读出DateTimeField字段时,显示的时间格式和数据库中存放的格式不一致,比如数据库字段内容为2016-06-03 13:00:00,但是页面显示的却是Apr. 03, 2016, 1 p.m.

为了页面和数据库中显示一致,需要在页面格式化时间,需要添加<td>{{ infor.updatetime|date:"Y-m-d H:i:s" }}</td> 类似的过滤器。刷新页面,即可正常显示。

 

官方文档:

DateField
class DateField(auto_now=False, auto_now_add=False, **options)
A date, represented in Python by a datetime.date instance. Has a few extra, optional arguments:

DateField.auto_now
Automatically set the field to now every time the object is saved. Useful for “last-modified” timestamps. Note that the current date is always used; it’s not just a default value that you can override.

The field is only automatically updated when calling Model.save(). The field isn’t updated when making updates to other fields in other ways such as QuerySet.update(), though you can specify a custom value for the field in an update like that.

DateField.auto_now_add
Automatically set the field to now when the object is first created. Useful for creation of timestamps. Note that the current date is always used; it’s not just a default value that you can override. So even if you set a value for this field when creating the object, it will be ignored. If you want to be able to modify this field, set the following instead of auto_now_add=True:

    1. For DateField: default=date.today - from datetime.date.today()
            # For DateTimeField: default = datetime.now  # 不要加 ()  # - from datetime.datetime.now()
    2. For DateTimeField: default=timezone.now - from django.utils.timezone.now()
The default form widget for this field is a TextInput. The admin adds a JavaScript calendar, and a shortcut for “Today”. Includes an additional invalid_date error message key.

The options auto_now_add, auto_now, and default are mutually exclusive. Any combination of these options will result in an error.

"""
Note

As currently implemented, setting auto_now or auto_now_add to True will cause the field to have editable=False and blank=True set
"""

"""
Note

The auto_now and auto_now_add options will always use the date in the default timezone at the moment of creation or update. If you need something different, you may want to consider simply using your own callable default or overriding save() instead of using auto_now or auto_now_add; or using a DateTimeField instead of a DateField and deciding how to handle the conversion from datetime to date at display time.
"""

 

以上是关于httpServletRequest.setAttribute这个方法是啥意思,请详细解释下的主要内容,如果未能解决你的问题,请参考以下文章