码迷,mamicode.com
首页 > 其他好文 > 详细

Struts2--属性设置方式

时间:2016-09-25 20:34:59      阅读:134      评论:0      收藏:0      [点我收藏+]

标签:

Struts2自动获取/设置数据的方式一共分为两种

  • 属性驱动(FieldDriven)
  • 模型驱动(ModelDriven)
  1.  属性驱动

      属性又分为两种:

        |- 基本数据类型

        |- JavaBean属性类型

基本数据类型:实例

 

 1 <%@ page language="java" pageEncoding="UTF-8"%>
 2 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 3 <html>
 4 <head>
 5 <title>Insert title here</title>
 6 </head>
 7 <body>
 8     <form action="hello" method="get">
 9         姓名;<input type="text" name="name" size="10px" value="fuwh" readonly="readonly"/>&nbsp;
10         年龄:<input type="text" name="age" size="10px" value="23" readonly="readonly"/>
11         <button type="submit">提交</button>
12     </form>
13 </body>
14 </html>
package com.fuwh.struts;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ModelDriven;

public class HelloAction implements Action{

    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;
    }
    @Override
    public String execute() throws Exception {
        // TODO Auto-generated method stub
        System.out.println("执行了HelloAction的默认方法");
        return SUCCESS;
    }

}
 1 <%@ page language="java" pageEncoding="UTF-8"%>
 2 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 3 <html>
 4 <head>
 5 <title>Insert title here</title>
 6 </head>
 7 <body>
 8 欢迎${age }岁的${name }光临
 9 </body>
10 </html>
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE struts PUBLIC
 3     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
 4     "http://struts.apache.org/dtds/struts-2.3.dtd">
 5 
 6 <struts>
 7     <package name="mypack" extends="struts-default">
 8         <action name="hello" class="com.fuwh.struts.HelloAction">
 9             <result name="success">hello.jsp</result>
10         </action>
11     </package>
12 </struts>

 

JavaBean属性类型

 1 package com.fuwh.struts;
 2 
 3 public class User implements java.io.Serializable{
 4 
 5     private static final long serialVersionUID = 1L;
 6     
 7     private String name;
 8     private int age;
 9     
10     public String getName() {
11         return name;
12     }
13     public void setName(String name) {
14         this.name = name;
15     }
16     public int getAge() {
17         return age;
18     }
19     public void setAge(int age) {
20         this.age = age;
21     }
22     
23 }
 1 <%@ page language="java" pageEncoding="UTF-8"%>
 2 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 3 <html>
 4 <head>
 5 <title>Insert title here</title>
 6 </head>
 7 <body>
 8     <form action="hello" method="get">
 9         姓名;<input type="text" name="user.name" size="10px" value="fuwh" readonly="readonly"/>&nbsp;
10         年龄:<input type="text" name="user.age" size="10px" value="23" readonly="readonly"/>
11         <button type="submit">提交</button>
12     </form>
13 </body>
14 </html>
 1 package com.fuwh.struts;
 2 
 3 import com.opensymphony.xwork2.Action;
 4 
 5  public class HelloAction implements Action{ 
 6 
 7    private User user 
 8 
 9    public User getUser() {
10         return user;
11     }
12     public void setUser(User) {
13         this.user = user;
14     }
15     @Override
16     public String execute() throws Exception {
17         // TODO Auto-generated method stub
18         System.out.println("执行了HelloAction的默认方法");
19         return SUCCESS;
20     }
21 
22 }
 1 <%@ page language="java" pageEncoding="UTF-8"%>
 2 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 3 <html>
 4 <head>
 5 <title>Insert title here</title>
 6 </head>
 7 <body>
 8 欢迎${user.age }岁的${user.name }光临
 9 </body>
10 </html>

struts.xml配置

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE struts PUBLIC
 3     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
 4     "http://struts.apache.org/dtds/struts-2.3.dtd">
 5 
 6 <struts>
 7     <package name="mypack" extends="struts-default">
 8         <action name="hello" class="com.fuwh.struts.HelloAction">
 9             <result name="success">hello.jsp</result>
10         </action>
11     </package>
12 </struts>

 

 

模型驱动

 1 <%@ page language="java" pageEncoding="UTF-8"%>
 2 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 3 <html>
 4 <head>
 5 <title>Insert title here</title>
 6 </head>
 7 <body>
 8     <form action="hello" method="get">
 9         姓名;<input type="text" name="name" size="10px" value="fuwh" readonly="readonly"/>&nbsp;
10         年龄:<input type="text" name="age" size="10px" value="23" readonly="readonly"/>
11         <button type="submit">提交</button>
12     </form>
13 </body>
14 </html>
 1 package com.fuwh.struts;
 2 
 3 import com.opensymphony.xwork2.Action;
 4 import com.opensymphony.xwork2.ModelDriven;
 5 
 6 public class HelloAction implements Action,ModelDriven{
 7 
 8     private User user=new User();
 9     
10     @Override
11     public String execute() throws Exception {
12         // TODO Auto-generated method stub
13         System.out.println("执行了HelloAction的默认方法mae");
14         return SUCCESS;
15     }
16     @Override
17     public User getModel() {
18         // TODO Auto-generated method stub
19         System.out.println("执行了getModel方法");
20         return this.user;
21     }
22 }
 1 <%@ page language="java" pageEncoding="UTF-8"%>
 2 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 3 <html>
 4 <head>
 5 <title>Insert title here</title>
 6 </head>
 7 <body>
 8 欢迎${age }岁的${name }光临
 9 </body>
10 </html>
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE struts PUBLIC
 3     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
 4     "http://struts.apache.org/dtds/struts-2.3.dtd">
 5 
 6 <struts>
 7     <package name="mypack" extends="struts-default">
 8         <action name="hello" class="com.fuwh.struts.HelloAction">
 9             <result name="success">hello.jsp</result>
10         </action>
11     </package>
12 </struts>

 

Struts2--属性设置方式

标签:

原文地址:http://www.cnblogs.com/zerotomax/p/5906578.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!