經過上一篇文章的學習,我們來做一個實例
要求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>
<%@ 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>
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>