Struts2学习笔记(三)

来源:互联网 发布:C#socket异步接收数据 编辑:程序博客网 时间:2024/05/21 08:46
1.ognl表达式和el表达式的不同?
①ognl通常要结合struts2的标志一起使用,如<s:property="#xx" />,在页面中不能单独使用,el可以单独使用,${sessionScope.userName}
②ognl是struts2默认的表达式语言。#符号的用途有两种
Ⅰ访问非根属性,例如#session.username,在struts2中值栈value stack为跟对象,所以访问非根对象时,要加#前缀。#相当于ActionContext.getContext();
#session.username相当于
ActionContext.getSession().getAttribute("username");
Ⅱ用来构造map,例如#{name:"zhangsan",age:15}
③ognl表达式里边也可以有$符号,到时注意和el区别。可以用加不加#测试区别。
④el表达式基本格式为:${表达式}
表达式的值为null,则在页面显示为空字符串,而不是null。它获取的是作用域中的值,因此若没有往作用域中设置值,则显示为空字符串。

2.ActionContext对象是action执行的上下文环境,包括request,session,application,parameter,attr,action
ACtionContext采用threadLocal模式,为我们共享信息,这样可以在线程的任何地方获取到想要的数据。action不是单例,单例是为了节省内存资源,控制对象数量。

㈠struts2的ognl表达式,可以分为两点
①针对根对象(Root Object)的操作,表达式是自根对象到被访问对象的链式操作的字符串。所谓的根对象,就是值栈(value stack)中的内容。
②针对上下文环境(Context)的操作,表达式是自上下文环境(Context)到被访问对象的链式操作的字符串,但必须在该字符串前加“#”,表示与访问根对象的区别。
Ognl的上下文环境是一个map结构,称之为OgnlContext.根对象(root object)也会被加入到上下文环境中,并将这作为一个特殊的变量处理。具体表现就是针对根对象(root object)的存取操作的表达式是不需要增加#符号来区分的。

㈡ognlContext的分类(很重要)


㈢struts2中,ognl表达式需要配合struts标签一起使用。如<s:property value="name" />

㈣在jsp页面中,可以通过el表达式直接访问根对象value stack中的值,如

${uname};//表示获取值栈中某个对象的uname属性

如果访问其它Context中的对象,由于它们不是根对象,所以在访问时需要加“#”前缀。

㈤ognl表达式中的#相当于ActionContext.getContext();

#session.msg;//就相当于ActionContext.getContext().getSession().getAttribute("msg");
3.出现错误

Could not find action or result There is no Action mapped for namespace / and action name login. - [unknown location] 
原因很明显,配置不正确。但我的原因不是struts.xml出错,而是web.xml

就在在下了网站模板,web.xml配置文件中的

<welcome-file-list>        <welcome-file>index.html</welcome-file></welcome-file-list>
没改成index.jsp,这也会影响。所以说在这些配置中更应该注意,后期如果有时间的话,将自己所遇到的错集中到一块。

在网上查了,若不写这个的话启动后必须在IE地址栏输入

http://localhost:8080/.........../login.jsp
写了该配置才可以直接访问项目

http://localhost:8080/项目名称
此时不会报错,后边不用写login.jsp

4.struts.xml中namespace="/"意为访问

<a href="<%=request.getContextPath()%>/xxx.do">xx</a>
或namespace="/user",访问

<a href="<%=request.getContextPath()%>/user/xxx.do">xxx</a>
5.struts2要获取值,可看下边的例子。

从上边可以看出,get和set方法必须有,否则取不到值。这涉及到struts2内部原理,慢慢理解。


6.struts2单表查询单条数据,写法不再和以前的一样。
以前是根据

public String list(int id){ //往方法传入一个id,根据id查找数据schance = scDao.findById(id);return "list";}
而struts2中写法不再是这样,id和类都是通过ioc获取,如下

private Sell_Chance schance;//实体类private int chc_id;//chc_id和数据库表中字段要一致,也要单独写一个id的属性public int getChc_id() {//get和set方法不能丢return chc_id;}public void setChc_id(int chc_id) {this.chc_id = chc_id;}public Sell_Chance getSchance() {return schance;}public void setSchance(Sell_Chance schance) {this.schance = schance;}
需要注意的是,此时findById的写法应为

public String comlist(){ //这儿的方法中comlist不需要传递id这个参数,而由get/set方法获取schance = scDao.findById(this.chc_id);//获取id的方法已由struts2封装,和以前不一样return "comlist";}
7.jsp页面点击按钮跳转页面,可以用

click="window.open('jsp/add.jsp')";//跳转页面的单引号必须加,否则出错
html中button如何跳转到action?
可以用

<input type="button" onclick="window.location='xxx.action'">
8.如何在jsp页面显示系统当前时间?

<% SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd");//若想以其它格式显示,可以在这里修改Date date = new Date();%>当前时间:<%=s.format(date)%>
在添加数据时,不用将Date型转换为String型,因为struts2中可以将这二者直接转换。

9.struts2中s:action的用法,它可以直接访问action。比如

<s:action name="StatusAction!list" namespace="/"  var="st"></s:action>机会状态:<select name="schance.chc_status">   <s:iterator value="#st.status" var="obj"><option value="${obj.sid }">${obj.sname }</option></s:iterator></select>
表示访问StatusAction的list方法,var存放的是StatusDao 中的list,表示从Status数据库获取的值。虽然list()方法返回值为null,但那是给struts.xml的result使用的,不影响此处。然后在s:iterator中value为从action中传过来的值,但要在前边加#,sid和sname都是实体类Status中属性。

而StatusAction为

public class StatusAction {private StatusDao sdao = (StatusDao) DaoFactory.getDao("StatusDao");private List<Status> status;//盛装结果,必须写相应的get/set方法 public List<Status> getStatus() {return status;}public void setStatus(List<Status> status) {this.status = status;}public String list(){this.status = sdao.findAll();return null;}}
StatusDao中为

public class StatusDao {public List<Status> findAll(){Session session = HiberUtil.getSession();String hql = "from Status";Query query = session.createQuery(hql);List<Status> list = query.list();//session.close();//这行不能写,否则报错return list;}}
10.struts2开启开发模式,可以用

<struts><constant name="struts.devMode" value="true"></constant></struts>
直接在<struts>标签下写即可。


原创粉丝点击