首页 \ 问答 \ 如何以编程方式获取部署地址?(How to get deployed address programmatically?)

如何以编程方式获取部署地址?(How to get deployed address programmatically?)

我有以下应用程序URI结构:

ip:port/applicationName/someAction

如何以编程方式获取URI的一部分:

ip:port/applicationName/

我试过

servletContext.getContext()

但它只返回applicationName

PS

这很有效

String applicationBasePath = request.getRequestURL().substring(0,request.getRequestURL().indexOf("/",request.getRequestURL().indexOf("/")+2));

和这个:

request.getRequestURL().substring(0, request.getRequestURL().indexOf(request.getRequestURI()))

但我不喜欢它。


I have following application URI structure:

ip:port/applicationName/someAction

How can I get following part of URI programmatically:

ip:port/applicationName/

I tryed

servletContext.getContext()

but it returns only applicationName.

P.S.

this works

String applicationBasePath = request.getRequestURL().substring(0,request.getRequestURL().indexOf("/",request.getRequestURL().indexOf("/")+2));

and this:

request.getRequestURL().substring(0, request.getRequestURL().indexOf(request.getRequestURI()))

But I don't like it.


原文:https://stackoverflow.com/questions/27756973
更新时间:2023-06-01 14:06

最满意答案

您正在寻找的功能是ports 。 将您的模块声明为port module (即,使用port module替换文件顶部的单词port module ),并声明一个传出端口:

port alert : String -> Cmd msg

这定义了一个Elm函数alert ,它采用String并生成一个命令。 (注意它是一个Cmd msg ,小写的msg ,意味着它实际上不会产生大写-M Msg ; msg是小写的事实表明它是一个类型变量,并且alert "blah"将是可返回的从任何更新函数,无论具体的Msg类型。)

在JavaScript中,你会写这样的东西:

app.ports.alert.subscribe(function(str) {
    alert(str);
});

该代码“订阅”由Elm代码发送到alert端口的消息 - 不要将此与Elm subscriptions混淆,Elm subscriptions监听发送 Elm的消息。

现在,在Elm中,您可以创建alert "Hi there"等命令。 执行时,该命令将调用您在Javascript中定义的函数,从而导致警报显示"Hi there"文本。

请注意,您不能声明,例如, alertTime : Cmd msg没有参数。 您可以声明alertTime : () -> Cmd msg ,并通过从更新函数返回alertTime ()来使用它。 我相信如果您的Javascript函数接受输入,这将发送值为[]的Javascript。


The feature you're looking for is ports. Declare your module to be a port module (that is, replace the word module at the top of your file with port module), and declare an outgoing port:

port alert : String -> Cmd msg

This defines an Elm function alert which takes a String and produces a command. (Note that it is a Cmd msg, lowercase msg, meaning that it doesn't actually ever result in a capital-M Msg; the fact that msg is lowercase indicates that it is a type variable, and alert "blah" will be returnable from any update function, no matter the concrete Msg type.)

In JavaScript, you'd write something like this:

app.ports.alert.subscribe(function(str) {
    alert(str);
});

That code "subscribes" to messages being sent by the Elm code to the alert port -- don't confuse this with Elm subscriptions, which listen for messages being sent to Elm.

Now, in Elm, you can create commands like alert "Hi there". When executed, that command will call the function you defined in Javascript, causing an alert to appear with the text "Hi there".

Note that you cannot declare, e.g., alertTime : Cmd msg with no arguments. You can declare alertTime : () -> Cmd msg, and use it by returning alertTime () from your update function. I believe this will send Javascript the value [], if your Javascript function accepts input.

相关问答

更多
  • 使用json模块(或Python 2.6之前的simplejson )。 你只需要记住两个函数:json.dumps和json.loads。 >>> import json >>> json.dumps(['foo', {'bar': ('baz', None, 1.0, 2)}]) '["foo", {"bar": ["baz", null, 1.0, 2]}]' >>> json.loads('["foo", {"bar": ["baz", null, 1.0, 2]}]') [u'foo', {u'b ...
  • 不.HTTP是字节流协议(*); 任何下降的东西都必须打包成字节。 如果你愿意的话,你当然可以使用更紧凑的二进制表示值,但是你的PHP编码和JS解码会更加困难。 无论如何,对于小数字的常见情况,文本表示往往非常有效。 您的示例1.2345实际上比一个字符串(6个字节)小,而不是双精度浮点数(8个字节)。 JSON的发明正是为了允许通过HTTP连接传输非字符串类型。 它就像你将要获得的一样无缝。 有没有什么好理由关心PHP float和JavaScript Number之间有一个serialise-> str ...
  • 您正在寻找的功能是ports 。 将您的模块声明为port module (即,使用port module替换文件顶部的单词port module ),并声明一个传出端口: port alert : String -> Cmd msg 这定义了一个Elm函数alert ,它采用String并生成一个命令。 (注意它是一个Cmd msg ,小写的msg ,意味着它实际上不会产生大写-M Msg ; msg是小写的事实表明它是一个类型变量,并且alert "blah"将是可返回的从任何更新函数,无论具体的Ms ...
  • 看到你标记这个JavaEE我建议将JavaScript与Servlets结合使用(3.0) 您可以向任何服务器servlet映射进行POST,该映射可以使用其doPost()方法读取请求。 对于一些代码示例,请参阅BalusC 使用javascript调用servlet的解释 Seeing as you tagged this JavaEE I would suggest using JavaScript in combination with Servlets(3.0) You would able to ...
  • 看起来你对定义和技术有点困惑,所以让我首先澄清一些缺陷(粗略的描述,不完全是): JSON:一种将数据结构(如数组)打包成字符串并检索数据的格式。 它是提交实际数据最常用的格式之一,但它本身不能传输数据:它只是数据本身 AJAX:在不重新加载整个页面的情况下向服务器发送请求的主流技术。 如果您通过访问页面发送相同的请求,AJAX将通过HTTP协议工作并发送与发送时相同的标头(例外:对AJAX特殊的标头,取决于库等)。 AJAX本身就是提交数据的技术,但不是数据本身。 但是,它可以,例如,提交以通常的URL编 ...
  • 您忘记了name属性,而您使用的onchange方法只是打开一个新窗口,其中param1作为url。