action接收jsp页面的数据
2012-12-01 11:30
316 查看
一、action可以直接接收value值的:input控件type=text/hidden、select的选中项、textarea
二、action不可直接接收value值得:input控件type=radio、checkbox选中项
1、jsp页面的String、int类型数据可分别用String、Integer类型接收
2、jsp页面的date类型数据在action要以String接收,再转换为Date类型
***************************************************
以下代码是我自己看的,你们就略过吧
test.jsp
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<base href="<%=basePath%>">
<title>test</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">
<script type="text/javascript" src="style/jquery-1.7.2.min.js"></script>
<link rel="stylesheet" type="text/css" href="style/common.css"/>
<script type="text/javascript">
var mark = true;
$(document).ready(function(){
$("input").change(function(){
mark = false;
});
$("select").change(function(){
mark = false;
});
$("textarea").change(function(){
mark = false;
});
$("#softId").change(getTypes);
/* 绑定beforeunload事件 */
$(window).bind('beforeunload',function(){
if(!mark){
return '您输入的内容尚未保存,确定离开此页面吗?';
}
});
});
function getTypes(){
var obj = eval("document.all.softId");
var softId = obj.options[obj.selectedIndex].value;
var div = document.getElementById("typee");
if(softId != 0){
$.ajax({
async : false,
cache: true,
type: 'POST',
dataType : "json",
url: "gettypes_bysoftid", //请求的action路径
data: "softId=" + softId,
success:function(data){ //请求成功后处理函数。
setJSON = eval("("+data.result+")"); //把后台封装好的简单Json格式赋给对象
},
error:function(data){
alert("获取信息异常");
}
});
div.innerHTML = "";
for(var i=0;i<setJSON.length;i++){
var html = '<span><input type="checkbox" name="typee" onblur="checkttype()" checked="checked" value="'+setJSON[i].id+'">'
+setJSON[i].name+' </span>';
div.innerHTML += html;
}
if(div.innerHTML != ""){
document.getElementById("ttypeError").style.display = 'none';
}
}else{
div.innerHTML = "";
}
}
// var flag = false;
//检查计划名称非空
function checksname(){
var snameValue = document.getElementById("sname").value;
if(snameValue.length == 0 || snameValue == null){
document.getElementById("snameError").style.display = 'block';
return false;
}else{
document.getElementById("snameError").style.display = 'none';
return true;
}
}
//给 升级计划类型值 隐藏域赋值
function setstype(){
//取得计划类型的下拉框选项
var scheduletypes = document.getElementsByName("sType");
var scheduletypeval = null;
for(var i=0;i<scheduletypes.length;i++){
if(scheduletypes.item(i).checked){
scheduletypeval=scheduletypes.item(i).getAttribute("value");
break;
}
}
if(scheduletypeval=="force"){
document.getElementById("scheduleType").setAttribute("value",1);
}else if(scheduletypeval=="optional"){
document.getElementById("scheduleType").setAttribute("value",2);
}
}
//检查软件包非空
function checksoft(){
var softvalue = document.getElementById("softId").value;
if(softvalue == 0 || softvalue == null){
document.getElementById("softError").style.display = 'block';
return false;
}else{
document.getElementById("softError").style.display = 'none';
return true;
}
}
//检查待升级掌机类型非空 以及给掌机类型隐藏域赋值
function checkttype(){
var ttypeObj = document.getElementsByName("typee");
var typevalue = "";
for(var i=0;i<ttypeObj.length;i++){
if(ttypeObj[i].checked == true)
{
typevalue += ttypeObj[i].value+",";
}
}
document.getElementById("ttypevalstr").setAttribute("value",typevalue); //给隐藏域赋值
if(typevalue == "" || typevalue == null){
document.getElementById("ttypeError").style.display = 'block';
return false;
}else{
document.getElementById("ttypeError").style.display = 'none';
return true;
}
}
//升级单位设值 根据升级范围 控制级联是否显示及设值
function checkstrategy(){
var strategy = document.getElementsByName("strategy");
for(var i=0;i<strategy.length;i++){
if(strategy.item(i).checked){
strategyval=strategy.item(i).getAttribute("value");
break;
}
}
if(strategyval=="all"){
document.getElementById("cascadearea").style.display = 'none';
}else if(strategyval=="part"){
document.getElementById("cascadearea").style.display = 'block';
}
}
//检查生效日期非空
function checkbegindate(){
var begindatevalue = document.getElementById("scheduleBeginDate").value;
if(begindatevalue == null || begindatevalue == ""){
document.getElementById("dateError").style.display = 'block';
return false;
}else{
document.getElementById("dateError").style.display = 'none';
return true;
}
}
function checkform(){
checksname();
checksoft();
checkttype();
checkstrategy();
checkbegindate();
setstype();
if(checksname() && checksoft() && checkttype() && checkbegindate()){
mark = true;
onsubmitForm();
}
else{
alert("请按要求填写表单!");
}
}
//提交表单
function onsubmitForm(){
document.form1.submit();
}
</script>
</head>
<body class="contentBody">
<form action="equipment/addUpdateSchedule" method="post" name="form1">
计划名称:
<input type="text" id="sname" name="scheduleTitle" onblur="checksname()"/>
<span id="snameError" style="display:none;color:red;"><span>计划名称不能为空</span></span>
...
计划类型:
<input type="radio" value="force" id="scheduleType1" name="sType" checked="checked" disabled="disabled">
<font color="gray">强制升级</font>
<input type="radio" value="optional" id="scheduleType2" name="sType" disabled="disabled">
<font color="gray">可选升级</font>
<input type="hidden" id="scheduleType" name="scheduleType">
...
升级软件包:
<select id="softId" id="scheduleSoft" name="scheduleSoft" onchange="checksoft()">
<option value="0">请选择</option>
<s:iterator value="softwares" var="software">
<option value="${software.id }">${software.versionName }</option>
</s:iterator>
</select>
<span id="softError" style="display:none;color:red;"><span>请选择一个软件升级包</span></span>
...
被升级的掌机类型:
<div id="typee"></div>
<input type="hidden" id="ttypevalstr" name="scheduleUpdateTerminalType">
<span id="ttypeError" style="display:none;color:red;"><span>请至少选择一种软件类型</span></span>
...
升级范围:
<input type="radio" value="all" name="strategy" checked="checked" disabled="disabled">
<font color="gray">全部单位</font>
<input type="radio" value="part" name="strategy" disabled="disabled">
<font color="gray">指定单位</font>
<input type="hidden" id="scheduleUnit" name="scheduleUnit" value="0">
...
是否级联:
<input type="radio" value="1" id="cascade1" name="sCascade" checked="checked">级联
<input type="radio" value="2" id="cascade2" name="sCascade">不级联
<input type="hidden" id="scheduleCascade" name="scheduleCascade" value="0">
...
生效日期:
<input id="nowtime" type="hidden" readonly="readonly" class="Wdate"
onfocus="var scheduleBeginDate=$dp.$('scheduleBeginDate');WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss',onpicked:function(){scheduleBeginDate.focus();},maxDate:'#F{$dp.$D(\'scheduleBeginDate\')}'})">
<input id="scheduleBeginDate" name="scheduleBeginDate" readonly="readonly" class="Wdate"
onfocus="WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss',minDate:'#F{$dp.$D(\'nowtime\')}'})"
onblur="checkbegindate()">
<span id="dateError" style="display:none;color:red;"><span>请选择生效日期</span></span>
...
备注:
<input type="hidden" name="scheduleStatus" value="1"> <!-- 计划状态影藏域 -->
<textarea onpropertychange="if(value.length>100) value=value.substr(0,100)" rows="5" cols="80" name="scheduleMemo"></textarea>
...
<input type="button" style="height: 24px; width: 62px;" onclick="checkform()" value="保存">
<input type="button" onclick="returnOper('equipment/getAllSchedule')" style="height: 24px; width: 62px;" value="返回">
</form>
</body>
</html>
struts.xml
...
<package name="equipment" namespace="/equipment" extends="default">
<action name="getAllSchedule" class="com.keyman.fss.action.EquipmentAction" method="getAllSchedule">
<result name="success">/equipment/updateSchedule.jsp</result>
</action>
<action name="addUpdateSchedule" class="com.keyman.fss.action.EquipmentAction" method="addUpdateSchedule">
<result name="success" type="chain">
<param name="actionName">getAllSchedule</param>
</result>
</action>
</package>
...
.action
private String scheduleTitle;
private Integer scheduleType;
private Integer scheduleSoft;
private Integer scheduleUnit;
private Integer scheduleCascade;
private String scheduleBeginDate;
private Integer scheduleStatus;
private String scheduleMemo;
private String scheduleUpdateTerminalType;
public String addUpdateSchedule(){
Integer sunit = null;
if(scheduleUnit != 0){
sunit = scheduleUnit;
}
Integer scascade = null;
if(scheduleCascade != 0){
scascade = scheduleCascade;
}
//生效日期
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date beginDate = null;
try {
beginDate = sdf.parse(scheduleBeginDate);
} catch (ParseException e) {
e.printStackTrace();
}
//选中升级的掌机类型集
String[] selectedtypes = scheduleUpdateTerminalType.substring(0, scheduleUpdateTerminalType.length()-1) //去除字符串末尾逗号
.split(","); //根据逗号分开成数组
Integer[] types = new Integer[selectedtypes.length];
for(int i = 0;i<selectedtypes.length;i++){
int index = i;
types[index] = Integer.parseInt(selectedtypes[i]);
}
try {
...
UpdateSchedule schedule = ConnectEjb.fssRemote.addUpdateScheduleAndItem(ConnectEjb.actor,
scheduleTitle, scheduleType, scheduleSoft, sunit, scascade, beginDate,
scheduleStatus, types, scheduleMemo);
System.out.println(schedule.getScheduleTitle());
} catch (NoMethodAuthorityException e) {
errorString = e.getMessage();
} catch (NoObjectAuthorityException e) {
errorString = e.getMessage();
} catch (NoAvailableActorException e) {
errorString = e.getMessage();
} catch (Exception e) {
errorString = "未知错误";
}
return SUCCESS;
}
相关文章推荐
- Action如何接收jsp页面form表单提交的数据?
- Action如何接收jsp页面form表单提交的数据?
- Struts2的Action向JSP传输List集合并且将数据显示在页面上
- jsp之同一个页面提交和接收数据
- 在jsp页面用request.setAttribute()在action用request.getAttribute()接收得到为null的问题的解决方法
- struts2后台Action接收jsp页面的值
- JSP接收数据为空--页面显示为0
- 请求Action的路径、参数设置及Struts2的Action向JSP传输List集合并且将数据显示在页面上
- 如何在JSP页面快速输出从Servlet接收的数据_看JSTL常用C标签的威力
- 如何打开jsp页面时经过action从数据库取得数据显示在页面上
- Jsp到Action后台的数据接收方法
- jsp中为什么后台传递过来的数据没有空格 而jsp页面上用EL表达式接收就会显示有空格
- Struts2 之 Action类与 jsp 页面的数据交互
- Struts2的Action向JSP传输List集合并且将数据显示在页面上
- struts:将Action处理后的数据传递给前台jsp页面
- jsp页面传中文数据到Action中出现乱码
- Struts2 后台action接收 jsp页面中checkbox中的值
- 在jsp页面通过jstl接收servlet中List对象数据
- Struts2框架中action无法获取到jsp页面传递的表单数据的解决办法
- 关于使用EL语句来接收servlet给jsp传递及jsp页面之间传递的数据