What's the true purpose of setJavaScriptCanOpenWindowsAutomatically - javascript

According to Android document:
Tells JavaScript to open windows automatically. This applies to the JavaScript function window.open(). The default is false.
But I didn't see any different between use or not use this API in my code, window.open works fine in the both cases, so what's the true purpose of this API or my usage is not correct?
My code sample as below:
public class MainActivity extends Activity {
private WebView mWebView;
//private Context mContext
RelativeLayout mParent;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mParent = new RelativeLayout(MainActivity.this);
setContentView(mParent);
mWebView = new WebView(MainActivity.this);
WebSettings settings = mWebView.getSettings();
settings.setSupportMultipleWindows(true);
settings.setJavaScriptCanOpenWindowsAutomatically(false);
settings.setJavaScriptEnabled(true);
mParent.addView(mWebView);
mWebView.loadUrl("file:///android_asset/create_window.html");
mWebView.setWebViewClient(new MyWebViewClient());
mWebView.setWebChromeClient(new WebChromeClient() {
#Override
public boolean onCreateWindow(WebView view, boolean dialog, boolean userGesture, Message resultMsg) {
WebView newWebView = new WebView(view.getContext());
newWebView.getSettings().setUserAgentString("BBB");
//newWebView.getSettings().setJavaScriptEnabled(true);
//newWebView.setWebChromeClient(this);
newWebView.setWebViewClient(new MyWebViewClient());
//view.getSettings().setUserAgentString("CCC");
mParent.addView(newWebView);
WebView.WebViewTransport transport = (WebView.WebViewTransport) resultMsg.obj;
transport.setWebView(newWebView);
resultMsg.sendToTarget();
return true;
}
#Override
public void onCloseWindow(WebView window) {
mParent.removeViewAt(mParent.getChildCount() - 1);
}
});
}
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
#Override
public void onPageFinished(WebView view, final String url) {
super.onPageFinished(view, url);
}
}
}
The content of create_window.html as below:
<html>
<!-- scripts -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Create Window</title>
<script>
function
openURL ()
{
alert ("opening window");
window.open ("http://www.whoishostingthis.com/tools/user-agent/", "_blank", "width=200,height=100");
}
</script>
</head>
<body>
<div>
<button onclick="openURL()">open website in new window</button>
</div>
</body>
</html>

Related

Maui сhange navigation in BlazorWebView

How can I change a page in BlazorWebView using Maui? For example, the page '/' was opened and I need to open '/fetch'.
I found how to return to the previous link via js.
Created a Custom Navigation Manager:
public class CustomNavigationManager
{
private static IJSRuntime JSRuntime { get; set; }
public CustomNavigationManager(IJSRuntime jSRuntime)
{
JSRuntime = jSRuntime;
}
public static async Task Navigation(string url)
{
//Microsoft.Maui.Platform;
if (JSRuntime!= null)
{
await JSRuntime.InvokeVoidAsync("navigation", url);
}
}
}
Which calls the Js code. Which calls the Js code. Which I placed in wwwroot/index.html
<script type="text/javascript">
window.navigation = (url) => {
window.location.href = url; // Error: There is no content at fetch.
//history.back();
//window.location="https://0.0.0.0/fetch"; //Error: There is no content at fetch.
}
</script>
Registering a service
builder.Services.AddTransient<CustomNavigationManager>();
And inject in Shared/MainLayout.razor
#page "/"
#inject CustomNavigationManager navigation
And I use it in maui
await CustomNavigationManager.Navigation("/fetch");
If I use the js code history.back(); then everything works,
but if I want to redirect to /fetch using
window.location.href = url;
then I get an error: There is no content at fetch.
Fetch.razor page
#page "/fetch"
#page "/fetch/{id}"
<h1>Test!</h1>
Fetch.razor
#page "/fetch"
#page "/fetch/{text}"
<h3>#Text</h3>
#code
{
[Parameter]
public string Text { get; set; }
}
MainLayout.razor
#inherits LayoutComponentBase
#inject CustomNavigationManager navigation
<div class="page">
<div class="sidebar">
<NavMenu />
</div>
<main>
<div class="top-row px-4">
About
</div>
<article class="content px-4">
#Body
</article>
<div>#Url</div>
</main>
</div>
#code
{
[Inject]
private NavigationManager MyNavigationManager { get; set; }
private string Url;
protected override void OnInitialized()
{
base.OnInitialized();
MyNavigationManager.LocationChanged += OnLocationChanges;
Url = MyNavigationManager.Uri;
}
private void OnLocationChanges(object sender, LocationChangedEventArgs e)
{
Url = e.Location;
StateHasChanged();
}
}
CustomNavigationManager.cs
public class CustomNavigationManager
{
private static NavigationManager _navigationManager;
public CustomNavigationManager(NavigationManager MyNavigationManager)
{
_navigationManager = MyNavigationManager;
}
public static void Navigation(string url)
{
if (_navigationManager!=null)
{
_navigationManager.NavigateTo(url);
}
}
}
Decided so: Subscribed to the navigation change event. Implemented the service, and called NavigateTo. It didn't work through Js. Note: BlazorWebView must already download the project, otherwise nothing will work)

