Struts2提交表单实例和struts主题

经过上一篇文章的学习,我们来做一个实例

要求emp-input.jsp表单里的s:select标签和s:checkboxlist标签由数据库数据传入

在这里我们做简化,在Dao里直接写入数据

下面给出实例



struts.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>
	

    <package name="wul" namespace="/" extends="struts-default">
   		<action name="emp-*" class="com.wul.struts2.app.Emloyee"
   			method="{1}">
   			<result name="{1}">/emp-{1}.jsp</result>
   		</action> 
    </package>

</struts>


index.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=utf-8">
<title>Insert title here</title>
</head>
<body>
	<a href="emp-input.action">Emp Input Page</a>
</body>
</html>


Role.java

package com.wul.struts2.app;

public class Role {
	private Integer roleId;
	private String roleName;
	public Integer getRoleId() {
		return roleId;
	}
	public void setRoleId(Integer roleId) {
		this.roleId = roleId;
	}
	public String getRoleName() {
		return roleName;
	}
	public void setRoleName(String roleName) {
		this.roleName = roleName;
	}
	public Role() {
		super();
	}
	public Role(Integer roleId, String roleName) {
		super();
		this.roleId = roleId;
		this.roleName = roleName;
	}
	
}

Department.java

package com.wul.struts2.app;

public class Department {
	private Integer deptId;
	private String deptName;
	
	public Integer getDeptId() {
		return deptId;
	}
	public void setDeptId(Integer deptId) {
		this.deptId = deptId;
	}
	public String getDeptName() {
		return deptName;
	}
	public void setDeptName(String deptName) {
		this.deptName = deptName;
	}
	public Department() {

	}
	public Department(Integer deptId, String deptName) {
		this.deptId = deptId;
		this.deptName = deptName;
	}
	
	
	
}
Dao.java

package com.wul.struts2.app;

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

public class Dao {
	
	public  List<Department> getDepartments(){
		
		List<Department> depts = new ArrayList<>();
		depts.add(new Department(1001,"AAA"));
		depts.add(new Department(1002,"BBB"));
		depts.add(new Department(1003,"CCC"));
		depts.add(new Department(1004,"DDD"));
		depts.add(new Department(1005,"EEE"));
		
		return depts;
	}
	
	public List<Role> getRoles(){
		List<Role> roles = new ArrayList<>();
		roles.add(new Role(2001,"AAA"));
		roles.add(new Role(2002,"BBB"));
		roles.add(new Role(2003,"CCC"));
		roles.add(new Role(2004,"DDD"));
		roles.add(new Role(2005,"EEE"));
		
		return roles;
	}
	
	public Dao(){
		
	}
}

Emloyee.java

package com.wul.struts2.app;

import java.util.List;
import java.util.Map;

import org.apache.struts2.interceptor.RequestAware;

public class Emloyee 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 input(){
		requestMap.put("depts", dao.getDepartments());
		requestMap.put("roles", dao.getRoles());
		return "input";
	}
	
	public String save(){
		System.out.println("save:"+this);
		return "save";
	}
	
	@Override
	public void setRequest(Map<String, Object> requestMap) {
		// TODO 自动生成的方法存根
		this.requestMap = requestMap;
	}

	@Override
	public String toString() {
		return "Emloyee [name=" + name + ", password=" + password + ", gender="
				+ gender + ", dept=" + dept + ", roles=" + roles + ", desc="
				+ desc + "]";
	}
	
	
}

emp-input.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=utf-8" />
<title>Insert title here</title>
</head>
<body>
	
	
	<s:form action="emp-save" >
		
		<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"
			 name="dept"
			 label="Department">
		</s:select>
	
		<s:checkboxlist list="#request.roles"
			listKey="roleId"
			listValue="roleName"
			name="roles"
			label="Role">
		</s:checkboxlist>
	
		<s:textarea name="desc" label="Desc"></s:textarea>
		
		<s:submit></s:submit>                                                                   
		
		
	</s:form>
	
</body>
</html>

emp-save.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=utf-8" />
<title>Insert title here</title>
</head>
<body>
	
		Name: ${name }
	<br><br>
		Password: ${password }
	<br><br>
		Gender: ${gender }
	<br><br>
		Dept: ${dept }
	<br><br>
		Roles: ${roles }
	<br><br>
		Desc: ${name }
	<br><br>
</body>
</html>

web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>struts2_2</display-name>
  
  
   <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
  
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>


struts主题

默认情况下,form标签将呈现为一个HTML form元素和一个tabel元素

每一种输入标签都将呈现为一个带标号的输入元素,而这个输入元素将被包含在一个tr元素和td元素的内部。

主题:为了让所有的UI标签能够产生同样的视觉效果而归集到一起的一组模版,即风格相近的模版被打包为一个主题。

--simple 把UI标签翻译成最简单的HTML对应元素,而且会忽略行标属性
--xhtml:xhtml是默认的主题,这个主题的模版通过使用一个布局表格提供了一种自动化的排版机制。
css xhtml:这个主题里的模版与html主题里的模版很相似,但他们将使用css来进行布局和排版
--ajax:这个主题里的模版以xhtml主题里模版为基础,但加入了一些Ajax功能


修改主题
--通过UI标签的theme属性

	<s:form action="emp-save" theme="simple">

--在一个表单里,若没有给出某个UI标签的theme属性,它将使用这个表单的主题

--在page,request,session或application中添加一个theme属性

	<%
	
		request.setAttribute("theme", "simple");
	
	%>


--修改struts.properties文件中的struts.ui.theme属性。

<!--修改当前struts应用的主题  -->

	<constant name="struts.ui.theme" value="simple"></constant>







你可能感兴趣的:(表单,标签,实例,Web应用,struts2.0)