gpt4 book ai didi

java - 玩2.0模板重用页脚模板的正确方法

转载 作者:行者123 更新时间:2023-11-30 04:41:59 25 4
gpt4 key购买 nike

我有一个页脚模板,其中包含用于电子邮件订阅的输入和提交。页脚由每个页面使用。订阅成功后,会重定向回当前页面。但是,我发现我正在传递一个字符串值来指示当前页面是什么。为 Play 2.0 应用程序提供页脚模板的最佳方式是什么?

footer.scala.html

@(page: String)
<div id="footer">
<div class="input-append">
<form action="@routes.ApplicationController.saveSubscription(page)"
method="post">
<input type="text" name="emailAddress" placeholder="Your Email" />
<input class="btn" type="submit" value="Subscribe" />
</form>
</div> <!-- /input-append -->
</div> <!-- /footer -->

ApplicationController.java

public class ApplicationController extends Controller {
public static Result saveSubscription(String page) {
..........
flash("success", "success message");
if (page.equals("page1")) {
return redirect(routes.ApplicationController.page1());
} else if (page.equals("page2")) {
return redirect(routes.ApplicationController.page2());
}
}
}

page1.scala.html

@main("Page 1") {
<div>
<p>page 1</p>
</div>
@footer("page1")
}

page2.scala.html

@main("Page 2") {
<div>
<p>page 2</p>
</div>
@footer("page2")
}

编辑 1

我关注@virtualeyes,但似乎 subscribe.js 从未被调用过。这是新设置。

ma​​in.scala.html

<html>
<head>
<script type="text/javascript" src="@routes.Assets.at("javascripts/jquery.min.js")"></script>
<script type="text/javascript" src="@routes.Assets.at("javascripts/vendor/jquery.validate.min.js")"></script>
<script src="@routes.Assets.at("javascripts/main.js")" type="text/javascript"></script>
<script src="@routes.Assets.at("javascripts/subscribe.js")" type="text/javascript"></script>

</head>
<body>
@footer()
</body>
</html>

footer.scala.html

<div id="footer">
<div class="input-append">
<form id="_form" action="@routes.ApplicationController.simpleSubscription()">
<input type="text" name="emailAddress" placeholder="Your Email" />
<input id="_process" class="btn" type="submit" value="Subscribe" />
</form>
</div> <!-- /input-append -->
</div> <!-- /footer -->

路线

POST    /subscribe  controllers.ApplicationController.simpleSubscription()

现在我得到了他的错误:未找到操作对于请求“GET/subscribe?emailAddress=fdsaf%40rte.com”

我不确定是否是因为 method="post"被删除了。如果我把它放回去,那么结果将返回,但会重定向到/subscribe 页面。我还在 subscribe.js 设置了一个断点,但它似乎根本没有被调用。

编辑 2 - 工作

在我对 subscribe.coffee 进行了一些更改并删除了 main.coffee 之后,现在它可以工作了。

订阅.coffee

$('#_process').click (e) ->
e.preventDefault()

isValid = $('#_form').validate().form()
if isValid
$('#_process').spin()
$.ajax
type: "POST"
url: $('#_form').attr('action')
data: $('#_form').serialize()
success: (data) ->
$('#_status > div').removeClass('alert-error').addClass('alert-success')
$('#_status > div').html( data )
$('#_status').fadeIn()
fade = () -> $('#_status').fadeOut('slow')
setTimeout fade, 2000
$('#_process').spin('stop')

error: (data) ->
$('#_status > div').removeClass('alert-success').addClass('alert-error')
$('#_status > div').html( data.responseText )
$('#_status').fadeIn()
fade = () -> $('#_status').fadeOut('slow')
setTimeout fade, 2000
$('#_process').spin('stop')

complete: () -> $('#_process').spin('stop')

footer.scala.html

 <div id="_status">
<div class="alert alert-error"></div>
</div>
<div class="input-append">
<form id="_form" action="@routes.ApplicationController.simpleSubscription">
<input type="text" name="emailAddress" placeholder="Your Email" />
<input id="_process" class="btn" type="submit" value="Subscribe" />
</form>
<div id="_spin"></div>
</div> <!-- /input-append -->

The spin() function is from https://github.com/pshizzle/spin.coffee

最佳答案

AJAX 就是出路™

//footer.scala.html
<div id="footer">
<div class="input-append">
<form id="_form" action="@routes.ApplicationController.saveSubscription">
<input type="text" name="emailAddress" placeholder="Your Email" />
<input id="_process" class="btn" type="submit" value="Subscribe" />
<img id="spinner" src="/assets/img/loader.gif" alt="loading..." />
</form>
</div>
</div>

<div id="status">
<div class="alert alert-error"></div>
</div>
<style type="text/css">
#spinner, #status { display: none; }
</style>

//ApplicationController.java
public class ApplicationController extends Controller {
public static Result saveSubscription() {
// save subscription
...
// pseudo code
if(success)
Ok( i18n("subscription success").toJson() );
else
Conflict( i18n("subscription fail").toJson() );
}
}

//main.coffee
jQuery ->
$.ajaxSetup
type: "POST"
cache: false
dataType: "json"

# prevent form submit on keypress
$('form').find('input').keypress (e) -> e.preventDefault() if(e.which == 13)

jParse = (data) ->
try jQuery.parseJSON(data)
catch e
data

jText = (data) -> jParse(data.responseText)

toSuccess = (msg) ->
$('#status > div').removeClass('alert-error').addClass('alert-success')
$('#status > div').html( jParse(msg) )
$('#status').fadeIn()

toFail = (data) ->
$('#status > div').html( jText(data) )
$('#status').fadeIn()

//subscribe.coffee
jQuery ->
$('#_process').click (e) ->
e.preventDefault()

isValid = $('#_form').validate().form() // assumes jQuery validation plugin
if isValid
$('#spinner').show()
$.ajax
data: $('#_form').serialize()
success: (msg) ->
toSuccess(msg)
fade = () -> $('#status').fadeOut('slow')
setTimeout fade, 2000

error: (data) -> toFail(data)

complete: () -> $('#spinner').hide()

关于java - 玩2.0模板重用页脚模板的正确方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12061144/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com