我有一个页脚模板,它有一个输入并提交电子邮件订阅。每页都使用页脚。订阅成功后,它将重定向回当前页面。但是,我发现我传递一个字符串值来指示当前页面是什么。为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。这是新设置。
main.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后,现在它正在工作。
subscribe.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 -->
spin()函数来自https://github.com/pshizzle/spin.coffee
答案 0 :(得分:2)
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()
答案 1 :(得分:0)
如何准备在隐藏字段中使用重定向地址?
footer
@(returnPath: String)
<form action="@routes.ApplicationController.saveSubscription()" method="post">
...
<input type="hidden" name="returnPath" value="@returnPath" />
</form>
page1
@main("Page 1") {
...
<!-- it's important to use toString at the end -->
@footer(routes.ApplicationController.page1.toString)
}
甚至更好:从当前path
request()
@main("Page 1") {
...
@footer(request.path)
}
controller
public static Result saveSubscription() {
...
flash("success", "success message");
return redirect(form().bindFromRequest().get("returnPath"));
}
顺便说一句:也许使用AJAX表单会更容易和更好?在这种情况下,您可以避免冗余重新加载(并查找对象并一次又一次地渲染整个模板等)