Spring boot AJAX POST request and knockout.js debug error from user interface

I have two java models as player and team. I have a controller class. I get an error when I send a post request via ajax from the user interface. I've solved errors like CORS. But there is an error I can't solve; The knockout.js debug error and the post request are not occurring. There is no change in my database. I shared the model classes, the controller class, the html and javascriptfiles.
What do I need to change? Could you help?
Player Model
#Entity
#Table(name = "player")
public class Player{
#Id
#GeneratedValue
#NotNull
#Column
private int id;
#NotNull
#Column
private String playerName;
#NotNull
#Column
private String playerSurname;
#Column
private int playerAge;
public String getPlayerName() {
return playerName;
}
public void setPlayerName(String playerName) {
this.playerName = playerName;
}
public String getPlayerSurname() {
return playerSurname;
}
public void setPlayerSurname(String playerSurname) {
this.playerSurname = playerSurname;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getPlayerAge() {
return playerAge;
}
public void setPlayerAge(int playerAge) {
this.playerAge = playerAge;
}
}
Team Model
#Entity
#Table(name = "team")
public class Team {
#Id
#GeneratedValue
#NotNull
#Column
private int id;
#NotNull
#Column
private String teamName;
#Column
private String teamCountry;
public Team(){
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTeamName() {
return teamName;
}
public void setTeamName(String teamName) {
this.teamName = teamName;
}
public String getTeamCountry() {
return teamCountry;
}
public void setTeamCountry(String teamCountry) {
this.teamCountry = teamCountry;
}
#ManyToOne
private Player player;
public Player getPlayer() {
return player;
}
public void setPlayer(Player player) {
this.player = player;
}
}
Controller
#CrossOrigin(origins = "http://localhost:8000/")
#RestController
public class TeamController {
#Autowired
PlayerRepository playerRepository;
#Autowired
TeamRepository teamRepository;
#RequestMapping("/")
public void main() {
//create players by manual
Player messi = new Player();
Player ronaldo = new Player();
Player ozil = new Player();
messi.setPlayerName("Lionel");
messi.setPlayerSurname("Messi");
messi.setPlayerAge(31);
ronaldo.setPlayerName("Cristiano");
ronaldo.setPlayerSurname("Ronaldo");
ronaldo.setPlayerAge(32);
ozil.setPlayerName("Mesut");
ozil.setPlayerSurname("Ozil");
ozil.setPlayerAge(29);
this.playerRepository.save(messi);
this.playerRepository.save(ronaldo);
this.playerRepository.save(ozil);
//create teams by manual
Team barcelona = new Team();
Team juventus = new Team();
Team arsenal = new Team();
barcelona.setTeamName("Barcelona");
barcelona.setTeamCountry("Spain");
barcelona.setPlayer(messi);
juventus.setTeamName("Juventus");
juventus.setTeamCountry("Italy");
juventus.setPlayer(ronaldo);
arsenal.setTeamName("Arsenal");
arsenal.setTeamCountry("England");
arsenal.setPlayer(ozil);
this.teamRepository.save(barcelona);
this.teamRepository.save(juventus);
this.teamRepository.save(arsenal);
}
//**PLAYER**
#GetMapping(value="/getAllPLayers")
public List<Player> getAllPlayers(){
return playerRepository.findAll();
}
#PostMapping(value="getPlayerByName")
public List<Player> getPlayerByName(#RequestParam("playerName") String playerName){
return playerRepository.findByPlayerName(playerName);
}
#PostMapping(value="getPlayerBySurname")
public List<Player> getPlayerBySurname(#RequestParam("playerSurname") String playerSurname){
return playerRepository.findByPlayerSurname(playerSurname);
}
#PostMapping(value="getPlayerByAge")
public List<Player> getPlayerByAge(#RequestParam("playerAge") int playerAge){
return playerRepository.findByPlayerAge(playerAge);
}
#PostMapping(value="createNewPlayer")
public Player createNewPlayer(#Valid #RequestBody Player player) {
return playerRepository.save(player);
}
//**TEAM**
#GetMapping(value="/getAllTeams")
public List<Team> getAllTeams(){
return teamRepository.findAll();
}
#PostMapping(value = "/getTeamsByName")
public List<Team> getTeamByName(#RequestParam("teamName") String teamName){
return teamRepository.findByTeamName(teamName);
}
#PostMapping(value = "/getTeamsByCountry")
public List<Team> getTeamByCountry(#RequestParam("teamCountry") String teamCountry){
return teamRepository.findByTeamCountry(teamCountry);
}
#PostMapping(value="/createNewTeam")
public Team createNewTeam(#Valid #RequestBody Player player,#RequestBody Team team) {
playerRepository.save(player);
team.setPlayer(player);
return teamRepository.save(team);
}
}
Config
#Configuration
#EnableWebMvc
public class WebConfig implements Filter,WebMvcConfigurer {
#Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**");
}
#Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) {
HttpServletResponse response = (HttpServletResponse) res;
HttpServletRequest request = (HttpServletRequest) req;
System.out.println("WebConfig; "+request.getRequestURI());
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Methods", "POST, PUT, GET, OPTIONS, DELETE");
response.setHeader("Access-Control-Allow-Headers", "Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With,observe");
response.setHeader("Access-Control-Max-Age", "3600");
response.setHeader("Access-Control-Allow-Credentials", "true");
response.setHeader("Access-Control-Expose-Headers", "Authorization");
response.addHeader("Access-Control-Expose-Headers", "USERID");
response.addHeader("Access-Control-Expose-Headers", "ROLE");
response.addHeader("Access-Control-Expose-Headers", "responseType");
response.addHeader("Access-Control-Expose-Headers", "observe");
System.out.println("Request Method: "+request.getMethod());
if (!(request.getMethod().equalsIgnoreCase("OPTIONS"))) {
try {
chain.doFilter(req, res);
} catch(Exception e) {
e.printStackTrace();
}
} else {
System.out.println("Pre-flight");
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Methods", "POST,GET,DELETE,PUT");
response.setHeader("Access-Control-Max-Age", "3600");
response.setHeader("Access-Control-Allow-Headers", "Access-Control-Expose-Headers"+"Authorization, content-type," +
"USERID"+"ROLE"+
"access-control-request-headers,access-control-request-method,accept,origin,authorization,x-requested-with,responseType,observe");
response.setStatus(HttpServletResponse.SC_OK);
}
}
}
Player Repo
public interface PlayerRepository extends JpaRepository<Player, Integer>{
List<Player> findByPlayerName(String playerName);
List<Player> findByPlayerSurname(String playerSurname);
List<Player> findByPlayerAge(int playerAge);
}
Team Repo
public interface TeamRepository extends JpaRepository<Team, Integer>{
List<Team> findByTeamName(String teamName);
List<Team> findByTeamCountry(String teamCountry);
}
POST HTML Page
<!DOCTYPE HTML>
<html>
<head>
<title>Team Management</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" />
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script
src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.0/umd/popper.min.js"></script>
<script
src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js"></script>
<script src="js/restGetPlayer.js"></script>
<script src="js/restGetTeam.js"></script>
<script src="js/postTeam.js"></script>
</head>
<body>
<div class="container">
<h3 style="color: darkolivegreen" align="center">Create a new team</h3>
<div class"main-block>
<form id="teamForm" class="form-inline" style="margin:20px 20px 20px 20px">
<div class="form-group">
<label for="playerName" style="margin-left:20px; margin-right:5px">Player Name:</label>
<input type="text" class="form-control" id="playerName" placeholder="Enter player name"/>
</div>
<br>
<div class="form-group">
<label for="playerSurname" style="margin-left:20px; margin-right:5px">Player Surname:</label>
<input type="text" class="form-control" id="playerSurname" placeholder="Enter player surname"/>
</div>
<div class="form-group">
<label for="playerAge" style="margin-left:20px; margin-right:5px">Player Age:</label>
<input type="text" class="form-control" id="playerAge" placeholder="Enter player age"/>
</div>
<br style="clear: both;">
<div class="form-group">
<label for="teamName" style="margin-right:5px">Team Name:</label>
<input type="text" class="form-control" id="teamName" placeholder="Enter team name"/>
</div>
<br>
<div class="form-group">
<label for="teamCountry" style="margin-left:20px; margin-right:5px">Team Country:</label>
<input type="text" class="form-control" id="teamCountry" placeholder="Enter team country"/>
</div>
<br>
<hr>
<button type="submit" class="btn btn-default" style="margin-left:20px; margin-right:5px" >Submit Team</button>
</form>
</div>
<div class="col-sm-7" style="margin:20px 0px 20px 0px">
<button id="getAllTeams" class="btn btn-primary">Get Team Information</button>
<div id="getResultDiv3" style="padding:20px 10px 20px 50px">
<ul class="list-group">
</ul>
</div>
</div>
</div>
<style>
.container {
max-width: 400px;
width: 100%;
margin: 0 auto;
position: relative;
}
#contact {
background: #F9F9F9;
padding: 25px;
margin: 150px 0;
box-shadow: 0 0 20px 0 rgba(0, 0, 0, 0.2), 0 5px 5px 0 rgba(0, 0, 0, 0.24);
}
</style>
<script type="text/javascript" src="js/libs/require/require.js"></script>
<script type="text/javascript" src="js/main.js"></script>
</body>
</html>
AJAX POST
$( document ).ready(function() {
$("#teamForm").submit(function(event) {
event.preventDefault();
ajaxPost();
});
function ajaxPost(){
var formData = {
playerName : $("#playerName").val(),
playerSurname : $("#playerSurname").val(),
playerAge : $("#playerAge").val(),
teamName : $("#teamName").val(),
teamCountry : $("#teamCountry").val()
}
$.ajax({
type : "POST",
contentType : "application/json",
url : "http://localhost:8080" + "/createNewTeam",
data : JSON.stringify(formData),
dataType : 'json',
success: function(result){
alert("Teams loaded!");
console.log(result);
$('#getResultDiv3 ul').empty();
var tableTitle = "<h2><strong>Team List</strong></h2>";
var teamList = "<table border='1'>";
teamList += "<tr><td><strong>Team Name</strong></td><td><strong>Team Country</strong></td><td><strong>Player Name</strong></td><td><strong>Player Surname</strong></td><td><strong>Player Age</strong></td></tr>";
$.each(result, function(i, teams){
teamList +="<tr>";
teamList +="<td>"+teams.teamName +"</td>";
teamList +="<td>"+teams.teamCountry+"</td>";
teamList +="<td>"+teams.player.playerName+"</td>";
teamList +="<td>"+teams.player.playerSurname+"</td>";
teamList +="<td>"+teams.player.playerAge+"</td>";
teamList +="</tr>";
});
teamList +="</table>";
$('#getResultDiv3').append(tableTitle, teamList)
console.log("Success: ", result);
},
error : function(e) {
$("#getResultDiv3").html("<strong>Error</strong>");
console.log("ERROR: ", e);
}
});
resetData();
}
function resetData(){
$("#playerName").val("");
$("#playerSurname").val("");
$("#playerAge").val("");
$("#teamName").val("");
$("#teamCountry").val("");
}
})
MainController.js
/**
* #license
* Copyright (c) 2014, 2019, Oracle and/or its affiliates.
* The Universal Permissive License (UPL), Version 1.0
*/
/*
* Your application specific code will go here
*/
define(['ojs/ojresponsiveutils', 'ojs/ojresponsiveknockoututils', 'knockout', 'ojs/ojknockout'],
function(ResponsiveUtils, ResponsiveKnockoutUtils, ko) {
function ControllerViewModel() {
var self = this;
// Media queries for repsonsive layouts
var smQuery = ResponsiveUtils.getFrameworkQuery(ResponsiveUtils.FRAMEWORK_QUERY_KEY.SM_ONLY);
self.smScreen = ResponsiveKnockoutUtils.createMediaQueryObservable(smQuery);
// Header
// Application Name used in Branding Area
self.appName = ko.observable("Oracle JET + REST API");
// User Info used in Global Navigation area
self.userLogin = ko.observable("admin#oracle.com");
// Footer
function footerLink(name, id, linkTarget) {
this.name = name;
this.linkId = id;
this.linkTarget = linkTarget;
}
self.footerLinks = ko.observableArray([
new footerLink('About Oracle', 'aboutOracle', 'http://www.oracle.com/us/corporate/index.html#menu-about'),
new footerLink('Contact Us', 'contactUs', 'http://www.oracle.com/us/corporate/contact/index.html'),
new footerLink('Legal Notices', 'legalNotices', 'http://www.oracle.com/us/legal/index.html'),
new footerLink('Terms Of Use', 'termsOfUse', 'http://www.oracle.com/us/legal/terms/index.html'),
new footerLink('Your Privacy Rights', 'yourPrivacyRights', 'http://www.oracle.com/us/legal/privacy/index.html')
]);
}
return new ControllerViewModel();
}
);
Picture 1:
Adding Player
Picture 2:
ERROR log After add player
Picture 3:
MySQL player table did not any change
Answering your question, it is not doing anything because you are not sending the parameter Player (that must be valid) due you are using a form this is not the way you should do it. (request params are send via url and your form should do it via #RequestBody)
Check this post for information. Can I use #Requestparam annotation for a Post request?
I would do something like this:
On my controller due I can have only one #RequestBody but my form sends an object containing data for multiple types of an object I would do a Dto
Something like this:
#PostMapping(value="/createNewTeam")
public Team createNewTeam(#Valid #RequestBody PayloadDto payloadDto) {
final Player player = new Player();
player.setPlayerName(payloadDto.playerName);
// fill player information
final Team team = new Team();
// fill team information...
playerRepository.save(player);
return teamRepository.save(team);
}
Your PayloadDto object must contain all attributes you are sending..
public class PayloadDto{
#JsonProperty("playerName")
public String playerName;
#JsonProperty("playerSurname")
public String playerSurname;
// .. more player and team properties
}
Be sure that the jsonProperty("propertyName) holds the same name you are using when sending the information

AJAX POST not working in android studio webview

I have simple login page which uses AJAX POST the page works fine in the browsers. I have created an android studio project with the web view and given the link of the login page, I have also included HTML 5 features for the page, but the login page does not send or receive any data when i run it on the android studio emulator.
The AJAX POST CODE is as follows
var ins = empID + "~" + empPassword;
$.ajax({
type: "POST",
contentType: "application/json",
url: "http://Servername/Service1.svc/json/EmpLogin",
data: '{"EDetailslogin": "' + ins + '"}',
crossDomain: true,
dataType: "json",
processData: false,
success: function(data) {
widow.location.href = "Page2.html";
},
error: function(data) {
alert("User Not Recognized");
}
});
The android manifest code is as follows
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.Myproj.Sample">
<uses-permission android:name="android.permission.INTERNET" />
<application android:allowBackup="true" android:icon="#mipmap/ic_launcher" android:label="#string/app_name" android:roundIcon="#mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
My Main Activity.java file has the following code with the HTML-5 features added to the page.
public class MainActivity extends AppCompatActivity {
private WebView wv1;
public String TAG;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
wv1 = (WebView) findViewById(R.id.webView);
wv1.setWebViewClient(new MyBrowser());
String url = "file:///android_asset/login.html";
wv1.getSettings().setLoadsImagesAutomatically(true);
wv1.getSettings().setJavaScriptEnabled(true);
wv1.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
WebSettings ws = wv1.getSettings();
ws.setJavaScriptEnabled(true);
ws.setAllowFileAccess(true);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ECLAIR) {
try {
Log.d(TAG, "Enabling HTML5-Features");
Method m1 = WebSettings.class.getMethod("setDomStorageEnabled", new Class[] {
Boolean.TYPE
});
m1.invoke(ws, Boolean.TRUE);
Method m2 = WebSettings.class.getMethod("setDatabaseEnabled", new Class[] {
Boolean.TYPE
});
m2.invoke(ws, Boolean.TRUE);
Method m3 = WebSettings.class.getMethod("setDatabasePath", new Class[] {
String.class
});
m3.invoke(ws, "/data/data/" + getPackageName() + "/databases/");
Method m4 = WebSettings.class.getMethod("setAppCacheMaxSize", new Class[] {
Long.TYPE
});
m4.invoke(ws, 1024 * 1024 * 8);
Method m5 = WebSettings.class.getMethod("setAppCachePath", new Class[] {
String.class
});
m5.invoke(ws, "/data/data/" + getPackageName() + "/cache/");
Method m6 = WebSettings.class.getMethod("setAppCacheEnabled", new Class[] {
Boolean.TYPE
});
m6.invoke(ws, Boolean.TRUE);
Log.d(TAG, "Enabled HTML5-Features");
} catch (NoSuchMethodException e) {
Log.e(TAG, "Reflection fail", e);
} catch (InvocationTargetException e) {
Log.e(TAG, "Reflection fail", e);
} catch (IllegalAccessException e) {
Log.e(TAG, "Reflection fail", e);
}
}
wv1.loadUrl(url);
//wv1.loadUrl("file:///android_asset/index.html");
}
private class MyBrowser extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
}
I have also Imported all the necessary class files. Still the application does not seem to work. Can anyone please help me with this.

