<body> <h1>登录页面</h1> <s:debug></s:debug> <s:form method="POST" action="login.action"> <s:bean name="com.zchen.model.User" id="u"></s:bean> <s:bean name="com.zchen.action.FormAction" id="fa"></s:bean> <s:textfield name="user.username" value="%{#u.username}" label="Username" required="true" requiredposition="right"></s:textfield> <!--value:从后台取值填充 %{}动态从值栈 取 label:自动在名称后面加: required:必填项会自动出现* requiredposition:*号出现的位置(right or left) --> <s:password name="user.password" label="Password"></s:password> <s:radio list="#{1:'男',2:'女'}" name="user.sex" label="性别" value="1"></s:radio> <!-- value:默认选中项 list:可以从后台取 --> <s:checkbox label="婚否" name="user.hf" value="true"></s:checkbox> <!-- 此checkbook只有一个选项 value:true or false --> <s:checkboxlist name="user.love" label="爱好" list="{'足球','篮球','排球','网球'}"></s:checkboxlist> <s:checkboxlist name="user.love" label="爱好" list="#{1:'足球',2:'篮球',3:'排球',4:'网球'}"></s:checkboxlist> <!-- 前者保存的是文字后者保存的是数字(一般用map保存数字的节省资源防止乱码) --> <s:select list="#fa.provice" name="user.provice" label="籍贯" ></s:select> <!-- 可以放在后台赋值 在后台新建一个Action 用<s:bean>接受 --> <s:select list="#fa.provice" name="user.provice" label="籍贯" ></s:select> <!-- 利用action的result给select赋值在struts.xml中配置action 注意的是不能直接访问此页面要先访问FormAction --> <s:submit value="提交"></s:submit> </s:form> </body>
由于select标签利用了从后台去值,所以引入了:
package com.zchen.action;
import java.util.HashMap;
import java.util.Map;
import com.opensymphony.xwork2.ActionSupport;
public class FormAction extends ActionSupport {
private static final long serialVersionUID = 4387832093273420762L;
private Map<Integer,String> provice;
public Map<Integer,String> getProvice() {
return provice;
}
public void setProvice(Map<Integer,String> provice) {
this.provice = provice;
}
public FormAction(){
provice = new HashMap<Integer,String>();
provice.put(1,"山东省");
provice.put(2,"山西省");
provice.put(3,"湖南省");
provice.put(4,"湖北省");
provice.put(5,"北京省");
provice.put(6,"广东省");
provice.put(7,"广西省");
provice.put(8,"河南省");
provice.put(9,"河北省");
provice.put(10,"天津省");
}
@Override
public String execute() throws Exception {
return SUCCESS;
}}
将值传给表单login.jsp页面的选择标签分两种情况:
1.利用bean属性;
2.配置action:
<?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="annotation" extends="struts-default">
<action name="login" class="com.zchen.action.LoginAction" >
<result name="success">index.jsp</result>
</action>
<action name="formAction" class="com.zchen.action.FormAction">
<result>/login.jsp</result>
</action>
</package>
</struts>
package com.zchen.model;
public class User {
private String username = "请在此填写用户名";
private String password;
private int sex;
private String[] love;
private boolean hf;
private String provice;
public String getProvice() {
return provice;
}
public void setProvice(String provice) {
this.provice = provice;
}
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;
}
public int getSex() {
return sex;
}
public void setSex(int sex) {
this.sex = sex;
}
public String[] getLove() {
return love;
}
public void setLove(String[] love) {
this.love = love;
}
public boolean isHf() {
return hf;
}
public void setHf(boolean hf) {
this.hf = hf;
}
}
package com.zchen.action;
import com.opensymphony.xwork2.ActionSupport;
import com.zchen.model.User;
public class LoginAction extends ActionSupport {
private static final long serialVersionUID = 4387832093273420762L;
private User user = null;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
@Override
public String execute() throws Exception {
return SUCCESS;
}
}
<body> <s:property value="user.username"/><br> <s:property value="user.password"/><br> <s:property value="user.sex"/><br> <s:property value="user.love"/><br> <s:property value="user.hf"/><br> <s:property value="user.provice"/><br> </body>