How to display a dijit.Dialog as a popup up - java

I am trying to display a dijit.Dialog as a popup window. This is a new feature addition to an existing project and as I have never woked wth Dojo;s before I am struggling a bit.
I have created the pop up window but it keeps displaying as a seperate page than a popup, what am I doing wrong? Please help!
THe page where the button to show the popup
<input class="style_off pink_button ft_white" onclick='submitOnclick()' id="show_popup" type="submit" name="showPopup" value="Generate PopUp"/>
<span id="validationPopUp" style="display:none;"><c:out value=""/></span>
The script page:
function submitOnclick() {
dojo.xhrPost({
url: "${get_valpopup_url}",
form: dojo.byId("searchResults"),
load: function(data){
//Destroy widget to avoid reallocating it during parse.
if(valDialog) {
valDialog.destroy();
}
dojo.place(data, "validationPopUp", "only");
dojo.parser.parse("validationPopUp");
valDialog = dijit.byId('inputPopUpId');
valDialog.show();
},
error: function(e) {
console.log("Ajax call failed", e);
}
});
return false;
}
the pop up window
<?xml version="1.0" encoding="UTF-8"?>
<div id="inputPopUpId" dojotype="dijit.Dialog" title="${popup_title}"
height="500px;" xmlns:f="http://www.twister.total.com/taglib"
xmlns:fn="http://java.sun.com/jsp/jstl/functions"
xmlns:fmt="http://java.sun.com/jsp/jstl/fmt"
xmlns:tiles="http://tiles.apache.org/tags-tiles"
xmlns:c="http://java.sun.com/jsp/jstl/core"
xmlns:util="urn:jsptagdir:/WEB-INF/tags/util"
xmlns:form="http://www.springframework.org/tags/form"
xmlns:jsp="http://java.sun.com/JSP/Page"
xmlns:s="http://www.twister.total.com/taglib/sessionAttr"
xmlns:spring="http://www.springframework.org/tags" version="2.0">
<jsp:directive.page contentType="text/html;charset=UTF-8" />
<jsp:output omit-xml-declaration="yes" />
<spring:message code="random_extract_popup_title" var="popup_title" htmlEscape="false" />
<p>
<spring:message code="random_extract_popup_message" />
</p>
<form:form method="post" modelAttribute="inputPopup" action="reports" enctype="application/x-www-form-urlencoded">
<s:insertSessionConversationId attributeName="searchResults"/>
<s:insertSessionConversationId attributeName="searchDto"/>
<div>
<label for="noOfRecords">
<spring:message code="random_extract_popup_form_label" />
</label>
<input id="noOfRecords" type='text' name='noOfRecords' style="width:150px" />
<spring:message code="random_extract_popup_form_message" var="name_msg" htmlEscape="false" />
<script type="text/javascript">
<c:set var="sec_name_msg">
<spring:escapeBody javaScriptEscape="true">${name_msg}</spring:escapeBody>
</c:set>
Spring.addDecoration(new Spring.ElementDecoration({elementId : "noOfRecords", widgetType : "dijit.form.NumberTextBox", widgetAttrs : {promptMessage: "${sec_name_msg}", required : true, constraints: {pattern: "0, ####"}}}));
</script>
</div>
<br />
<br />
<div class="submit">
<spring:message code="button_extract_random_report" var="extract_random_report" htmlEscape="false" />
<script type="text/javascript">Spring.addDecoration(new Spring.ValidateAllDecoration({elementId:'extract_random_report', event:'onclick'}));</script>
<input class="startLoading style_off pink_button ft_white" id="extract_random_report" type="submit" name="extractrandom" value="${fn:escapeXml(extract_random_report)}"/>
</div>
</form:form>
</div>
The views.xml
<definition extends="empty" name="a/s/validationPopUpId">
<put-attribute name="body" value="validation-dialog.jspx"/>
</definition>
The code in the controller
#RequestMapping(value = "sox/search/reports", params="showPopup", method = RequestMethod.POST)
public String generatePopup(#ModelAttribute(TwisterConstants.SEARCH_RESULTS) AuditSoxListResultsDto searchResults, #ModelAttribute(TwisterConstants.SEARCH_DTO) AuditSoxSearchRequest searchDto,
Model uiModel, Locale selectedLocale, HttpServletRequest request, HttpServletResponse response){
uiModel.addAttribute(TwisterConstants.SEARCH_RESULTS, TwisterConstants.SEARCH_DTO);
addDateTimeFormatPatterns(uiModel,"S-");
return "a/s/validationPopUpId";
}

