java学习

java学习

 

struts2中用ognl访问普通类的构造方法

public class User implements Serializable{

 /**
  *
  */
 private static final long serialVersionUID = 1L;
 
 private String name;
 public User(){
  System.out.println("===============");
 }
 public User(String name){
  this.name = name;
 }

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }
 
 @Override
 public String toString() {
  return "user="+this.name;
 }

}

  <s:property value="new User('name')"/>

posted @ 2013-04-15 22:54 杨军威 阅读(1267) | 评论 (0)编辑 收藏

struts2用ognl访问普通类的静态属性和静态方法

public class Test {

 public static String NAME = "nametom";
 public static String test(){
  return "testtom";
 }
}



在struts.xml配置文件中加<constant name="struts.ognl.allowStaticMethodAccess" value="true"></constant>
  <s:property value="@Test@test()"/>
  <s:property value="@Test@NAME"/>

posted @ 2013-04-15 22:44 杨军威 阅读(321) | 评论 (0)编辑 收藏

struts2中ognl直接访问action中的方法

public class UserAction extends ActionSupport{

 /**
  *
  */
 private static final long serialVersionUID = 1L;
  public String  mm(){
  return "mm";
  
 }
}
   <s:property value="mm()"/>

posted @ 2013-04-15 22:23 杨军威 阅读(145) | 评论 (0)编辑 收藏

struts2中ognl在视图中访问对象的方法

public class User implements Serializable{

 /**
  *
  */
 private static final long serialVersionUID = 1L;
 
 private String name;
 public User(){
  System.out.println("===============");
 }
 public User(String name){
  this.name = name;
 }

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }
 
 @Override
 public String toString() {
  return "user="+this.name;
 }

}

public class UserAction extends ActionSupport{

 /**
  *
  */
 private static final long serialVersionUID = 1L;
 private String name;
 private User user;
 private Cat cat;
 
 @Override
 public String execute() throws Exception {
 // System.out.println(cat.getFriend().getName());
  return ActionSupport.SUCCESS;
  
 }
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public User getUser() {
  return user;
 }
 public void setUser(User user) {
  this.user = user;
 }
 public Cat getCat() {
  return cat;
 }
 public void setCat(Cat cat) {
  this.cat = cat;
 }

}

  <s:property value="user.toString()"/>

posted @ 2013-04-15 22:15 杨军威 阅读(180) | 评论 (0)编辑 收藏

struts2控制反转时创建对象一定要有参数是空的实体类的构造方法

public class User implements Serializable{

 /**
  *
  */
 private static final long serialVersionUID = 1L;
 
 private String name;
 public User(){
  System.out.println("========空的构造方法=必须有======");
 }
 public User(String name){
  this.name = name;
 }

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }
 
 

}



public class UserAction extends ActionSupport{

 /**
  *
  */
 private static final long serialVersionUID = 1L;
 private String name;
 private User user;
 
 @Override
 public String execute() throws Exception {
  System.out.println(user.getName());
  return ActionSupport.SUCCESS;
  
 }
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public User getUser() {
  return user;
 }
 public void setUser(User user) {
  this.user = user;
 }

}

posted @ 2013-04-15 21:49 杨军威 阅读(536) | 评论 (0)编辑 收藏

struts2验证框架的使用

<body>
   <s:form action="user/tologin.action" method="post">
       <s:textarea name="id"></s:textarea>
       <s:password name="user.password"></s:password>
       <s:textfield name="user.name" label="ddddd" > </s:textfield>
       <s:submit value="提交" method="login"></s:submit>
       <s:submit value="打印" method="print"></s:submit>
   </s:form>
  </body>
验证框架的命名规则是Action类名字-action的bean名字-validation.xml,这个验证文件需要放在需要验证的.class文件同一目录下
例如UserAction-tologin-validation.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC
          "-//Apache Struts//XWork Validator 1.0.2//EN"
          "http://struts.apache.org/dtds/xwork-validator-1.0.2.dtd">
          <validators>
              <field name="id">
                  <field-validator type="int">
                      <param name="min">10</param>
                      <param name="max">20</param>
                      <message>必须是整数</message>
                  </field-validator>
              </field>
              
              <field name="user.name">
                  <field-validator type="requiredstring">
                      <message>name必须填写</message>
                  </field-validator>
              </field>
          </validators>

在struts-xml中的配置
<action name="tologin" class="UserAction" >
                <result>/WEB-INF/user/list.jsp</result>
                <result name="input">/WEB-INF/user/user.jsp</result>
            </action>

