我正在尝试创建一个基于wicket的简单应用程序。我想在我的页面上放置一个Panel,指示用户是否已登录。 可悲的是,我无法找到问题的一个有效例子:如果有人登录,我希望我的Label显示用户名,如果没有用户登录,我希望显示一些消息。 我想我错过了什么。
代码如下:
面板:
public class Header extends Panel {
IModel<StringResourceModel> userNameModel = new LoadableDetachableModel<StringResourceModel>() {
@Override
protected StringResourceModel load() {
final User user = MySession.get().getAuthenticatedUser();
if (user==null){
return new StringResourceModel("username.noUser", Header.this , null);
}else{
return new StringResourceModel("username.user", Header.this, Model.of(user));
}
}
};
public Header(final String id) {
super(id);
}
@Override
protected void onInitialize() {
super.onInitialize();
add(new Label("username", userNameModel));
}
}
属性:
username.noUser=You are not logged in
username.user=Welcome, ${name}
标记:
<wicket:panel>
<wicket:label class="username" wicket:id="username" />
</wicket:panel>
渲染文字为
StringResourceModel [键:username.noUser,默认:空,则params:[]]
这种行为是有道理的。但是如何修复它以便呈现所需的文本?
答案 0 :(得分:0)
您应该从StringResourceModel#getObject()
方法返回字符串:
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
$(document).ready(function () {
var json = [{
"RATE_UPLOAD_DATE": "07/01/2015 8:17 AM CT",
"GROUPS": [
{
"NAME": "Conforming Fixed Rate Mortgage Purchase",
"PRODUCT": [
{
"DESCR": "30 Year Fixed Rate",
"RATE": "4.25",
"APR": "4.277",
"POINTS": "0",
"<PAYMENT_STREAM_URL>": "https://publicservices.mortgagewebcenter.com/PaymentStream.aspx?CobranderId=1152&CriteriaId=113444902&ResultId=58"
},
{
"DESCR": "20 Year Fixed Rate",
"RATE": "4.125",
"APR": "4.162",
"POINTS": "0",
"<PAYMENT_STREAM_URL>": "https://publicservices.mortgagewebcenter.com/PaymentStream.aspx?CobranderId=1152&CriteriaId=113444902&ResultId=52"
},
{
"DESCR": "15 Year Fixed Rate",
"RATE": "3.375",
"APR": "3.422",
"POINTS": "0",
"<PAYMENT_STREAM_URL>": "https://publicservices.mortgagewebcenter.com/PaymentStream.aspx?CobranderId=1152&CriteriaId=113444902&ResultId=45"
}
]
},
{
"NAME": "Conforming Adjustable Rate Mortgage Purchase",
"PRODUCT": [
{
"DESCR": "3/1 Fully Amortizing ARM",
"RATE": "3.625",
"APR": "3.166",
"POINTS": "0",
"<PAYMENT_STREAM_URL>": "https://publicservices.mortgagewebcenter.com/PaymentStream.aspx?CobranderId=1152&CriteriaId=113444913&ResultId=27"
},
{
"DESCR": "5/1 Fully Amortizing ARM",
"RATE": "3.25",
"APR": "3.113",
"POINTS": "0",
"<PAYMENT_STREAM_URL>": "https://publicservices.mortgagewebcenter.com/PaymentStream.aspx?CobranderId=1152&CriteriaId=113444913&ResultId=13"
},
{
"DESCR": "7/1 Fully Amortizing ARM",
"RATE": "3.5",
"APR": "3.258",
"POINTS": "0",
"<PAYMENT_STREAM_URL>": "https://publicservices.mortgagewebcenter.com/PaymentStream.aspx?CobranderId=1152&CriteriaId=113444913&ResultId=5"
},
{
"DESCR": "10/1 Fully Amortizing ARM",
"RATE": "3.75",
"APR": "3.487",
"POINTS": "0",
"<PAYMENT_STREAM_URL>": "https://publicservices.mortgagewebcenter.com/PaymentStream.aspx?CobranderId=1152&CriteriaId=113444913&ResultId=65"
}
]
},
{
"NAME": "Jumbo Fixed Rate Purchase",
"PRODUCT": [
{
"DESCR": "30 Year Fixed Rate",
"RATE": "4.25",
"APR": "4.265",
"POINTS": "0",
"<PAYMENT_STREAM_URL>": "https://publicservices.mortgagewebcenter.com/PaymentStream.aspx?CobranderId=1152&CriteriaId=113444921&ResultId=6"
},
{
"DESCR": "15 Year Fixed Rate",
"RATE": "3.5",
"APR": "3.526",
"POINTS": "0",
"<PAYMENT_STREAM_URL>": "https://publicservices.mortgagewebcenter.com/PaymentStream.aspx?CobranderId=1152&CriteriaId=113444921&ResultId=30"
}
]
},
{
"NAME": "Jumbo Adjustable Rate Mortgage Purchase",
"PRODUCT": [
{
"DESCR": "3/1 Fully Amortizing ARM",
"RATE": "2.75",
"APR": "2.959",
"POINTS": "0",
"<PAYMENT_STREAM_URL>": "https://publicservices.mortgagewebcenter.com/PaymentStream.aspx?CobranderId=1152&CriteriaId=113444935&ResultId=56"
},
{
"DESCR": "5/1 Fully Amortizing ARM",
"RATE": "3",
"APR": "3.014",
"POINTS": "0",
"<PAYMENT_STREAM_URL>": "https://publicservices.mortgagewebcenter.com/PaymentStream.aspx?CobranderId=1152&CriteriaId=113444935&ResultId=45"
},
{
"DESCR": "7/1 Fully Amortizing ARM",
"RATE": "3.25",
"APR": "3.13",
"POINTS": "0",
"<PAYMENT_STREAM_URL>": "https://publicservices.mortgagewebcenter.com/PaymentStream.aspx?CobranderId=1152&CriteriaId=113444935&ResultId=37"
},
{
"DESCR": "5/1 Interest Only ARM",
"RATE": "3.125",
"APR": "3.055",
"POINTS": "0",
"<PAYMENT_STREAM_URL>": "https://publicservices.mortgagewebcenter.com/PaymentStream.aspx?CobranderId=1152&CriteriaId=113444935&ResultId=13"
},
{
"DESCR": "10/1 Fully Amortizing ARM",
"RATE": "3.5",
"APR": "3.32",
"POINTS": "0",
"<PAYMENT_STREAM_URL>": "https://publicservices.mortgagewebcenter.com/PaymentStream.aspx?CobranderId=1152&CriteriaId=113444935&ResultId=22"
}
]
},
{
"NAME": "Conforming Fixed Rate Mortgage Refinance",
"PRODUCT": [
{
"DESCR": "30 Year Fixed Rate",
"RATE": "4.375",
"APR": "4.402",
"POINTS": "0",
"<PAYMENT_STREAM_URL>": "https://publicservices.mortgagewebcenter.com/PaymentStream.aspx?CobranderId=1152&CriteriaId=113444940&ResultId=62"
},
{
"DESCR": "20 Year Fixed Rate",
"RATE": "4.25",
"APR": "4.287",
"POINTS": "0",
"<PAYMENT_STREAM_URL>": "https://publicservices.mortgagewebcenter.com/PaymentStream.aspx?CobranderId=1152&CriteriaId=113444940&ResultId=55"
},
{
"DESCR": "15 Year Fixed Rate",
"RATE": "3.5",
"APR": "3.547",
"POINTS": "0",
"<PAYMENT_STREAM_URL>": "https://publicservices.mortgagewebcenter.com/PaymentStream.aspx?CobranderId=1152&CriteriaId=113444940&ResultId=47"
}
]
},
{
"NAME": "Conforming Adjustable Rate Mortgage Refinance",
"PRODUCT": [
{
"DESCR": "3/1 Fully Amortizing ARM",
"RATE": "3.75",
"APR": "3.194",
"POINTS": "0",
"<PAYMENT_STREAM_URL>": "https://publicservices.mortgagewebcenter.com/PaymentStream.aspx?CobranderId=1152&CriteriaId=113444949&ResultId=30"
},
{
"DESCR": "5/1 Fully Amortizing ARM",
"RATE": "3.375",
"APR": "3.157",
"POINTS": "0",
"<PAYMENT_STREAM_URL>": "https://publicservices.mortgagewebcenter.com/PaymentStream.aspx?CobranderId=1152&CriteriaId=113444949&ResultId=15"
},
{
"DESCR": "7/1 Fully Amortizing ARM",
"RATE": "3.625",
"APR": "3.317",
"POINTS": "0",
"<PAYMENT_STREAM_URL>": "https://publicservices.mortgagewebcenter.com/PaymentStream.aspx?CobranderId=1152&CriteriaId=113444949&ResultId=6"
},
{
"DESCR": "10/1 Fully Amortizing ARM",
"RATE": "3.875",
"APR": "3.566",
"POINTS": "0",
"<PAYMENT_STREAM_URL>": "https://publicservices.mortgagewebcenter.com/PaymentStream.aspx?CobranderId=1152&CriteriaId=113444949&ResultId=70"
}
]
},
{
"NAME": "Jumbo Fixed Rate Refinance",
"PRODUCT": [
{
"DESCR": "30 Year Fixed Rate",
"RATE": "4.375",
"APR": "4.39",
"POINTS": "0",
"<PAYMENT_STREAM_URL>": "https://publicservices.mortgagewebcenter.com/PaymentStream.aspx?CobranderId=1152&CriteriaId=113444962&ResultId=6"
},
{
"DESCR": "15 Year Fixed Rate",
"RATE": "3.625",
"APR": "3.651",
"POINTS": "0",
"<PAYMENT_STREAM_URL>": "https://publicservices.mortgagewebcenter.com/PaymentStream.aspx?CobranderId=1152&CriteriaId=113444962&ResultId=30"
}
]
},
{
"NAME": "Jumbo Adjustable Rate Mortgage Refinance",
"PRODUCT": [
{
"DESCR": "3/1 Fully Amortizing ARM",
"RATE": "2.875",
"APR": "2.986",
"POINTS": "0",
"<PAYMENT_STREAM_URL>": "https://publicservices.mortgagewebcenter.com/PaymentStream.aspx?CobranderId=1152&CriteriaId=113444969&ResultId=56"
},
{
"DESCR": "5/1 Fully Amortizing ARM",
"RATE": "3.125",
"APR": "3.058",
"POINTS": "0",
"<PAYMENT_STREAM_URL>": "https://publicservices.mortgagewebcenter.com/PaymentStream.aspx?CobranderId=1152&CriteriaId=113444969&ResultId=45"
},
{
"DESCR": "7/1 Fully Amortizing ARM",
"RATE": "3.375",
"APR": "3.188",
"POINTS": "0",
"<PAYMENT_STREAM_URL>": "https://publicservices.mortgagewebcenter.com/PaymentStream.aspx?CobranderId=1152&CriteriaId=113444969&ResultId=37"
},
{
"DESCR": "5/1 Interest Only ARM",
"RATE": "3.25",
"APR": "3.097",
"POINTS": "0",
"<PAYMENT_STREAM_URL>": "https://publicservices.mortgagewebcenter.com/PaymentStream.aspx?CobranderId=1152&CriteriaId=113444969&ResultId=13"
},
{
"DESCR": "10/1 Fully Amortizing ARM",
"RATE": "3.625",
"APR": "3.397",
"POINTS": "0",
"<PAYMENT_STREAM_URL>": "https://publicservices.mortgagewebcenter.com/PaymentStream.aspx?CobranderId=1152&CriteriaId=113444969&ResultId=22"
}
]
}
]
}];
var tr;
for (var i = 0; i < json.length; i++) {
tr = $('<tr/>');
tr.append("<td>" + json[i].DESCR + "</td>");
tr.append("<td>" + json[i].RATE + "</td>");
tr.append("<td>" + json[i].APR + "</td>");
tr.append("<td>" + json[i].POINTS + "</td>");
$('table').append(tr);
}
});
</script>
</head>
<body>
<table>
<tr>
<th>PRODUCT</th>
<th>RATE</th>
<th>APR*</th>
<th>POINTS</th>
</tr>
</table>
</body>
</html>
请注意对ng-app="myApp"
的调用。
答案 1 :(得分:0)
我对wiki进行了深入研究,并选择了以下方法:
public class Header extends Panel {
private static class UserState {
private final String userLoggedIn;
private final User user;
public UserState(final User user) {
super();
userLoggedIn = user==null?"noUser":"user";
this.user = user;
}
public String getUserLoggedIn() {
return userLoggedIn;
}
public User getUser() {
return user;
}
}
IModel<UserState> userModel = new LoadableDetachableModel<UserState>() {
@Override
protected UserState load() {
return new UserState(HouseSession.get()
.getAuthenticatedUser());
}
};
public Header(final String id) {
super(id);
}
@Override
protected void onInitialize() {
super.onInitialize();
final StringResourceModel messageModel = new StringResourceModel("username.${userLoggedIn}", Header.this, userModel);
add(new Label("username", messageModel));
}
@Override
protected void onDetach() {
super.onDetach();
userModel.detach();
}
}