Struts2的表单标签

来源:互联网 发布:朱元璋 知乎 编辑:程序博客网 时间:2024/05/17 23:07

1.表单的简单jsp页面

<%@ page language="java" contentType="text/html; charset=utf-8"    pageEncoding="utf-8"%><%@ taglib prefix="s" uri="/struts-tags" %><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Insert title here</title></head><body>    <s:form action="empsave" namespace="/">        <s:textfield name="name" label="name"></s:textfield>        <s:password name="password" label="Password"></s:password>        <s:radio name="gender"             list="#{'1':'Male','0':'Female' }" label="Gender">        </s:radio>        <!-- 服务器与需要使用集合类型,以保证正常的回显 -->        <s:select list="#request.depts" listKey="deptId"            listValue="deptName" label="Department" name="dept" >        </s:select>        <s:checkboxlist list="#request.roles" listKey="roleId"            listValue="roleName" label="Role" name="roles">        </s:checkboxlist>        <s:textarea name="desc" label="Desc"></s:textarea>        <s:submit></s:submit>    </s:form></body></html>

2.Employee.java文件

实现RequestAware接口进行访问数据

package FormAction;import java.util.List;import java.util.Map;import org.apache.struts2.interceptor.RequestAware;public class Employee implements RequestAware{    private Map<String,Object> requestMap=null;    private Dao dao=new Dao();    private String name;    private String password;    private String gender;    private String dept;    private List<String> roles;    private String desc;    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public String getPassword() {        return password;    }    public void setPassword(String password) {        this.password = password;    }    public String getGender() {        return gender;    }    public void setGender(String gender) {        this.gender = gender;    }    public String getDept() {        return dept;    }    public void setDept(String dept) {        this.dept = dept;    }    public List<String> getRoles() {        return roles;    }    public void setRoles(List<String> roles) {        this.roles = roles;    }    public String getDesc() {        return desc;    }    public void setDesc(String desc) {        this.desc = desc;    }    public String save()    {        System.out.println("save: "+this);        return "save";    }    public String input()    {        requestMap.put("depts", dao.getDepartments());        requestMap.put("roles", dao.getRole());        return "input";    }    @Override    public void setRequest(Map<String, Object> request) {        this.requestMap=request;    }    @Override    public String toString() {        return "Employee [name=" + name + ", password=" + password + ", gender=" + gender + ", dept=" + dept                + ", roles=" + roles + ", desc=" + desc + "]";    }}

3.struts2.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.ognl.allowStaticMethodAccess" value="true"></constant><!-- 配置Struts可以受理的请求的扩展名 --><constant name="struts.action.extension" value="action,do,"></constant><!-- 打开允许动态方法的开关,默认是false --><constant name="struts.enable.DynamicMethodInvocation" value="true"></constant><package extends="struts-default" name="Hello" namespace="/">       <action name="empinput" class="FormAction.Employee"        method="input">        <result name="input">/emp-input.jsp</result>        </action>    <action name="empsave" class="FormAction.Employee"        method="save">        <result name="save">/emp-save.jsp</result>      </action></package></struts>

4.要跳转接收数据的jsp页面

<%@ page language="java" contentType="text/html; charset=utf-8"    pageEncoding="utf-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Insert title here</title></head><body>    Name:${name }    <br>    Password:${password }    <br>    Gender:${gender }    <br>    Dept:${dept }    <br>    Roles:${roles }    <br>    Desc:${desc }</body></html>
原创粉丝点击