posted @ 2013-04-15 15:05 杨军威 阅读(136) | 评论 (0)编辑 收藏

struts2一个表单多种提交ognl

 <body>
   <s:form action="user/tologin.action" method="post">
       <s:textarea name="id"></s:textarea>
       <s:password name="user.password"></s:password>
       <s:textfield name="user.name"></s:textfield>
       <s:submit value="提交" method="login"></s:submit>
       <s:submit value="打印" method="print"></s:submit>
   </s:form>
  </body>
<action name="tologin" class="UserAction" >
                <result>/WEB-INF/user/list.jsp</result>
            </action>

public class UserAction extends ActionSupport{

    private static final long serialVersionUID = 1L;

    private User user;
    private String id;

    @Override
    public String execute() throws Exception {
        
        return ActionSupport.SUCCESS;
    }

    public String login(){

        return ActionSupport.SUCCESS;
    }

    public String print(){
        System.out.println(user.getName());
        System.out.println(user.getPassword());
        System.out.println(id);
        return ActionSupport.SUCCESS;
    }
    

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }
    
    
}

posted @ 2013-04-15 14:07 杨军威 阅读(167) | 评论 (0)编辑 收藏

struts2重定向到jsp传值

 <a href="hello?name=1">重定向传参数</a>
<package name="default" namespace="/" extends="struts-default">
 <default-action-ref name="hello"></default-action-ref>
  <action name="hello" class="UserAction">   
   <result type="redirect">/index2.jsp?t=${name}</result>
  </action>
 </package>

 

public class UserAction extends ActionSupport{

 /**
  *
  */
 private static final long serialVersionUID = 1L;
 private String name;
 @Override
 public String execute() throws Exception {
  System.out.println(name);
  return ActionSupport.SUCCESS;
  
 }
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }

}
  <s:property value="#parameters.t"/>

posted @ 2013-04-14 21:11 杨军威 阅读(698) | 评论 (0)编辑 收藏

struts2中四种实现request,session,application的方法

第一种
public class UserAction extends ActionSupport{

 /**
  *
  */
 private static final long serialVersionUID = 1L;
 private String name;
 private Map session;
 private Map request;
 private Map application;
 @Override
 public String execute() throws Exception {
  session =  ActionContext.getContext().getSession();
  request = (Map) ActionContext.getContext().get("request");
  application =  ActionContext.getContext().getApplication();
  session.put("sk", "sv");
  request.put("rk", "rv");
  application.put("ak", "av");
  System.out.println(name);
  return ActionSupport.SUCCESS;
  
 }
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }

}


第二种

public class DepAction extends ActionSupport implements RequestAware,SessionAware,ApplicationAware{

 private Map<String, Object> request;
 private Map<String, Object> session;
 private Map<String, Object> application;
 
 
 public void setRequest(Map<String, Object> request) {
  this.request = request;
  
 }

 public void setSession(Map<String, Object> session) {
  this.session = session;
  
 }

 public void setApplication(Map<String, Object> application) {
  this.application = application;
  
 }

}


第三种

public class CarAction implements ServletRequestAware{
 private HttpServletRequest request;
 private HttpSession session;
 private ServletContext application;

 public void setServletRequest(HttpServletRequest request) {
  this.request = request;
  this.session = request.getSession();
  this.application = session.getServletContext();
  
 }

}


第四种

public class HomeAction extends ActionSupport{
 
 private HttpServletRequest request;
 private HttpSession session;
 private ServletContext application;

 public HomeAction (){
  request = ServletActionContext.getRequest();
  session = request.getSession();
  application = session.getServletContext();
}

}

posted @ 2013-04-13 09:49 杨军威 阅读(167) | 评论 (0)编辑 收藏

struts2的标签ognl

     摘要: Struts2常用标签总结 一 介绍 1.Struts2的作用 Struts2标签库提供了主题、模板支持,极大地简化了视图页面的编写,而且,struts2的主题、模板都提供了很好的扩展性。实现了更好的代码复用。Struts2允许在页面中使用自定义组件,这完全能满足项目中页面显示复杂,多变的需求。 Struts2的标签库有一个巨大的改进之处,struts2标签库的标签不依赖于任何...  阅读全文

posted @ 2013-04-12 17:07 杨军威 阅读(231) | 评论 (0)编辑 收藏

仅列出标题
共43页: First 上一页 22 23 24 25 26 27 28 29 30 下一页 Last 

导航

统计

常用链接

留言簿

随笔档案

搜索

最新评论

阅读排行榜

评论排行榜