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数据就可以回显!
* 用户名:
密码:
*
@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表达式}
*
* 用户名:
密码:
*
@return
*
*/
public
String showDataFromMap(){
User user =
new User();
user.setPassword(
"111");
user.setUsername(
"zhangsan");
ActionContext. getContext().put(
"user"
, user);
return "user_map" ;
}
/**
* Action中的属性位于对象栈中,在回显时可以直接使用value来回显:
* 用户名:
密码:
*
@return
*/
public
String showDataFromActionProperty(){
this.username = "lisi" ;
this.password = "asfd" ;
return "user_actionproperty" ;
}
/**
* 使用s:select标签来显示下拉选择框,必须有list属性;
* jsp:选择省份:
* 翻译后的代码:
* 选择省份:
">
*
@return
*/
public
String putDataToSelect(){
List
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" ;
}
}
|
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
>
|
<%@
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">
用户名:
<s:textfield name="username" >s:textfield>< br/>
密码:
<s:password name= "password" showPassword="true" >s:password ><br/>
选择省份:
<s:select name= "pid" list="#pList" listKey="pid" listValue="pname" headerKey="" headerValue= "请选择省份" >s:select>< br/>
省份:
<s:checkboxlist list= "#pList" name="pid" listKey="pid" listValue="pname" >s:checkboxlist>
s:form>
body >
html
>
|
用户名:
密码:
选择省份:
省份:
|