Botdetect Captcha HTML <taglib> - html

I want to add Botdetect Captcha to my html file.But in the website of the Botdetect captcha, there is example only for jsp. In this jsp file, <taglib> is used like that:
<%#taglib prefix="botDetect" uri="https://captcha.com/java/jsp"%>
....
<botDetect:captcha id="basicExample" userInputID="captchaCode" />
<div class="validationDiv">
<input name="captchaCode" type="text" id="captchaCode" value="${basicExample.captchaCode}" />
<input type="submit" name="validateCaptchaButton" value="Validate" id="validateCaptchaButton" />
<span class="correct">${basicExample.captchaCorrect}</span>
<span class="incorrect">${basicExample.captchaIncorrect}</span>
</div>
Is there any alternative for <%#taglib> in HTML files. How can I solve this problem?

I'm facing this problem because I use Thymeleaf instead of JSP so I can't use the taglib but I still have "dynamic HTML". I'm answering assuming this is your case.
I saw a possible solution on the help pages here: https://captcha.com/doc/java/jsp-captcha.html at section 2:
<%
// Adding BotDetect Captcha to the page
Captcha captcha = Captcha.load(request, "exampleCaptcha");
captcha.setUserInputID("captchaCode");
String captchaHtml = captcha.getHtml();
out.write(captchaHtml);
%>
This is JSP code but can be easily adapted to Thymeleaf. This in the #Controller that opens the page:
Captcha captcha = Captcha.load(request, "exampleCaptcha");
captcha.setUserInputID("captchaCode");
String captchaHtml = captcha.getHtml();
model.addAttribute("captchaHtml", captchaHtml);
and the html is like
<th:block th:utext="${captchaHtml}"></th:block>
The code for captcha checking is the same as for JSP, but placed in the #Controller that handles the form:
// validate the Captcha to check we're not dealing with a bot
boolean isHuman = captcha.validate(request.getParameter("captchaCode"));
if (isHuman) {
// TODO: Captcha validation passed, perform protected action
} else {
// TODO: Captcha validation failed, show error message
}
To finish you also need to edit web.xml (if you have it) and import the java libs as by the official docs. I'm using Gradle and importing 'com.captcha:botdetect-servlet:4.0.beta3.7'
Warning: your server should be on HTTPS or you might get this error when using version 4.0.beta3.7:
Captcha.UserInputID is not set. Your implementation of BotDetect is
not completely secure yet

<%#taglib prefix="botDetect" uri="https://captcha.com/java/jsp"%>
....
<botDetect:captcha id="basicExample" userInputID="captchaCode" />
<div class="validationDiv">
<input name="captchaCode" type="text" id="captchaCode" value="${basicExample.captchaCode}" />
<input type="submit" name="validateCaptchaButton" value="Validate" id="validateCaptchaButton" />
<span class="correct">${basicExample.captchaCorrect}</span>
<span class="incorrect">${basicExample.captchaIncorrect}</span>
</div>
That is wrong, with HTML (HyperText Markup Language) you have to list the version of HTML in this case it is Doctype HTML so the following one should be correct...
<!DOCTYPE html>
<%#taglib prefix="botDetect" uri="https://captcha.com/java/jsp"%>
....
<botDetect:captcha id="basicExample" userInputID="captchaCode" />
<div class="validationDiv">
<input name="captchaCode" type="text" id="captchaCode" value="${basicExample.captchaCode}" />
<input type="submit" name="validateCaptchaButton" value="Validate" id="validateCaptchaButton" />
<span class="correct">${basicExample.captchaCorrect}</span>
<span class="incorrect">${basicExample.captchaIncorrect}</span>
</div>

Related

Ruby Sinatra textarea displays variable string instead of variable value

I'm trying to set up a webapp using Sinatra, and I've got it working currently where the user can enter a list of URLs into a textarea, click submit, and then some code is ran on the URLs and the user is redirected to a second page to view the result. I'd like to remove the redirection and display the results in another textarea on the same page(or even re-use the same text area) but I've so far been unsuccessful in this venture. Instead of displaying the result, the second textarea just displays the <%=res%> and the submitted URLs disappear.
ERB page
<h1>Put URLs Here</h1>
<form method="POST" action="">
<p><textarea id="urls" name="urls" rows="20" cols="60">
</textarea></p>
<input type="submit">
</form>
<br />
<br />
<h1>Results</h1>
<p><textarea id="results" name="results" rows="20" cols="60">
<%=res%>
</textarea></p>
app.rb
require 'sinatra'
get '/ip' do
erb :ip
end
post '/ip' do
res = Hash.new("")
u = params[:urls].lines("\n")
list = Ipcheck.new()
res = list.check(u)
erb :ip, :locals => {'res' => res}
end
What am I doing wrong? I've searched and have not been able to find a solution to this specific problem.
Use global variable instead of local.
<%= #res %>
and
#res = Hash.new("")
u = params[:urls].lines("\n")
list = Ipcheck.new()
#res = list.check(u)
erb :ip

