Fork me on GitHub

Struts2(十一)OGNL标签三与Struts2标签

一、UI标签

 

二、简单例子

 

<h1>添加信息</h1>
<!--通过指定theme的属性改变主题  -->
<s:form action="" method="psot"  theme="simple">
房屋标题:<s:textfield  id="title" name="title"></s:textfield><br/>
房屋描述:<s:textarea value="房屋介绍 ...." name="desc"></s:textarea><br/>
<s:submit value="提交"></s:submit>
</s:form>

或者在struts.xml中配置常量

<constant name="struts.ui.theme" value="simple" />

 

三、s:select

 

四、Ajax标签

 

更改模板后,要在src下建立相遇的包和文件才可以使用

五、简单例子

 实体类

package com.pb.entity;
/*
 * 地区类
 */
public class District {
    
    private int districtId;            //地区编号
    private String districtName;    //地区名称
    
    public District() {
        super();
        // TODO Auto-generated constructor stub
    }
    public District(int districtId, String districtName) {
        super();
        this.districtId = districtId;
        this.districtName = districtName;
    }
    public int getDistrictId() {
        return districtId;
    }
    public String getDistrictName() {
        return districtName;
    }
    public void setDistrictId(int districtId) {
        this.districtId = districtId;
    }
    public void setDistrictName(String districtName) {
        this.districtName = districtName;
    }
    
    

}
package com.pb.entity;
/*
 * 街道
 */
public class Street {

    
        private int streetId;            //街道ID
        private String streetName;    //街道名称
        private District district;        //街道所在地区
        public Street() {
            super();
            // TODO Auto-generated constructor stub
        }
        public Street(int streetId, String streetName, District district) {
            super();
            this.streetId = streetId;
            this.streetName = streetName;
            this.district = district;
        }
        public District getDistrict() {
            return district;
        }
        public int getStreetId() {
            return streetId;
        }
        public String getStreetName() {
            return streetName;
        }
        public void setDistrict(District district) {
            this.district = district;
        }
        public void setStreetId(int streetId) {
            this.streetId = streetId;
        }
        public void setStreetName(String streetName) {
            this.streetName = streetName;
        }
        
        
}
package com.pb.entity;

import java.util.Date;
/*
 * 房屋
 */
public class House {
    
    
    private int houseId;    //房屋ID
    private String title;        //房屋标题
    private String desc;   //房屋描述
    private Street street;//房屋所在街道
    private Date addDate;//房屋发布日期

    public House() {
        super();
        // TODO Auto-generated constructor stub
    }
    public House(int houseId, String title, String desc, Date addDate,
            Street street) {
        super();
        this.houseId = houseId;
        this.title = title;
        this.desc=desc;
        this.addDate = addDate;
        this.street = street;
    }
    public Date getAddDate() {
        return addDate;
    }
    
    public int getHouseId() {
        return houseId;
    }
    public Street getStreet() {
        return street;
    }
    public String getTitle() {
        return title;
    }
    public void setAddDate(Date addDate) {
        this.addDate = addDate;
    }
    
    public void setHouseId(int houseId) {
        this.houseId = houseId;
    }
    public void setStreet(Street street) {
        this.street = street;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getDesc() {
        return desc;
    }
    public void setDesc(String desc) {
        this.desc = desc;
    }

}
package com.pb.entity;
/*
 * 类型
 */
public class HouseType {
    private int typeId;
    private String type;
    public HouseType() {
        super();
        // TODO Auto-generated constructor stub
    }
    public HouseType(int typeId, String type) {
        super();
        this.typeId = typeId;
        this.type = type;
    }
    public int getTypeId() {
        return typeId;
    }
    public void setTypeId(int typeId) {
        this.typeId = typeId;
    }
    public String getType() {
        return type;
    }
    public void setType(String type) {
        this.type = type;
    }
    

}

action

package com.pb.web.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.opensymphony.xwork2.ActionSupport;
import com.pb.entity.District;
import com.pb.entity.HouseType;
import com.pb.entity.Street;

public class AddAction extends ActionSupport {
    private List<HouseType> types; // 房屋类型
    private Map<District, List<Street>> map;// 地区-街道

    public List<HouseType> getTypes() {
        return types;
    }

    public void setTypes(List<HouseType> types) {
        this.types = types;
    }

    public Map<District, List<Street>> getMap() {
        return map;
    }

    public void setMap(Map<District, List<Street>> map) {
        this.map = map;
    }

