struts2(一)

来源:互联网 发布:大众mirrorlink 软件 编辑:程序博客网 时间:2024/06/07 03:34
使用通配符,将配置降到最低。
不过要遵循“约定优于配置”原则


在action里。有通配符的都属于同一等级。优先匹配最精确的。
例如:
Student_add  ----------优先
{


*_*       ----------其次
Student_*   
} 他们谁在前先匹配谁。 




-----------------------------------------------------------------------------------------------------

action 传递参数


是调用类里的setXxx方法 后面的Xxx必须和地址栏的参数名称一样。
比如 setName(String name)  地址栏必须:http://localhost/Project/index?name=zhangsan


————————————————


通过模型驱动来传递参数。  对应的action类实现ModelDriven<T> 接口。重写getModel方法。
public class UserAction extends ActionSupport implements ModelDriven<User>{private User user = new User();//这个时候需要自己newpublic String add() {System.out.println("name=" + user.getName());System.out.println("age=" + user.getAge());return SUCCESS;}@Overridepublic User getModel() {return user;}}public class User {private String name;private int age;public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}}


通过
URL= http://localhost:8080/WebProjectName/user/user!add?name=aaa&age=8
来访问






当然,传递参数最多用的还是下面这种
public class UserAction extends ActionSupport{private User user = null;public String add() {System.out.println("name=" + user.getName());System.out.println("age=" + user.getAge());return SUCCESS;}public User getUser(){return user;}public void setUser(User user){this.user = user;}}


通过URL= http://localhost:8080/WebProjectName/user/user!add?user.name=aaa&user.age=8 来访问


————————————————


网页中提交数据有中文时乱码问题
在struts 节点下 配置 <constant name="struts.i18n.encoding" value="GBK" /> //i18n == internationalization
在Struts2.1.6中,做以上配置中文问题应该就可以解决,但是实际上并非如此,官网说这是个bug,在2.1.7才会修复。






OGNL表达式:(Object Graph Navigation language)对象图导航语言
errors= {name = [name is error]}
取出来的时候用标签  <s:property value="errors.name[0]">。


Stack Context 中attr属性是一个map 、其中的值包含了session request applicatoin 等等中的值。准确的说是个工具类。帮我们去搜其他属性中的值。




//可以通过实现接口的方式获得request;session;application;对象
public class LoginAction2 extends ActionSupport implements RequestAware,SessionAware, ApplicationAware {private Map<String, Object> request;private Map<String, Object> session;private Map<String, Object> application;//DI dependency injection  依赖注入//IoC inverse of control  控制翻转public String execute() {request.put("r1", "r1");session.put("s1", "s1");application.put("a1", "a1");return SUCCESS; }@Overridepublic void setRequest(Map<String, Object> request) {this.request = request;}@Overridepublic void setSession(Map<String, Object> session) {this.session = session;}@Overridepublic void setApplication(Map<String, Object> application) {this.application = application;}}




当输入一个工程名没有指定具体的action的时候,可以用以下配置一个默认action
<struts>    <constant name="struts.devMode" value="true" />        <package name="default" namespace="/" extends="struts-default">    <default-action-ref name="index"></default-action-ref>    <action name="index">    <result>/default.jsp</result>    </action>    </package></struts>


这个包里共用的result 不需要每个action里都写
<global-results><result name="mainpage" >/main.jsp</result></global-results>






一次request只有一个值栈。服务器端forward共享一个值栈。
user.xxx 只有传,才会构造,想要初始化domain model,可以自己new ,也可以传参数值,但是这个时候需要有空构造方法。


ognl表达式学习
<title>OGNL表达式语言学习</title></head><body><ol><li>访问值栈中的action的普通属性: username = <s:property value="username"/> </li><li>访问值栈中对象的普通属性(get set方法):<s:property value="user.age"/> | <s:property value="user['age']"/> | <s:property value="user[\"age\"]"/> | wrong: <%--<s:property value="user[age]"/>--%></li><li>访问值栈中对象的普通属性(get set方法): <s:property value="cat.friend.name"/></li><li>访问值栈中对象的普通方法:<s:property value="password.length()"/></li><li>访问值栈中对象的普通方法:<s:property value="cat.miaomiao()" /></li><li>访问值栈中action的普通方法:<s:property value="m()" /></li><hr /><li>访问静态方法:<s:property value="@com.bjsxt.struts2.ognl.S@s()"/></li><li>访问静态属性:<s:property value="@com.bjsxt.struts2.ognl.S@STR"/></li><li>访问Math类的静态方法:<s:property value="@@max(2,3)" /></li><hr /><li>访问普通类的构造方法:<s:property value="new com.bjsxt.struts2.ognl.User(8)"/></li><hr /><li>访问List:<s:property value="users"/></li><li>访问List中某个元素:<s:property value="users[1]"/></li><li>访问List中元素某个属性的集合:<s:property value="users.{age}"/></li><li>访问List中元素某个属性的集合中的特定值:<s:property value="users.{age}[0]"/> | <s:property value="users[0].age"/></li><li>访问Set:<s:property value="dogs"/></li><li>访问Set中某个元素:<s:property value="dogs[1]"/></li><li>访问Map:<s:property value="dogMap"/></li><li>访问Map中某个元素:<s:property value="dogMap.dog101"/> | <s:property value="dogMap['dog101']"/> | <s:property value="dogMap[\"dog101\"]"/></li><li>访问Map中所有的key:<s:property value="dogMap.keys"/></li><li>访问Map中所有的value:<s:property value="dogMap.values"/></li><li>访问容器的大小:<s:property value="dogMap.size()"/> | <s:property value="users.size"/> </li><hr /><li>投影(过滤):<s:property value="users.{?#this.age==1}[0]"/></li><li>投影:<s:property value="users.{^#this.age>1}.{age}"/></li><li>投影:<s:property value="users.{$#this.age>1}.{age}"/></li><li>投影:<s:property value="users.{$#this.age>1}.{age} == null"/></li><hr /><li>[]:<s:property value="[0].username"/></li></ol><s:debug></s:debug></body>



%{#incPage}  %{}的意思是不要把里面的内容当成字符串,强制转成OGNL表达式

0 0