How to fix the Angular syntax parser error within a Struts 2 <s:checkbox> tag?

I have encountered a strange error using AngularJS + Struts 2.
I have a Java object form with a boolean attribute named paid.
When I write:
<div class="col-sm-10 form-checkbox">
<s:checkbox name="xxxxx" ng-model="model"
ng-init="model = <s:property value = '%{form.paid}' />"
theme="simple" />
</div>
I get FireBug complaining about AngularJS syntax parser error which directs me to this page:
syntax error page
suggesting expression error.
And if I write:
<div class="col-sm-10 form-checkbox">
<s:checkbox name="xxxxx" ng-model="model"
ng-init="%{form.paid}"
theme="simple" />
</div>
No error is reported. I guess it is because Struts tags begin with <, which is not welcomed in Angular.
But, with this line no error is reported:
<select ng-model="estadoId"
ng-init="estadoId=<s:property value='%{form.estadoId}'/>"
name="form.estadoId" id="form.estadoId"
value="<s:property value='%{form.estadoId}' />" >
So, AngularJS is complaining about <> in Struts 2? Or, I am nor permitted to use <s:...> inside another <s:...>? If the latter is the case, why is complaning Angular not Struts 2??
In the first snippet, you are nesting Struts tags, that is a syntax error:
<s:checkbox name="xxxxx"
ng-model="model"
ng-init="model = <s:property value = '%{form.paid}' />"
theme="simple" />
In the second one, you are doing it right with OGNL, but you omitted the model = part:
<s:checkbox name="xxxxx"
ng-model="model"
ng-init="%{form.paid}"
theme="simple" />
The right version is the mix of the two:
<s:checkbox name="xxxxx"
ng-model="model"
ng-init="model = %{form.paid}"
theme="simple" />
Otherwise you could use a raw HTML tag (as in your <select> example, that is a standard HTML tag and not an <s:select>, hence no tags nesting is happening):
<input type="check" name="xxxxx"
ng-model="model"
ng-init="model = %{form.paid}" />
Note: in that case, you should create an hidden parameter under each checkbox to emulate the <s:checkbox> tag behavior and make the Checkbox Interceptor happy.

Struts ajax form validation