Uncaught TypeError: window.HybridApp.setMessage is not a function error

I want to make an app for hybridApp but some errors occurred.
"Uncaught TypeError: window.HybridApp.setMessage is not a function error"
I tried to find the cause, but I could not.
proguard
-keepattributes JavascriptInterface
-keep public class com.test.crosswalkdemo2$AndroidBridge
-keep public class * implements com.test.crosswalkdemo2$AndroidBridge
-keepclassmembers class com.test.crosswalkdemo2$AndroidBridge {
<methods>;
}
-keepclassmembers class * {
#android.webkit.JavascriptInterface <methods>;
}
main
public class AndroidBridge {
#JavascriptInterface
public void setMessage(final String text) { // must be final
handler.post(new Runnable() {
public void run() {
mTextView.setText(text);
}
});
}
}
html
<script language="JavaScript">
function setMessage(arg) {
document.getElementById('textMessageFromApp').innerHTML = arg;
}
function sendMessage(msg){
window.HybridApp.setMessage(msg);
}
</script>
</head>
<body>
<h2>Hybrid App (WEB+APP)</h2>
<hr/>
<input type="text" id="textMessageFromWeb" value="Hello, Hybrid(WEB)!!"/>
<input type="button" value="Send" onclick="sendMessage(document.getElementById('textMessageFromWeb').value)"/>
please see my code and help.