    public String addHouse() {
        types = new ArrayList<HouseType>();
        types.add(new HouseType(1, "单间"));
        types.add(new HouseType(2, "一房一厅"));
        types.add(new HouseType(3, "两室一厅"));
        types.add(new HouseType(4, "两室两厅"));

        //
        // 声明地区
        District dt1 = new District(75501, "宝安区");
        District dt2 = new District(75502, "南山区");
        // 声明街道
        Street street1 = new Street(74, "布心一村", dt1);
        Street street2 = new Street(25, "商业街", dt1);
        Street street3 = new Street(87, "科技园", dt2);
        Street street4 = new Street(99, "南头古城", dt2);
        List<Street> streets1=new ArrayList<Street>();
        List<Street> streets2=new ArrayList<Street>();
        streets1.add(street1);
        streets1.add(street2);
        streets2.add(street3);
        streets2.add(street4);
        
        map=new HashMap<District, List<Street>>();
        map.put(dt1, streets1);
        map.put(dt2, streets2);
        return SUCCESS;
    }

}
package com.pb.web.action;

import java.util.Date;

import com.opensymphony.xwork2.ActionSupport;
import com.pb.entity.House;

public class HouseAction extends ActionSupport {
    private String title;
    private String houseType;
    private String district;
    private String street;
    private Date addDate;
    private String desc;
    
    
    @Override
    public String execute() throws Exception {
        System.out.println(title);
        System.out.println(houseType);
        System.out.println(district);
        System.out.println(street);
        System.out.println(addDate);
        System.out.println(desc);
        if(title.equals("")){
            return ERROR;
        }
        if(desc.equals("")){
            return INPUT;
        }
        
        return SUCCESS;
    }


    public String getTitle() {
        return title;
    }


    public void setTitle(String title) {
        this.title = title;
    }


    public String getHouseType() {
        return houseType;
    }


    public void setHouseType(String houseType) {
        this.houseType = houseType;
    }


    public String getDistrict() {
        return district;
    }


    public void setDistrict(String district) {
        this.district = district;
    }


    public String getStreet() {
        return street;
    }


    public void setStreet(String street) {
        this.street = street;
    }


    public Date getAddDate() {
        return addDate;
    }


    public void setAddDate(Date addDate) {
        this.addDate = addDate;
    }


    public String getDesc() {
        return desc;
    }


    public void setDesc(String desc) {
        this.desc = desc;
    }

}

struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
    <constant name="struts.enable.DynamicMethodInvocation" value="false" />
    <constant name="struts.devMode" value="true" />
    
<!--struts.ui.theme  -->
    <package name="default" namespace="/" extends="struts-default">
    
    <action name="house" class="com.pb.web.action.AddAction" method="addHouse">
    <result name="success">
     /addHouse.jsp
    </result>
    </action>
    <action name="add" class="com.pb.web.action.HouseAction">
    <result name="success">
      /addSuccess.jsp
    </result>
    <!-- 使用chain返回时,注册填写的信息仍然存在 -->
     <result name="error" type="chain">
          <param name="actionName">house</param>
          <param name="namespace">/</param>
    </result>
    <!-- 使用chain返回时,注册填写的信息仍然存在 -->
    <result name="input" type="chain">
    <param name="actionName">house</param>
    <param name="namespace">/</param>
    </result>
    </action>
    
    </package>
</struts>

jsp页面 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ taglib prefix="sx" uri="/struts-dojo-tags" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://"
            + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<sx:head parseContent="true"/>
<base href="<%=basePath%>">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>添加信息</title>
</head>
<body>
    <h1>添加信息</h1>
    <!--通过指定theme的属性改变主题  -->
    <!--  此处一定要加name属性,不然下面2个下拉框必然报错-->
    <s:form action="add.action" method="post" theme="simple" name="name">
房屋标题:<s:textfield id="title" name="title"></s:textfield>
        <br />
房屋类型:<s:select name="houseType" list="types" listKey="typeId"
            listValue="type" />
        <br />
房屋地址:

<s:doubleselect name="district" list="map.keySet()" listKey="districtId"
            listValue="districtName" doubleName="street"
            doubleList="map.get(top)" doubleListKey="streetId"
            doubleListValue="streetName" theme="doubleselect" />
        <br /><br/>
        
发布日期<sx:datetimepicker name="addDate" label="" /><br/><br/>
房屋描述:<s:textarea  name="desc"></s:textarea>
        <br />
        <s:submit value="提交"></s:submit>
    </s:form>
    <s:debug />
</body>
</html>

 

posted @ 2015-04-01 00:24  森林森  阅读(612)  评论(0编辑  收藏  举报