i have a problem with ajax form validation. I'm using struts-core 2.3.1.2, struts-jquery-plugin 3.3.0 and struts-json-plugin.
The problem occurs if an ajax form will be submitted by an ajax request and validation fails. Cause then the whole form will be placed on the result element. Therefore you can activate ajax validation on the ajax sumbit button.
http://code.google.com/p/struts2-jquery/wiki/Validation
Here are also outdated information:
http://struts.apache.org/2.2.3.1/docs/ajax-validation.html
But the interceptor "jsonValidationWorkflowStack" is missing in struts-default.xml like written in post: jsonValidationWorkflowStack seems to be removed in Struts 2.3.1
It is sourced out to the struts-json-plugin in struts-plugin.xml. I don't know how i can use this directly but i build my own Stack in struts.xml:
<!-- Sample JSON validation stack -->
<interceptor-stack name="jsonValidationWorkflowStack">
<interceptor-ref name="basicStack"/>
<interceptor-ref name="validation">
<param name="excludeMethods">input,back,cancel</param>
</interceptor-ref>
<interceptor-ref name="jsonValidation"/>
<interceptor-ref name="workflow"/>
</interceptor-stack>
</interceptors>
<action name="updateMySettings" method="execute" class="de.ra.daod.actions.MyAppSettingAction">
<interceptor-ref name="jsonValidationWorkflowStack"/>
<!-- This is not beauty within ajax -->
<result name="input">/WEB-INF/jsp/mysetting_ajax.jsp</result>
<result name="success" type="stream">
<param name="contentType">text/html</param>
<param name="inputName">inputStream</param>
</result>
</action>
And my form looks like:
<s:head />
<sj:head />
<!-- This files are needed for AJAX Validation of XHTML Forms -->
<script src="${pageContext.request.contextPath}/struts/xhtml/validation.js" type="text/javascript"></script>
<s:form id="form" action="private/updateMySettings" theme="xhtml">
<s:textfield id="screenRes" key="appSetting.screenResolution" label="Screen resolution" required="true" />
<s:select key="appSetting.screenDepth" label="Color depth" list="#{'8':'8','16':'16','24':'24'}" required="true" />
<sj:submit value="Update Settings" targets="status" validate="true"/>
</s:form>
Unfortunately i get a javascript error if validation fails or not:
Uncaught TypeError: Object #<Object> has no method 'indexOf'
f.extend.ajax jquery-1.7.1.min.js:4
b.fn.ajaxSubmit
a.struts2_jquery.validateForm jquery.struts2-3.3.0.min.js:18
a.subscribeHandler.h.beforeSubmit jquery.struts2-3.3.0.min.js:18
b.fn.ajaxSubmit
a.subscribeHandler.e jquery.struts2-3.3.0.min.js:18
e.extend.each jquery-1.7.1.min.js:2
a.subscribeHandler.e jquery.struts2-3.3.0.min.js:18
f.event.dispatch jquery-1.7.1.min.js:3
f.event.add.h.handle.i jquery-1.7.1.min.js:3
f.event.trigger jquery-1.7.1.min.js:3
f.fn.extend.trigger jquery-1.7.1.min.js:3
e.extend.each jquery-1.7.1.min.js:2
e.fn.e.each jquery-1.7.1.min.js:2
f.fn.extend.trigger jquery-1.7.1.min.js:3
d.fn.extend.publish jquery.subscribe.min.js:16
e.extend.each jquery-1.7.1.min.js:2
d.fn.extend.publish jquery.subscribe.min.js:16
(anonymous function) jquery.struts2-3.3.0.min.js:18
f.event.dispatch jquery-1.7.1.min.js:3
f.event.add.h.handle.i jquery-1.7.1.min.js:3
It seems that the json object from the response can't be handled and i don't know why cause i followed the old instructions. I assume the cause is the function StrutsUtils.getValidationErrors from struts/utils.js if this function is used with the json object but i'm not sure. Can anyone help ?
I guess stream result and ajax doesn't play well together. just remove the targets
attribute. – jogep
I don't agree with you because the tenor of AJAX isn't load a new page but rather load pieces of whatever to the current page that is why i use an AJAX submit button. To notice the user of the action without reloading the page itself. The workaround is to clear the content of the status div element with javascript. I think that can be automated in struts2-jquery-plugin. This is my form which is embedded in tabbed pane.
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%# taglib prefix="s" uri="/struts-tags" %>
<%# taglib prefix="sj" uri="/struts-jquery-tags"%>
<s:if test="appSetting != null">
<h3>Application settings</h3>
<div id="status" class="welcome"></div>
<table>
<tr>
<td>
<s:form id="form" action="updateMySettings" theme="xhtml">
<s:textfield id="screenRes" key="appSetting.screenResolution"
label="Screen resolution" required="true" />
<s:select key="appSetting.screenDepth" label="Color depth"
list="#{'8':'8','16':'16','24':'24'}" required="true" />
<sj:submit id="updateSetting" value="Update Settings" targets="status" validate="true" />
</s:form>
</td>
<td valign="top">
<button id="fullScreen">Use full screen</button>
<button id="fullBrowser">Use full browser</button>
</td>
</tr>
</table>
<script>
$("#fullScreen").click(function() {
scr_width = Math.max(screen.width,screen.height);
scr_height = Math.min(screen.width,screen.height);
$("#screenRes").val(scr_width + 'x' + scr_height);
});
$("#fullBrowser").click(function() {
brw_width = Math.max(window.innerWidth, window.innerHeight);
brw_height = Math.min(window.innerWidth, window.innerHeight);
$("#screenRes").val(brw_width + 'x' + brw_height);
});
$("#updateSetting").click(function() {
$("#status").empty();
})
</script>
</s:if>
<s:else>
<p>No settings available.</p>
</s:else>
It works very well.
You are step in an solved Struts2 Issue. Upgrade to latest version 2.3.3 or 2.3.4 and your problem should be gone.

How to Get Model Data from Partial View?