Related

Using ajax methods .get() and .post() in JQuery to make add to cart

Im working in an assignment and I want to make a "add to cart button" I tried the post and get methods but it does nothing, the code consist of 4 files explore.html, explore.js, cart.html and cart.js and the users should be allowed to click on a link while exploring products and it will appear in the cart.html
explore.html
<html>
<head>
<title>Digital Waves::Explore</title>
<link rel="stylesheet" href="../css/explore.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="../js/explore.js"></script>
</head>
<body>
<div class="form-group">
<form action="/html/cart" method="POST">
<input name="quantity" type="hidden" value="1" />
<input name="product_id" type="hidden" value="91801160" />
<input name="product_name" type="hidden" value="Red Hat" />
<button class="add-button" type="submit">Add to Cart!</button>
</form>
</div>
</body>
</html>
explore.js
$(document).ready(function(){
$(".add-button").click(function(){
//alert("explore is clicked");
var productId = $("#productId").val();
var productName = $("#productName").val();
var productQuantity = $("#productQuantity").val();
var data = {
'product_id': productId,
'product_name': productName,
'quantity': productQuantity
};
$.post("html/cart", data, showDone);
var showDone = function() {
alert("I sent the data");
};
});
});
cart.html
<!DOCTYPE HTML>
<html>
<head>
<title>Digital Waves::Cart</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
<script src="../js/cart.js"></script>
</head>
<body>
<button id="buttonx">I get info</button>
<p id="x"></p>
</body>
</html>
cart.js
$(document).ready(function(){
$("#buttonx").click(function(){
//alert("cart is clicked");
$.get("../html/explore.html",function(data , status){
$("x").html(data);
alert("done");
})
});
});
As per your HTML code, you have not assigned id attribute to your input fields. You need to use id field as below:
<div class="form-group">
<form action="/html/cart" method="POST">
<input id="quantity" name="quantity" type="hidden" value="1" />
<input id="product_id" name="product_id" type="hidden" value="91801160" />
<input id="product_name" name="product_name" type="hidden" value="Red Hat" />
<button class="add-button" type="submit">Add to Cart!</button>
</form>
</div>
After this, your jQuery code should work fine. And if you have multiple add cart form, then you should use class instead of id. Hope it helps you.

How to display selected item from "select" to "text field" in Thymeleaf?

I'm develop simple Spring boot app using Thymeleaf and want to display selected "user" from Combobox1 at the "userName" text field. How i can made it?
<!DOCTYPE HTML>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="utf-8">
<title>Добавить цитату</title>
</head>
<body>
<form th:action="#{/addQuote}"
th:object="${personForm}" method="POST">
<p><b>Имя</b><br>
<div>
<input name = "userName" type="text" size="38" th:field="*{name}" >
<a style="position:fixed;left:0px;top:0px;margin: 0;border-width:0;z-
index:255"></a>
</div>
<textarea name="comment" cols="40" rows="6" th:field="*{text}" >
</textarea>
<p><input type="reset" value="Отменить">
<input type="submit" value="+"></p>
<select name="Combobox1" size="1" id="Combobox1"
style="position:absolute;left:17%;top:4%;width:199px;height:20px;z-
index:3;" >
<option th:each="user : ${users}"
th:value="${user}"
th:utext="${user}"/>
</select>
</form>
<div th:if="${errorMessage}" th:utext="${errorMessage}"
style="color:red;font-style:italic;">
</div>
</body>
</html>
Option 1:
Add Id in your userName input tag
<input id="userName" name = "userName" type="text" size="38" th:field="*{name}" />
Add script
<script>
function check() {
document.getElementById("userName").value=document.getElementById("Combobox1").value;
}
</script>
Call script function
<select id="Combobox1" onChange="check();">
Option2:
Another option with Jquery
$(function() {
$("#Combobox1").on("change",function() {
alert();
$("#userName").val($(this).val());
});
});