How can I add form fields when I click on link only by using Struts2 without javascript/jquery

How can I add form fields when I click on link only by using Struts2, jsp without javascript/jquery.
I want when I click on addCompte link in the same page jsp add more fields about compte in addition of client fields already exist.
I want to stay in the same jsp page, when user click on the link more fields about Account display. the Question is when user click on the link which expression should me use in
This my client.jsp code:
<body>
<h1>Show de Clients</h1>
<s:form action="updateClient">
<s:textfield name="nom" label="Nom"/>
<s:textfield name="prenom" label="Prenom"/>
<s:textfield name="age" label="Age"/>
<s:textfield name="codeClient" label="CodeClient"/>
<s:url var="addCompteURL" action="addCompteToClient">
<s:param name="codeClient" value="%{codeClient}"></s:param>
<s:hidden name="moreCompte" value="yes" label="hide"></s:hidden>
</s:url> <s:a href="%{addCompteURL}" onclick="">addCompte</s:a>
<s:if test="%{moreCompte!=null}">
<s:textfield name="numeroCompte" label="NumeroCompte"/>
<s:textfield name="solde" label="Solde"/>
<s:textfield name="dateCreation" label="DateCreation"/>
<s:submit action="addCompteToClient"
value="addCompteToClient" align="center">
</s:submit>
</s:if>
<s:else>
<s:submit action="updateClient" value="update client"
align="center">
</s:submit>
</s:else>
</s:form>
</body>
this my action code ClientAction.java:
public class ClientAction extends ActionSupport{
private int codeClient;
private String nom;
private String prenom;
private int age;
private String adresse;
private String numeroCompte;
private double solde;
private Date dateCreation;
private Client client;
List<Client> clientList;
ServiceMetier serviceMetier= new ServiceMetierImpl();
public int getCodeClient() {
return codeClient;
}
public void setCodeClient(int codeClient) {
this.codeClient = codeClient;
}
public String getNom() {
return nom;
}
public void setNom(String nom) {
this.nom = nom;
}
public String getPrenom() {
return prenom;
}
public void setPrenom(String prenom) {
this.prenom = prenom;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getAdresse() {
return adresse;
}
public void setAdresse(String adresse) {
this.adresse = adresse;
}
public String getNumeroCompte() {
return numeroCompte;
}
public void setNumeroCompte(String numeroCompte) {
this.numeroCompte = numeroCompte;
}
public double getSolde() {
return solde;
}
public void setSolde(double solde) {
this.solde = solde;
}
public Date getDateCreation() {
return dateCreation;
}
public void setDateCreation(Date dateCreation) {
this.dateCreation = dateCreation;
}
public Client getClient() {
return client;
}
public void setClient(Client client) {
this.client = client;
}
public List<Client> getClientList() {
System.out.println("methode getClienList");
return clientList;
}
public void setClientList(List<Client> clientList) {
System.out.println("methode setClientList");
this.clientList = clientList;
}
public String init(){
// clientList = serviceMetier.getAllClient();
System.out.println("methode init ");
return SUCCESS;
}
public String afficher(){
clientList=serviceMetier.getAllClient();
// client=serviceMetier.getClient(9);
System.out.println("methode afficher "+clientList);
return SUCCESS;
}
public String ajouter(){
System.out.println("methode afficher "+clientList);
BanqueFactory banque=new BanqueFactory();
Compte compte=banque.buildCompte();
compte.setNumeroCompte(numeroCompte);
compte.setSolde(solde);
compte.setDateCreation(dateCreation);
Client client=banque.buildClient();
client.setCodeClient(codeClient);
client.setNom(nom);
client.setPrenom(prenom);
client.setAge(age);
client.setAdresse(adresse);
client.addCompte(compte);
serviceMetier.createClient(client);
clientList=serviceMetier.getAllClient();
return SUCCESS;
}
public String addCompte(){
System.out.println("methode addCompte");
/*
BanqueFactory banque=new BanqueFactory();
Compte compte=banque.buildCompte();
compte.setNumeroCompte(numeroCompte);
compte.setSolde(solde);
compte.setDateCreation(dateCreation);
Client client=banque.buildClient();
client.setCodeClient(codeClient);
client.setNom(nom);
client.setPrenom(prenom);
client.setAge(age);
client.setAdresse(adresse);
client.addCompte(compte);
serviceMetier.updateClient(client);
// if(codeClient>0){
// serviceMetier.updateClient(codeClient);
// codeClient=0;
// }
*/
client=serviceMetier.getClient(codeClient);
return SUCCESS;
}
public String update(){
client=serviceMetier.getClient(codeClient);
if(nom!=null){
client.setNom(nom);
}
if(prenom!=null){
client.setPrenom(prenom);
}
if(age!=0){
client.setAge(age);
}
if(adresse!=null){
client.setAdresse(adresse);
}
serviceMetier.updateClient(client);
return SUCCESS;
}
public String delete(){
System.out.println("methode delete");
// if(codeClient>0){
serviceMetier.deleteClient(codeClient);
// codeClient=0;
// }
clientList=serviceMetier.getAllClient();
return SUCCESS;
}
}
this my struts.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="true" />
<constant name="struts.custom.i18n.resources" value="ApplicationResources" />
<package name="default" extends="struts-default" namespace="/">
<action name= "login" class="org.presentation.action.Account">
<result name="reussite" type="redirectAction">listClient</result>
<result name="echec">logging.jsp</result>
</action>
<action name="clientInit" class="org.presentation.action.ClientAction"
method="init">
<result name="success">index.jsp</result>
</action>
<action name= "addClient" class="org.presentation.action.ClientAction"
method="ajouter">
<result name="success">clientShow.jsp</result>
</action>
<action name="listClient" class="org.presentation.action.ClientAction"
method="afficher">
<result name="success">clientShow.jsp</result>
</action>
<action name="updateClient" class="org.presentation.action.ClientAction"
method="update">
<result name="success">client.jsp</result>
</action>
<action name="addCompteToClient" class="org.presentation.action.ClientAction"
method="addCompte">
<result name="success">client.jsp</result>
</action>
<action name="deleteClient" class="org.presentation.action.ClientAction"
method="delete">
<result name="success">clientShow.jsp</result>
</action>
</package>
</struts>
It's possible by using Struts2, jsp without javascript/jquery? if not how can I add jquery code in Struts2?

Categories