I am creating a site in which I utilize partial views to display various bits of data about a single Model. Here is a bit of the HTML. (Note, all of these are contained within a single form and the Index page that these partials are rendered in is strongly typed to the main model. The main model contains various lists of data.)
<div id="tab1"><% Html.RenderPartial("Tab1", Model); %></div>
<div id="tab2"><% Html.RenderPartial("Tab2", Model.AnItemList1.FirstOrDefault<AnItemList1>()); %></div>
<div id="tab3"><% Html.RenderPartial("Tab3", Model.AnItemList2.FirstOrDefault()); %></div>
Here is ONE of the partial views headers (for 'tab2'):
<%# Language="C#" Inherits="System.Web.Mvc.ViewUserControl<AnItem1>" %>
The pages display correctly. The issue is that, when I enter data into the various parts of the partial pages and then submit the entire form (via POST), the data is not making it back to my data store (MSSQL) - but this only happens for any of the list items (that are contained within the Model). The first partial page does properly have its data set within the data store.
What am I doing wrong here? Should I only be passing the model to Html.RenderPartial and then get the specific model I need on the partial page? Should I pass the entire list and then get the first (right now, I only care about the first item in the list - that will EVENTUALLY change, but not any time soon).
Suggestions or thoughts appreciated.
Update: Here is how I accessing the properties on the partial views.
<div class="data-group">
<%: Html.CheckBoxFor(model => model.Property1) %>
<%: Html.LabelFor(model => model.Property1) %>
</div>
Update 2: Per request...
Controller Action (ScenarioController):
public ActionResult Index(int id = 0)
{
if (id == 0)
{
SavedScenario scenario = new SavedScenario();
scenario.AnItemList1.Add(new AnItem1());
scenario.AnItemList2.Add(new AnItem2());
return View("Index", scenario);
}
else
{
SavedScenario scenario = repository.GetScenario(id);
if (scenario == null)
return View("NotFound");
else
return View("Index", scenario);
}
}
[HttpPost]
public ActionResult Index(SavedScenario scenario)
{
if (ModelState.IsValid && TryUpdateModel(scenario, "SaveScenario"))
{
repository.Add(scenario);
repository.Save();
}
return View(scenario);
}
Rendered HTML (I can only include parts of it - this is a small sample of what is in the form):
<form action="/Scenario" id="form0" method="post">
<!-- This is the one that works - the basic Scenario. Top level. -->
<fieldset>
<legend>Scenario Information</legend>
<div class="data-group">
<div class="editor-label">
<label for="ScenarioName">Scenario Name</label>
</div>
<div class="option1">
<input class="wide" id="ScenarioName" name="ScenarioName" type="text" value="" />
</div>
<div class="validation">
<div><span class="field-validation-valid" id="ScenarioName_validationMessage"></span></div>
</div>
</div>
</fieldset>
<!-- This does not work or get submitted (as far as I can tell). -->
<div id="tab2">
<fieldset>
<legend>Tab2</legend>
<div class="data-group">
<input id="Property1" name="Property1" type="checkbox" value="true" /><input name="Property1" type="hidden" value="false" />
<label for="Property1" />
</div>
</div>
</fieldset>
</form>
My apologies for having to keep this so generic.
Hard to guess from this much code. However you should make sure that all properties of your models have the same prefix when they are posted back to the server
Edit: form field names should match property names of your model to correctly bind all values. You have two fields with the same name that you can bind in following way
[HttpPost]
public ActionResult Index(SavedScenario scenario, List<bool> Property1)
{
// here you can do with values coming in property1
if (ModelState.IsValid && TryUpdateModel(scenario, "SaveScenario"))
{
repository.Add(scenario);
repository.Save();
}
return View(scenario);
}
It might be issue with naming the fields on your partial forms. Try naming the fields on your partial views by prefixing it with the name of the Model passed into it...like 'AnItemList1.name' instead of just 'name'..I am just guessing here though...but that's what I did sometimes to fix the problem when I was getting values as null..

HTML button calling an MVC Controller and Action method

