• Struts2标签之<s:select>


    根据MVC设计模式,JSP页面是不能直接访问的,而是需要通过Servlet或者Struts2中的action进行跳转访问;

    <s:select>标签的内容并不能像HTML一样,直接在页面中设置,而是需要通过读取Action的属性来进行设置;

    在HTML中,代码如下:

        <form>
        	<select name="name">
        		<option value="v1">v1</option>
        		<option value="v2">v2</option>
        	</select>
        </form>

    而在Struts2中,<s:select>的流程如下:



    Action:

    public class ConfigCustomerInfoAction extends ActionSupport {
    	private List<CustomerInfo> typeList = new ArrayList<CustomerInfo>();
    	public String execute()throws Exception{
    		CustomerInfo info1 = new CustomerInfo();
    		info1.setTypeName("normal");
    		info1.setTypeValue("普通用户");
    		typeList.add(info1);
    		CustomerInfo info2 = new CustomerInfo();
    		info2.setTypeName("special");
    		info2.setTypeValue("会员用户");
    		typeList.add(info2);
    		return SUCCESS;
    		
    	}
    	public List<CustomerInfo> getTypeList() {
    		return typeList;
    	}
    	public void setTypeList(List<CustomerInfo> typeList) {
    		this.typeList = typeList;
    	}
    }
    

    struts.xml

    <action name="configCustomerInfo" class="org.xiazdong.action.ConfigCustomerInfoAction">
    	<result>/fillCustomerInfo.jsp</result>
    </action>
    vo

    package org.xiazdong.vo;
    
    public class CustomerInfo {
    	private String typeName;
    	private String typeValue;
    	public String getTypeName() {
    		return typeName;
    	}
    	public void setTypeName(String typeName) {
    		this.typeName = typeName;
    	}
    	public String getTypeValue() {
    		return typeValue;
    	}
    	public void setTypeValue(String typeValue) {
    		this.typeValue = typeValue;
    	}
    	
    }
    JSP
    <s:select list="typeList" name="type" listKey="typeName" listValue="typeValue" label="用户类型"></s:select>

    注意点:

    (1)各种getter和setter方法都要有,毕竟Struts2框架就是依靠反射的。



  • 相关阅读:
    PHP 上传文件 function move_uploaded_file: failed to open stream
    python super() 方法使用
    python 负数转为无符号整数
    python Aes 加密 解密
    mongoDB 启动 Error: couldn't connect to server 127.0.0.1:27017 src/mongo/shell/mongo.js:91
    ansible使用
    字段类型binary
    7-14 求整数段和
    7-13 日K蜡烛图
    7-12 两个数的简单计算器
  • 原文地址:https://www.cnblogs.com/xiazdong/p/3058054.html
Copyright © 2020-2023  润新知