对于一个MVC框架而言,重点是实现两部分:业务逻辑控制器部分和视图页面部分。Struts2作为一个优秀的MVC框架,也把重点放在了这两部分上。控制器主要由Action来提供支持,而视图则是由大量的标签来提供支持.
1.1 通用标签
1.1.1 数据标签
property
set
scope=“action”,action=request+actionContext
push
修改页面
param
blue
注1:它是子标签
注2:url/action
date
java.text.SimpleDateFormat/DecimalFormat
debug
url/param/a
action
1.1.2 控制标签
iterator/if/elseif/else
写一个简单的struts标签提交方式
实体类Dept
package com.zking.entity;
public class Dept {
private Integer deptId;
private String deptName;
public Dept() {
super();
}
public Dept(Integer deptId, String deptName) {
super();
this.deptId = deptId;
this.deptName = 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;
}
@Override
public String toString() {
return "Dept [deptId=" + deptId + ", deptName=" + deptName + "]";
}
}
DAO方法
package com.zking.dao;
import java.util.ArrayList;
import java.util.List;
import com.zking.entity.Dept;
public class DeptDAO {
public List<Dept> list(){
List<Dept> deptList=new ArrayList<Dept>();
for (int i = 1; i <10; i++) {
deptList.add(new Dept(i,"超市"+i+"部"));
}
return deptList;
}
}
Action
package com.zking.action;
import java.util.List;
import com.zking.dao.DeptDAO;
import com.zking.entity.Dept;
public class DeptAction {
private DeptDAO deptDao=new DeptDAO();
private List<Dept> listDept;
public String listDept() {
listDept=deptDao.list();
return null;
}
public List<Dept> getListDept() {
return listDept;
}
}
jsp页面
<%@ page language=“java” contentType=“text/html; charset=UTF-8”
pageEncoding=“UTF-8”%>
<%@ taglib prefix=“s” uri="/struts-tags"%>
add
<s:select list="#daction.listDept" label="部门" name="deptId" listKey="deptId" listValue="deptName"
headerKey="" headerValue="---请选择---"></s:select>
<s:checkbox label="活动" name="married"></s:checkbox>
<s:checkboxlist list="#daction.listDept" label="部门列表" name="deptIds" listKey="deptId" listValue="deptName"></s:checkboxlist>
<s:submit value=“保存”/>
</s:form>
<struts>
<package name="sys" namespace="/sys" extends="base">
<global-allowed-methods>regex:.*</global-allowed-methods>
<action name="helloAction_*" class="com.zking.action.HelloAction" method="{1}">
<result name="c" type="redirect">/tag1.jsp</result>
</action>
<action name="vsAction" class="com.zking.action.VsAction">
<result name="b">/b.jsp</result>
</action>
<action name="employeeAction_*" class="com.zking.action.EmployeeAction" method="{1}">
</action>
<action name="deptAction" class="com.zking.action.DeptAction" method="listDept">
</action>
</package>
</struts>
核心配置文件(struts-base.xml)
<?xml version="1.0" encoding="UTF-8"?>
<struts>
<constant name="struts.devMode" value="true" />
<constant name="struts.configuration.xml.reload" value="true" />
<package name="base" extends="struts-default" abstract="true">
<global-allowed-methods>regex:.*</global-allowed-methods>
</package>
</struts>
配置web.xml
<?xml version="1.0" encoding="UTF-8"?>
struts2 index.html index.htm index.jsp default.html default.htm default.jsp
struts2 org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter struts2 /*