I know this isn't right, but for the sake of illustration I'd like to do something like this:
<%= Html.Button("Action", "Controller") %>
My goal is to make an HTML button that will call my MVC controller's action method.
No need to use a form at all unless you want to post to the action. An input button (not submit) will do the trick.
<input type="button"
value="Go Somewhere Else"
onclick="location.href='<%: Url.Action("Action", "Controller") %>'" />
Razor syntax is here:
<input type="button" value="Create" onclick="location.href='#Url.Action("Create", "User")'" />
<button type="button" onclick="location.href='#Url.Action("MyAction", "MyController")'" />
type="button" prevents page from submitting, instead it performs your action.
Try this:
#Html.ActionLink("DisplayText", "Action", "Controller", route, attribute)
This should work for you.
You can use Url.Action to specify generate the url to a controller action, so you could use either of the following:
<form method="post" action="<%: Url.Action("About", "Home") %>">
<input type="submit" value="Click me to go to /Home/About" />
</form>
or:
<form action="#">
<input type="submit" onclick="parent.location='<%: Url.Action("About", "Home") %>';return false;" value="Click me to go to /Home/About" />
<input type="submit" onclick="parent.location='<%: Url.Action("Register", "Account") %>';return false;" value="Click me to go to /Account/Register" />
</form>
This is how you can submit your form to a specific controller and action method in Razor.
<input type="submit" value="Upload" onclick="location.href='#Url.Action("ActionName", "ControllerName")'" />
Building on couple of the above answers, you could do this:
<button onclick="location.href='#Url.Action("ActionName", "ControllerName")'" />
Of all the suggestions, nobdy used the razor syntax (this is with bootstrap styles as well). This will make a button that redirects to the Login view in the Account controller:
<form>
<button class="btn btn-primary" asp-action="Login" asp-
controller="Account">#Localizer["Login"]</button>
</form>
it's better use this example
<a href="#Url.Action("Register","Account", new {id=Item.id })"
class="btn btn-primary btn-lg">Register</a>
The HTML <button> element can only postback to the form containing it.
Therefore, you need to make a form that POSTs to the action, then put a <button> or <input type="submit" /> in the form.
So, I'm using Razor but this will work using either. I'm basically wrapping a button in a link.
<a href="Controller/ActionMethod">
<input type="button" value="Click Me" />
</a>
Despite onclick Method you can also use formaction as follows:
<button type="submit" id="button1" name="button1" formaction='#Url.Action("Action", "Controller")'>Save</button>
Use this example :
<button name="nameButton" id="idButton" title="your title" class="btn btn-secondary" onclick="location.href='#Url.Action( "Index","Controller" new { id = Item.id })';return false;">valueButton</button>
In case if you are getting an error as "unterminated string constant", use the following razor syntax :
<input type="button" onclick="#("location.href='"+ Url.Action("Index","Test")+ "'")" />
When you implement the action in the controller, use
return View("Index");
or
return RedirectToAction("Index");
where Index.cshtml (or the page that generates the action) page is already defined. Otherwise you are likely encountering "the view or its master was not found..." error.
Source: https://blogs.msdn.microsoft.com/aspnetue/2010/09/17/best-practices-for-asp-net-mvc/
If you are in home page ("/Home/Index") and you would like to call Index action of Admin controller, following would work for you.
<li>Admin</li>
it's better use this example.
Call action and controller using a ActionLink:
#Html.ActionLink("Submit", "Action", "Controller", route, new { #class = "btn btn-block"})
OK, you basically need to pass the action to the button and call it when click happens, it doesn't need to be inside a from, you can use HTML onclick on button to trigger it when the button get clicked...
<button id="my-button" onclick="location.href='#Url.Action("YourActionName", "YourControllerName")'">Submit</button>
You can always play around with htmlHelpers and build some stuff
public static IHtmlContent BtnWithAction(this IHtmlHelper htmlHelper, string id, string text, string css="", string action="", string controller="")
{
try
{
string str = $"<button id=\"{id}\" class=\"{css}\" type=\"button\" ###>{text}</button>";
if (!string.IsNullOrEmpty(action) && !string.IsNullOrEmpty(controller))
{
string url = ((TagBuilder)htmlHelper.ActionLink("dummy", action, controller)).Attributes["href"];
var click = !string.IsNullOrEmpty(url) ? $"onclick=\"location.href='{url}'\"" : string.Empty;
return new HtmlString(str.Replace("###", click));
}
return new HtmlString(str.Replace("###", string.Empty));
}
catch (Exception ex)
{
Log.Error(ex, ex.Message);
var fkup = "<script>alert(\"assumption is the mother of all failures\")</script>";
return new HtmlString(fkup);
}
}
And then on the view call it like this
#Html.BtnWithAction("btnCaretakerBack", "Back", "btn btn-primary float-right", "Index", "Caretakers")