Ajax not working when I am trying to save data

When I press the save button ajax does not work.The page reloads.Here is my indx page and my Controller function.
URL is --- http://localhost:8082/spring-test/index
Controller
#RequestMapping(value = "/saveUser", method = RequestMethod.POST)
public String saveUser(#Valid #ModelAttribute("User") User user, BindingResult bindingResult,
ModelMap model, HttpServletRequest request, RedirectAttributes redirectAttrs) {
System.out.println("user: " + user.getUserId());
String type = request.getParameter("update") != null ? request.getParameter("update") : "";
String delete = request.getParameter("delete") != null ? request.getParameter("delete") : "";
if (type.equalsIgnoreCase("update") || delete.equalsIgnoreCase("delete")) {
User userDb = userService.getUserById(user.getUserId());
user.setUserId(userDb.getUserId());
}
if (bindingResult.hasErrors()) {
System.out.println("=====================error======================" + bindingResult.getFieldErrors());
return "index";
}
if (delete.equalsIgnoreCase("delete")) {
userService.deleteUser(user.getUserId());
} else {
userService.saveUser(user);
}
//model.clear();
model.clear();
return "redirect:/index";
}
index.html
<html xmlns:th="http://www.thymeleaf.org" xmlns:tiles="http://www.thymeleaf.org" class="no-js">
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://cdn.datatables.net/1.10.13/css/jquery.dataTables.min.css" />
<script src="https://cdn.datatables.net/1.10.13/js/jquery.dataTables.min.js"></script>
<script>
$(document).ready(function() {
$('#myTable').DataTable();
$('#btnSave').click(function(e) {
var dataUser = $("#name").val();
var dataPass = $("#pwd").val();
var userDdata = {
"name": dateUser,
"pass": dataPass
}
$.ajax({
type: "POST",
url: "/spring-test/saveUser",
data: userData,
success: function(response) {
alert(1)
},
error: function(e) {
alert('Error: ' + e);
}
});
}
});
</script>
</head>
<body>
<div class="container col-sm-12 col-md-12" style="height:20px;background:#85a3e0;">
</div>
<div class="container col-sm-12 col-md-12">
<h3 class="text-center">Super Admin</h3>
<div class="col-md-4 col-sm-4" style="margin-left: 6%;">
<form class="form-horizontal" role="form" th:action="#{/saveUser}" th:object="${user}" method="post">
<div class="form-group">
<label class="control-label col-sm-3" for="pwd">Username:</label>
<div class="col-sm-9">
<input type="text" class="form-control" id="name" placeholder="Enter Name" th:field="*{userName}" />
<input type="hidden" th:field="*{userId}" />
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-3" for="pwd">Password:</label>
<div class="col-sm-9">
<input type="text" class="form-control" id="pwd" placeholder="Enter password" th:field="*{password}" />
</div>
</div>
<div class="form-group">
<div class=" col-sm-12 text-right">
<button type="submit" class="btn btn-default" name="cancel" value="cancel">Cancel</button>
<button id="btnSave" type="submit" class="btn btn-primary" name="save" value="save">Save</button>
<button type="submit" class="btn btn-primary" name="update" value="update">Update</button>
<button type="submit" class="btn btn-primary" name="delete" value="delete">Delete</button>
</div>
</div>
</form>
</div>
</div>
</body>
</html>
Whats the problem here.
After adding the ajax function my dataTable is not appearing also.
Since you already have an action( th:action="#{/saveUser}") for the form submit, it will send the data directly, not via AJAX. In order to make it worked, you need to prevent this default behavior as like below.
$('#btnSave').click(function(e) {
e.preventDefault()
//Then do your stuff
});
It is not working because you have submit button and it directly submits the form.
What you can do it simply remove type="submit" from button and then try.
Vinod
Just try the below
<html xmlns:th="http://www.thymeleaf.org" xmlns:tiles="http://www.thymeleaf.org" class="no-js">
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://cdn.datatables.net/1.10.13/css/jquery.dataTables.min.css" />
<script src="https://cdn.datatables.net/1.10.13/js/jquery.dataTables.min.js"></script>
<script>
$(document).ready(function() {
$('#myTable').DataTable();
$('#btnSave').click(function(e) {
var userData = $("#form1").serializeArray();
$.ajax({
type: "POST",
url: "/spring-test/saveUser",
data: userData,
success: function(response) {
alert(1)
},
error: function(e) {
alert('Error: ' + e);
}
});
}
});
</script>
</head>
<body>
<div class="container col-sm-12 col-md-12" style="height:20px;background:#85a3e0;">
</div>
<div class="container col-sm-12 col-md-12">
<h3 class="text-center">Super Admin</h3>
<div class="col-md-4 col-sm-4" style="margin-left: 6%;">
<form class="form-horizontal" role="form" id="form1" th:action="#{/saveUser}" th:object="${user}" method="post">
<div class="form-group">
<label class="control-label col-sm-3" for="pwd">Username:</label>
<div class="col-sm-9">
<input type="text" class="form-control" id="name" placeholder="Enter Name" th:field="*{userName}" />
<input type="hidden" th:field="*{userId}" />
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-3" for="pwd">Password:</label>
<div class="col-sm-9">
<input type="text" class="form-control" id="pwd" placeholder="Enter password" th:field="*{password}" />
</div>
</div>
</form>
<div class="form-group">
<div class=" col-sm-12 text-right">
<button type="submit" class="btn btn-default" name="cancel" value="cancel">Cancel</button>
<button id="btnSave" type="button" class="btn btn-primary" name="save" value="save">Save</button>
<button type="submit" class="btn btn-primary" name="update" value="update">Update</button>
<button type="submit" class="btn btn-primary" name="delete" value="delete">Delete</button>
</div>
</div>
</div>
</div>
</body>
</html>

Strange message with servlets

I am using eclipse with tomcat here is my servlet ,the problem is when I click the button called GetStared it redirects me to empty html page with the text "surved at :myproject123" . myproject123 is the name of my project in the eclipse IDE.
#WebServlet("/home")
public class homeServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String htmlFile = "loginPage.html";
RequestDispatcher view = request.getRequestDispatcher(htmlFile);
view.forward(request, response);
}
}
Here is my html file : called index.html
<div class="background-wrap">
<video id="video-bg-elem"
preload="auto"
autoplay="autoplay"
loop="loop" muted="muted">
<source src="video.mp4" type="video/mp4">
Video not supported
</video>
</div>
<div class="content">
<div class="info">
<div class="vertical_align">
<h2>Spotify</h2>
<output>Music for each moment.</output>
</div>
<div class="vertical_align2">
<form action="home" method="GET">
<button type="submit"
value="Get Started"
class="animated_button">
GET STARTED
</button>
</form>
</div>
</div>
</div>
Here is loginPage.html
<div class="myCirle">
<img src="logo10.png" />
<div class="under_logo">
<p>Spotify.</p>
</div>
</div>
<form class="form-signin" action="login" method="POST">
<div class="form-input" >
<input type="text" name="Username" placeholder = "Enter username" />
</div >
<div class="form-input" >
<input type="password" name="Password" placeholder="Enter password" />
</div >
<div class="form-input">
<input type="submit" name="submit" value="Sign In" class="btn-login" /><br />
<input type="submit" name="submit" value="Sign In With Facebook" class="btn-loginFB" />
</div>
<div class="form-input">
Forgot password?
</div>
<div class="form-input">
<input type="submit" name="submit" value="Not a spotifyier yet?" class="btn-reg" />
</div>
</form>
</div>
</body >
problem seems to be with forward the request....you can try this.. request.getRequestDispatcher("/"+htmlFile); or request.getRequestDispatcher("//"+htmlFile);
it is so because getRequestDispatcher () accepts link to the resources instead of direct file name.So if your file in inside your webapp like MyApp/myFile,then
request.getRequestDispatcher("/myFile");
if it is inside WEB-INF,then..
request.getRequestDispatcher("/WEB-INF/myFile");
and so on...

