struts2表单回显

1.效果

014758_Ns03_3015803.png

--------------------------------------------------------------------------

020917_TCWJ_3015803.png

2.Dao.java

package com.huangliusong.app;

import java.util.ArrayList;
import java.util.List;

public class Dao {
	public List<Department> getdDepartments() {
		List<Department> departments=new ArrayList<Department>();
		departments.add(new Department(1001,"技术部1"));
		departments.add(new Department(1002,"技术部2"));
		departments.add(new Department(1003,"技术部3"));
		departments.add(new Department(1004,"技术部4"));
		departments.add(new Department(1005,"技术部5"));
		return departments;
	}

	public List<Role> getRoles() {
		List<Role> roles=new ArrayList<Role>();
		roles.add(new Role(2001,"角色1"));
		roles.add(new Role(2002,"角色2"));
		roles.add(new Role(2003,"角色3"));
		roles.add(new Role(2004,"角色4"));
		return roles;
	}
}


3Department.java

package com.huangliusong.app;

public class Department {
	public Integer getDepartmentId() {
		return departmentId;
	}
	public void setDepartmentId(Integer departmentId) {
		this.departmentId = departmentId;
	}
	public String getDepartmentName() {
		return departmentName;
	}
	@Override
	public String toString() {
		return "Department [departmentId=" + departmentId + ", departmentName="
				+ departmentName + "]";
	}
	public Department(Integer departmentId, String departmentName) {
		super();
		this.departmentId = departmentId;
		this.departmentName = departmentName;
	}
	public Department() {
		super();
		// TODO Auto-generated constructor stub
	}
	public void setDepartmentName(String departmentName) {
		this.departmentName = departmentName;
	}
	private Integer departmentId;
	private String departmentName;
	
}


4Employee.java

package com.huangliusong.app;

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;
	Dao dao=new Dao();
	public String inputs(){
		requestMap.put("departments",dao.getdDepartments());
		requestMap.put("roles", dao.getRoles());
		System.err.println("input()-----------");
		return "inputs";
	}
	
	@Override
	public void setRequest(Map<String, Object> requestM) {
		// TODO Auto-generated method stub
		this.requestMap=requestM;
	}
	private String name;
	private String password;
	private String gender;
	public Dao getDao() {
		return dao;
	}

	public void setDao(Dao dao) {
		this.dao = dao;
	}

	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 getDepartments() {
		return departments;
	}

	public void setDepartments(String departments) {
		this.departments = departments;
	}

	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;
	}
	private String departments;
	private List<String> roles;
	private String desc;
	
	@Override
	public String toString() {
		return "Employee [name=" + name + ", password=" + password
				+ ", gender=" + gender + ", departments=" + departments
				+ ", roles=" + roles + ", desc=" + desc + "]";
	}

	public String save(){
		System.err.println("save"+this);
		return "save";
	}
}


5Role.java

package com.huangliusong.app;

public class Role {
	private Integer roleId;
	private String roleName;
	public Integer getRoleId() {
		return roleId;
	}
	@Override
	public String toString() {
		return "Role [roleId=" + roleId + ", roleName=" + roleName + "]";
	}
	public Role() {
		super();
		// TODO Auto-generated constructor stub
	}
	public Role(Integer roleId, String roleName) {
		super();
		this.roleId = roleId;
		this.roleName = roleName;
	}
	public void setRoleId(Integer roleId) {
		this.roleId = roleId;
	}
	public String getRoleName() {
		return roleName;
	}
	public void setRoleName(String roleName) {
		this.roleName = roleName;
	}
	
}

struts.xml、

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
	<constant name="struts.ognl.allowStaticMethodAccess" value="true"></constant>
	<package name="default" namespace="/" extends="struts-default">
		<!-- 可以配置全局exception -->
			<global-results>
				<result name="input">/WEB-INF/pages/input.jsp</result>
		</global-results>
		<global-exception-mappings>
			<exception-mapping result="input"
				exception="java.lang.ArithmeticException"></exception-mapping>
		</global-exception-mappings>
	<action name="emp-*" class="com.huangliusong.app.Employee" method="{1}">
			<result name="{1}">{1}.jsp</result>
		</action>
		<action name="Product_input">
			<result>/WEB-INF/pages/input.jsp</result>
		</action>
		<action name="pruduct_save" class="com.huangliusong.entities.Product"
			method="save">
			<result name="details">/WEB-INF/pages/details.jsp</result>
		</action>
		<action name="testTag" class="com.huangliusong.entities.Product" method="testTag">
			<result>common-tag.jsp</result>
		</action>
		<action name="save" class="com.huangliusong.entities.UserAction" method="save">
			<result name="input">form-tag.jsp</result>
		</action>
		
		<action name="emp-save" class="com.huangliusong.app.Employee" method="save">
			<result name="save">emp-save.jsp</result>
		</action>
	</package>
</struts>    

inputs.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>input页面</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
  </head>
  
  <body>
  <s:form action="emp-save" method="">
  		<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.departments" listKey="departmentId" listValue="departmentName" name="departments" label="departments">
  		</s:select>
  		<s:checkboxlist list="#request.roles" listKey="roleId" listValue="roleName" label="roles" name="roles"></s:checkboxlist>
  		<s:textarea name="desc" label="desc"></s:textarea>
  		<s:submit></s:submit>
  </s:form>
   <h1>
   	input页面
   </h1>
  </body>
</html>

emp-save.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'emp-save.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
    name:${name }<br>
    password:${password }<br>
    gender:${gender }<br>
    desc:${desc }<br>
    departments:${departments }<br>
    roles:${roles }<br>
  </body>
</html>

 

转载于:https://my.oschina.net/liusonghuang/blog/830613

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值