ui标签:用在jsp页面用于回显数据的标签,这些标签是由框架定义的,用来替代原生的标签!
ui标签有:
- <s:textfield name="xxx"></s:textfield>
- <s:password name="xxx" showPassword="trus"></s:password>
- <s:select name="xxx" list="xx" listKey=.. listValue=.. headerValue=..><s/select>
- <s:checkboxlist list= "#pList" name="pid" listKey="pid" listValue="pname" ></s:checkboxlist>
需要回显的数据要放在valueStack中,可以放在map栈中,也可以放在对象栈中;
放对象栈中有两种情况:
一是将数据封装成bean,将bean放在对象栈中;
二是给action设置属性,action的属性就在对象栈中;
public class UIAction extends ActionSupport{
private
String username;
private
String password;
public
String getUsername() {
return username ;
}
public
void setUsername(String username) {
this.username = username;
}
public
String getPassword() {
return password ;
}
public
void setPassword(String password) {
this.password = password;
}
/**
* 1.在表单中回显数据:数据位于对象栈中,直接根据表单元素的name数据就可以回显!
* 用户名:<s:textfield name="username"></s:textfield><br/>
密码:<s:password name="password" showPassword="true"></s:password><br/>
*
@return
*/
public
String showDataFromObjStack(){
User user =
new User();
user.setPassword(
"aaa");
user.setUsername(
"bbb");
ActionContext. getContext().getValueStack().push(user);
return "user_objstack" ;
}
/**
* 从map栈中回显数据,s:textfield的value属性不能直接跟 ognl表达式,所以需要加%{ ognl表达式}
*
* 用户名:<s:textfield name=" username" value="%{#user.username}"></s:textfield><br/>
密码: <s:password name="password" showPassword="true" value="%{#user.password}"></s:password><br/>
*
@return
*
*/
public
String showDataFromMap(){
User user =
new User();
user.setPassword(
"111");
user.setUsername(
"zhangsan");
ActionContext. getContext().put(
"user"
, user);
return "user_map" ;
}
/**
* Action中的属性位于对象栈中,在回显时可以直接使用value来回显:
* 用户名:<s:textfield name="username"></s:textfield><br/>
密码: <s:password name="password" showPassword="true" ></s:password><br/>
*
@return
*/
public
String showDataFromActionProperty(){
this.username = "lisi" ;
this.password = "asfd" ;
return "user_actionproperty" ;
}
/**
* 使用s:select标签来显示下拉选择框,必须有list属性;
* jsp:选择省份:<s:select name=" pid" list="#pList" listKey="pid" listValue="pname" headerKey="" headerValue="请选择省份"></s:select> <br/>
* 翻译后的代码:
* 选择省份:
<select id="uiAction_form_action_pid" name="pid">
<option value="">请选择省份
</option>
<option value="1">北京市
</option>
<option value="2">上海市
</option>
<option value="3">重庆市
</option>
</select>
*
@return
*/
public
String putDataToSelect(){
List<Privice> pList =
new ArrayList<Privice>();
Privice privice1 =
new Privice();
privice1.setPid(1L);
privice1.setPname(
"北京市");
Privice privice2 =
new Privice();
privice2.setPid(2L);
privice2.setPname(
"上海市");
Privice privice3 =
new Privice();
privice3.setPid(3L);
privice3.setPname(
"重庆市");
pList.add(privice1);
pList.add(privice2);
pList.add(privice3);
//放在map栈中;
ActionContext. getContext().put(
"pList"
, pList);
return "select_map" ;
}
}
|
2.配置文件:struts-ui.xml:
<?
xml
version= "1.0" encoding ="UTF-8" ?>
<!
DOCTYPE
struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<
struts
>
<
package
name="ui" namespace="/" extends="struts-default" >
<action name= "uiAction_*" method ="{1}" class="cn.itheima03.struts2.ui.UIAction" >
<result name="user_objstack" >ui/ui.jsp</ result>
<result name= "user_map">ui/ui.jsp </result>
<result name="user_actionproperty" >ui/ui.jsp</ result>
<result name="select_map" >ui/ui.jsp</ result>
</action>
</
package
>
</
struts
>
|
2.jsp页面利用struts标签回显数据:
在jsp页面引用ui标签时,要加上:
<%@
taglib uri="/struts-tags" prefix="s"%>
<%@
page language="java" import= "java.util.*" pageEncoding="UTF-8" %>
<%@
taglib uri="/struts-tags" prefix= "s"%>
<!
DOCTYPE
HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<
html
>
<body >
<s:form action= "uiAction_form.action">
<!--
cssClass相当于 html标签中的class
-->
用户名:
<s:textfield name="username" ></s:textfield>< br/>
密码:
<s:password name= "password" showPassword="true" ></s:password ><br/>
<!--
list是select填充数据的来源
list后面能直接跟 ognl表达式
listKey是option中value的值
listValue是要显示的值
headerValue顶部要显示的内容
-->
<!-- s:select标签 -->
选择省份:
<s:select name= "pid" list="#pList" listKey="pid" listValue="pname" headerKey="" headerValue= "请选择省份" ></s:select>< br/>
<!-- checkboxlist 标签 -->
省份:
<s:checkboxlist list= "#pList" name="pid" listKey="pid" listValue="pname" ></s:checkboxlist>
</s:form>
</body >
</
html
>
|
效果图:
翻译过后的源代码:
用户名:
密码:
选择省份:
<br>
省份:
</form>
</body>
|