Struts 1 ActionForm - retrieving a collection from pure HTML

Hi all I have (just like the rest) inherited some struts 1 code. I have had need to add a few more pages to this project.
What I cannot figure out is how to map several distinct but similarly natured input elements to my ActionForm.
Let me elaborate. I create a new <Input> element dynamically as the user inputs more and more items (I use the YUI autocomplete form element and for each entered input I add it as an input element to my form and draw a new YUI autocomplete - complex sounding, I know)
So... My form looks a bit like (... after some prettifying and some such...):
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>My Cool App - Test Case Builder</title>
<link rel="stylesheet" type="text/css" href="../script/yui/fonts/fonts-min.css" />
<link rel="stylesheet" type="text/css" href="../skins/myCoolApp/button/button.css" />
<link rel="stylesheet" type="text/css" href="../script/yui/autocomplete/assets/skins/sam/autocomplete.css" />
<link rel="stylesheet" type="text/css" media="screen" href="../skins/myCoolApp/testcase.css" />
<!-- YUI JAVA SCRIPTS -->
<script type="text/javascript" src="../script/yui/yahoo-dom-event/yahoo-dom-event.js"></script>
<script type="text/javascript" src="../script/yui/element/element-min.js"></script>
<script type="text/javascript" src="../script/yui/button/button-min.js"></script>
<script type="text/javascript" src="../script/yui/datasource/datasource-min.js"></script>
<script type="text/javascript" src="../script/yui/autocomplete/autocomplete-min.js"></script>
<!-- APP JAVA SCRIPTS -->
<script type="text/javascript" src="../script/myCoolApp/myCoolApp.js" ></script>
<script type="text/javascript" src="../script/myCoolApp/stack.js" ></script>
<script type="text/javascript" src="../script/myCoolApp/testcase/testcase.js"></script>
<script type="text/javascript" src="../script/myCoolApp/testcase/default-data.js" ></script>
<script type="text/javascript" src="../script/myCoolApp/testcase/data-structs.js" ></script>
<script type="text/javascript" src="../script/myCoolApp/testcase/ui-elements.js" ></script>
</head>
<body class="cf010">
<div id="wrap">
<div id="header">
<div id="main-header">
COOL APP
</div>
</div>
<div id="main-body">
<div id="content">
<div class="col main">
<div id="main">
<form method="post" id="testcaseForm" class="typea" action="">
<fieldset>
<legend>Test Case Builder</legend>
<div id="tk1" class="tabcontrol">
<ul class="tabs">
<li class="first active">
<a href="#">
<span>General</span>
</a>
</li>
<li class="last">
<a href="#">
<span>Parameters</span>
</a>
</li>
</ul>
<div id="tab0" class="tc-panel">
<dl class="cls9">
<dt>
<label for="scenario">Choose Scenario:</label>
</dt>
<dd>
<input type="text" id="scenario" name="scenario" class="text" />
<span id="scenarioToggle"></span>
<div class="auto-complete" id="scenarioContainer"></div>
</dd>
<dt>
<label for="ruleID">Choose Rule ID:</label>
</dt>
<dd>
<input type="text" id="ruleID" name="ruleID" class="text" />
<span id="ruleIDToggle"></span>
<div class="auto-complete" id="ruleIDContainer"></div>
</dd>
<dt>
<label for="Test Case Name" accesskey="t"><span class="accesskey">T</span>est Case Name:</label>
</dt>
<dd>
<input type="text" id="testCaseName" name="testCaseName" class="text" />
</dd>
</dl>
</div>
<div id="tab1" class="tc-panel hidden">
<div class="toolbar" id="action-bar">
<ul>
<li class="first">
<a title="select all" href="#" id="btmSelectAll" class="button">
<span>select all</span>
</a>
</li>
<li>
<a title="remove row" href="#" id="btmRemove" class="button">
<span>remove row</span>
</a>
</li>
<li>
<a title="undo last" href="#" id="btmRollBack" class="button disabled">
<span>undo last</span>
</a>
</li>
<li class="last">
<a title="accept row" href="#" id="btmAccept" class="button disabled">
<span>accept row</span>
</a>
</li>
</ul>
</div>
<div id="param.list" class="gridclip">
<table id='param.list.tbl' class='grid modela' >
<caption>Test Case Summary</caption>
<col/><col/><col/>
<thead>
<tr>
<th class='hl center first'>
<input class='grid-select-all' type='checkbox' />
<th>
<th scope='col'>Row</th>
<th scope='col'>Parameter</th>
<th scope='col' class='last'>Value</th>
</tr>
</thead>
<tfoot>
<tr>
<th scope='row'>Total</th>
<td colspan='3'>2 parameters as Test Case input</td>
</tr>
</tfoot>
<tbody id='param.list.tbl.body'>
<tr class='odd'>
<td class='rowcheck center first'>
<input value='param1###value1' id='cb1' name='SelectedRows' class='grid-select-row' type='checkbox'/>
</td>
<td class='id'>1</td>
<td>param1</td>
<td class='last'>value1</td>
</tr>
<tr class='even'>
<td class='rowcheck center first'>
<input value='param2###value2' id='cb2' name='SelectedRows' class='grid-select-row' type='checkbox'/>
</td>
<td class='id'>2</td>
<td>param2</td>
<td class='last'>value2</td>
</tr>
<tr class='odd'>
<td class='rowcheck center first' />
<td class='id'><em>new</em></td>
<td>
<dl class='clsTable'>
<dt>
<input type='text' id='param' name='param' class='text paramInput' />
</dt>
<dd>
<span id='paramToggle' />
</dd>
<div class='auto-complete' id='paramContainer' />
</dl>
</td>
<td class='last'>
<dl class='clsTable'>
<dt>
<input type='text' id='value' name='value' class='text valueInput' />
</dt>
</dl>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div> <!-- tabcontrol -->
</fieldset>
<div class="submit-box">
<input type="submit" name="formRun" id="formRun" class="form-save"
value="Execute" accesskey="x" title="Run: Press Alt + [Shift] + x" />
<input type="submit" name="formSave" id="formSave"
value="Save" accesskey="s" title="Save: Press Alt + [Shift] + s" />
<input type="submit" name="formLoad" id="formLoad"
value="Load" accesskey="l" title="Load: Press Alt + [Shift] + l" />
<input type="submit" name="formCancel" id="formCancel" class="form-cancel"
value="Cancel" accesskey="c" title="Cancel: Press Alt + [Shift] + c" />
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
As you can see the following is pretty much a duplicate:
<tr class='odd'>
<td class='rowcheck center first'>
<input value='param1###value1' id='cb1' name='SelectedRows' class='grid-select-row' type='checkbox'/>
</td>
<td class='id'>1</td>
<td>param1</td>
<td class='last'>value1</td>
</tr>
<tr class='even'>
<td class='rowcheck center first'>
<input value='param2###value2' id='cb2' name='SelectedRows' class='grid-select-row' type='checkbox'/>
</td>
<td class='id'>2</td>
<td>param2</td>
<td class='last'>value2</td>
</tr>
The relevant part of my stuts-config.xml file is:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">
<struts-config>
<data-sources />
<form-beans>
<form-bean name="TestCaseForm" type="com.blahblah.mycoolapp.forms.TestCaseForm" />
</form-beans>
<action-mappings>
<action path="/pages/SaveTestCase" name="TestCaseForm"
type="org.springframework.web.struts.DelegatingActionProxy" scope="request">
</action>
</action-mappings>
<message-resources parameter="MessageResources" />
</struts-config>
I also use spring 2.56 (The relevant part being):
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean name="/pages/SaveTestCase" class="com.blahblah.mycoolapp.actions.TestCaseBuilderSaveAction" />
</beans>
My Java ActionForm class (from what I had learned off the net) is:
package com.blahblah.mycoolapp.forms;
import java.util.ArrayList;
import java.util.List;
import org.apache.struts.action.ActionForm;
public class TestCaseForm extends ActionForm {
private static final long serialVersionUID = 2352146257739099766L;
private String scenario;
private String ruleID;
private String testCaseName;
private List<String> SelectedRows = new ArrayList<String>() ;
public String getScenario() {
return scenario;
}
public void setScenario(String scenario) {
this.scenario = scenario;
}
public String getRuleID() {
return ruleID;
}
public void setRuleID(String ruleID) {
this.ruleID = ruleID;
}
public String getTestCaseName() {
return testCaseName;
}
public void setTestCaseName(String testCaseName) {
this.testCaseName = testCaseName;
}
public List<String> getSelectedRows() {
return SelectedRows;
}
public void setSelectedRows(int index, String value) {
this.SelectedRows.add(value);
}
}
The question is why do I get an empty SelectedRows in my TestCaseBuilderSave Action?
Thanks all who have the patience to read such a long question...
and (hopefully) thanks to all you potential saviors :)
EDIT 1:
Due to #ahiru's request I post my action class which - for the time being is just a stripped down. almost do nothing, class:
public class TestCaseBuilderSaveAction extends Action {
#Override
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
HttpServletResponse response) throws Exception {
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition","attachment;filename=superfish.xml");
try
{
ServletOutputStream out = response.getOutputStream();
String contentStr = "<Yaneeve>Has been here...</Yaneeve>";
byte[] bytes = contentStr.getBytes();
out.write(bytes, 0, bytes.length);
out.flush();
out.close();
}catch(Exception e){
e.printStackTrace();
}
return null;
}
}
I'm not a struts master or anything but can struts handle building a List out of the inputs? Would it rather be better to use an array of strings? Also have you tried adding a method in your form?
public void setSelectedRows(List<String> list) { SelectedRows = list;}
I think there is a way to define a indexed input in the struts config.
--update
Could you possibly post your action class where you are trying to get these values?
I know you said that the method above does not work but here is what works for me in a similar situation (I think). Sorry I can't find the exact code but here is what I remember:
-ActionForm
private String[] strings;
public void setStrings(String[] strings) { this.strings = strings;}
public String[] getStrings() { return strings; }
jsp/html/whatever
<input type="text" value="x" name="strings[]">
<input type="text" value="y" name="strings[]">
<input type="text" value="z" name="strings[]">
I'm also curious if spring and struts play together nicely (i've done no research on this)
It seems as if the problem had been that the checkboxes had not been 'checked'.
Only 'checked' checkboxes get their value transferred to the server...
One thing I wish to emphasize is that I had converted my code to be like what #ahiru suggested:
private String[] strings;
public void setStrings(String[] strings) { this.strings = strings;}
public String[] getStrings() { return strings; }

Categories