我将从一个简单的例子开始.你有一个Spring启动应用程序,它CommandLineRunner
在初始化时运行一个类.
// MyCommandLineRunner.java
public class MyCommandLineRunner implements CommandLineRunner {
private final Log logger = LogFactory.getLog(getClass());
@Autowired //IntelliJ Warning
private DataSource ds;
@Override
public void run(String... args) throws Exception {
logger.info("DataSource: " + ds.toString());
}
}
// Application.java
@SpringBootApplication
public class Application {
public static void main(String... args) {
SpringApplication.run(Application.class, args);
}
@Bean
public MyCommandLineRunner schedulerRunner() {
return new MyCommandLineRunner();
}
}
Run Code Online (Sandbox Code Playgroud)
现在,像这样,这个工作,一切都很好.但是,IntelliJ会在哪里发出警告@Autowired
(我在评论中标记了哪里)
Spring团队建议:始终在bean中使用基于构造函数的依赖注入.始终使用断言来强制依赖.
现在如果我遵循这个,我有一个基于构造函数的依赖注入
@Autowired
public MyCommandLineRunner(DataSource ds) { ... }
Run Code Online (Sandbox Code Playgroud)
这也意味着我必须编辑Application.java
,因为构造函数需要一个参数.在Application.java …
所以在JS中获取我需要的对象,我做了:
$('.combine-payment-input').each(function (index, value) {
if (parseFloat(value.value) > 0) {
if (methodOfPayment == -1) {
methodOfPayment = value.dataset.method;
}
else {
methodOfPayment = 0;
}
vmopl.push({
id: value.dataset.method,
name: $('label[for="' + value.id + '"]').html(),
inUse: 'True',
ammount: value.value
});
}
});
Run Code Online (Sandbox Code Playgroud)
如果我console.log
vmopl
最终,我会得到类似的东西
[Object { id="2", name="Card", inUse="True", ammount="500"},
Object { id="1", name="Cash", inUse="True", ammount="250"}]
Run Code Online (Sandbox Code Playgroud)
现在,如果我尝试将此发送给AJAX,请使用
$.get('/reports/savebill/' + methodOfPayment + '?vmop=' + JSON.stringify(vmopl), function (data) {
if (data == 'True') {
location.href = '/order/neworder/';
} else {
alert("Unsuccessful!"); …
Run Code Online (Sandbox Code Playgroud) 注意:这已经可以正常工作,但我试图理解为什么它以这种方式工作,而不是另一种方式.
现在,如果您单击"Napred"按钮,应删除这些图像(以及其他内容),我最初使用这些图像:
foreach(Control ctrl in Controls)
if(ctrl is PictureBox) ctrl.Dispose();
Run Code Online (Sandbox Code Playgroud)
要么
for(int i = 0; i < Controls.Count; i++)
if(Controls[i] is PictureBox) Controls[i].Dispose();
Run Code Online (Sandbox Code Playgroud)
现在如果我运行这个,我得到:
但是,如果我只是将for
声明改为向后发送,那么它有效吗?
for(int i = Controls.Count - 1; i >= 0; i--)
if(Controls[i] is PictureBox) Controls[i].Dispose();
Run Code Online (Sandbox Code Playgroud)
(我不会上传另一张图片,但会删除所有元素(我只留下最后的按钮))
有人可以告诉我为什么一个有效,而不是另一个有效吗?
编辑:我在Windows 10上使用VS2015社区版,如果这是一个调试错误(?)
所以我有一个查询:
cmd.CommandText = @"INSERT INTO [dbo].[bill](bruto, waiterid, reversalid, number, ddate, tableid, total, printed, posplace, guestid, numberofguests, closedtime, methodofpaymentid, realdate) " +
"VALUES (@bruto, @waiterid, 0, 0, @ddate, 1, @total, 'True', 0, 0, 0, @closedtime, @methodofpaymentid, @realtime) " +
"SELECT id";
//+ lines of cmd.Parameter.AddWithValue(...,...);
Run Code Online (Sandbox Code Playgroud)
但是一旦我尝试执行查询:
int newId = Convert.ToInt64(cmd.ExecuteScalar());
Run Code Online (Sandbox Code Playgroud)
我得到的id没有被识别(即使它确实存在).
如果我尝试更改SELECT
为[dbo].[bill].id
,我会收到错误
无法绑定多部分标识符"dbo.bill.id".
我也尝试过它SELECT MAX(id)
(因为这是最接近标量的东西),但是又一次,我得到了不明的错误.
谢谢阅读.
这是html:
<div class="btn btn-stock-report" id="superman">
Superman <input type="checkbox" name="superman" />
</div>
Run Code Online (Sandbox Code Playgroud)
这是JQuery:
$('#superman').on('click', function () {
if ($(this).children('input:checkbox:first').is(':checked')) {
superman = true;
$(this).children('input:checkbox:first').removeAttr('checked');
}
else {
superman = false;
$(this).children('input:checkbox:first').attr('checked', 'checked');
}
console.log("superman: " + superman);
});
Run Code Online (Sandbox Code Playgroud)
我正在尝试实现它只是更改子复选框的状态并更改superman
变量的值,但由于某种原因,它总是superman: false
在控制台日志中打印出来.即使我手动选中复选框并单击div,即使现在选中了复选框,它也会报告superman: false
可能是什么导致了这个?
c# ×3
javascript ×2
jquery ×2
ajax ×1
asp.net-mvc ×1
autowired ×1
coding-style ×1
for-loop ×1
html ×1
java ×1
parent-child ×1
picturebox ×1
spring ×1
spring-boot ×1
sql ×1
sql-server ×1
winforms ×1