我有以下Java bean类,使用Jackson转换为JSON.
public class Thing {
public String name;
@JsonRawValue
public Map content = new HashMap();
}
Run Code Online (Sandbox Code Playgroud)
content是一个地图,其值将是来自其他来源的原始JSON.例如:
String jsonFromElsewhere = "{ \"foo\": \"bar\" }";
Thing t = new Thing();
t.name = "test";
t.content.put("1", jsonFromElsewhere);
Run Code Online (Sandbox Code Playgroud)
所需的生成JSON是:
{"name":"test","content":{"1":{ "foo": "bar" }}}
Run Code Online (Sandbox Code Playgroud)
但是使用@JsonRawValue结果:
{"name":"test","content":{1={ "foo": "bar" }}}
Run Code Online (Sandbox Code Playgroud)
我需要的是一种@JsonRawValue仅为Map的值指定的方法.这可能与杰克逊有关吗?
我想从Java打开一个链接我试过这个
public static void main(String[] args) {
try {
//Set your page url in this string. For eg, I m using URL for Google Search engine
String url = "http://myurl.com?id=xx";
java.awt.Desktop.getDesktop().browse(java.net.URI.create(url));
}
catch (java.io.IOException e) {
System.out.println(e.getMessage());
}
}
Run Code Online (Sandbox Code Playgroud)
它工作正常,但问题是查询字符串在该URL中.我不想将它作为查询字符串传递,因为它是一个密钥.它应该作为隐藏传递给网页请求.我怎样才能做到这一点?
这是否可以以确定的方式运行多线程Java应用程序?我的意思是在我的应用程序的两个不同运行中始终使用相同的线程切换.
原因是在每次运行中以完全相同的条件运行模拟.
类似的情况是当使用随机数生成器获得总是相同的"随机"序列时,给出一些任意种子.
我只是注意到 java.beans.Introspector getBeanInfo 没有获取任何超级接口的属性。例子:
public interface Person {
String getName();
}
public interface Employee extends Person {
int getSalary();
}
Run Code Online (Sandbox Code Playgroud)
尽管 name 是从 Person 继承的,但对 Employee 的内省只会产生薪水。
为什么是这样?我宁愿不必使用反射来获取所有吸气剂。
是否可以通过循环内的代码访问JSTL的forEach变量?
<c:forEach items="${elements}" var="element">
<% element.someMethod(); %>
</c:forEach>
Run Code Online (Sandbox Code Playgroud) 我正在使用Spring,但这个问题适用于所有JSP控制器类型的设计.
JSP页面引用由相应控制器填充的数据(使用标记).我的问题是,在JSP或控制器中执行格式化的适当位置在哪里?
到目前为止,我一直在通过在控制器中格式化数据来准备数据.
public class ViewPersonController extends org.springframework.web.servlet.mvc.AbstractController
{
private static final Format MY_DATE_FORMAT = new SimpleDateFormat(...);
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response)
{
Person person = get person from backing service layer or database
Map properties = new HashMap();
// No formatting required, name is a String
properties.put("name", person.getName());
// getBirthDate() returns Date and is formatted by a Format
properties.put("birthDate", MY_DATE_FORMAT.format(person.getBirthDate()));
// latitude and longitude are separate fields in Person, but in the UI it's one field
properties.put("location", person.getLatitude() …Run Code Online (Sandbox Code Playgroud) Java 和/或 Spring 有属性的概念吗?我有一堆领域模型,每个领域模型都有几个属性。例子:
public class Person {
private String name;
private Date dateOfBirth;
private float height;
private float weight;
// getters and setters not shown
}
Run Code Online (Sandbox Code Playgroud)
当显示一个人时,属性名称在 JSP 中被硬编码。
Name: ${person.name}<br>
Date of birth: ${person.dateOfBirth}<br>
Height: ${person.height}<br>
Weight: ${person.weight}<br>
Run Code Online (Sandbox Code Playgroud)
此外,可能有几个不同的页面显示一个人。请注意,出生日期是java.util.Date,因此 JSP 或控制器将使用java.text.SimpleDateFormat它来格式化它。身高和体重是数字,它们可能有自己java.util.Format的格式用于格式化。
我正在寻找一种属性查找机制。每个属性(姓名、出生日期、身高等)都有显示名称、格式和描述(用于帮助或工具提示)。属性的属性将在某个配置文件中定义。显示属性时,将通过属性机制查找显示名称和格式。这也将解决本地化问题。
我的问题是是否已经为 Java 实现了这样的东西。
我使用以下行来对LinkedHashMap进行排序,但并非所有项都已排序,有什么不对吗?
LinkedHashMap<String,PatternData> statisticsMap;
// fill in the map ...
LinkedHashMap<String,PatternData> sortedStatisticsMap=new LinkedHashMap<String,PatternData>(); // Sort it by patternData's average
ArrayList<PatternData> statisticsMapValues=new ArrayList<PatternData>(statisticsMap.values());
Collections.sort(statisticsMapValues,Collections.reverseOrder()); // Sorting it (in reverse order)
patternData last_i=null;
for (PatternData i : statisticsMapValues) // Now, for each value
{
if (last_i==i) continue; // Without dublicates
last_i=i;
for (String s : statisticsMap.keySet()) // Get all hash keys
if (statisticsMap.get(s)==i) // Which have this value
{
sortedStatisticsMap.put(s,i);
}
}
class PatternData implements Comparable<PatternData>
{
float sum=0,average;
int totalCount=0;
Vector<String> records=new …Run Code Online (Sandbox Code Playgroud) 在MVC(例如JSP和Spring)中,在控制器中查看相关代码是不好的做法吗?
在我的例子中,控制器做了一些工作,然后将结果交给视图(JSP).在状态消息的情况下,我可以将整个消息文本传递给视图,或者传递一个键,让JSP将其映射到消息文本.
例:
控制器中生成的消息
弹簧控制器:
protected ModelAndView onSubmit(...) {
Map map = new HashMap();
// Controller processing
if (...)
map.put("status", "Case 1 status message");
else
map.put("status", "Case 2 status message");
return new ModelAndView("viewPage", map);
}
Run Code Online (Sandbox Code Playgroud)
JSP:
{$status}
Run Code Online (Sandbox Code Playgroud)
在视图中生成的消息
弹簧控制器:
protected ModelAndView onSubmit(...) {
Map map = new HashMap();
// Controller processing
if (...)
map.put("status", "case1");
else
map.put("status", "case2");
return new ModelAndView("viewPage", map);
}
Run Code Online (Sandbox Code Playgroud)
JSP:
<c:choose>
<c:when test="{$status eq 'case1'}">Case 1 status message</c:when>
<c:when test="{$status eq 'case2'}">Case 2 status message</c:when>
</c:choose> …Run Code Online (Sandbox Code Playgroud) 我有两个域类 - 人有很多书.如果我创建一个Person,调用save(),然后创建+添加Books到Person,一切都持久化到数据库.如果我创建了一个Person,那么创建+添加Books,然后是save(),不会有任何持久性.
示例代码:
class Person {
...
static hasMany = [books: Book]
}
class Book {
...
static belongsTo = [person: Person]
}
Run Code Online (Sandbox Code Playgroud)
Works,保存到数据库:
def person = new Person(...).save()
def book = new Book(...)
person.addToBooks(book)
Run Code Online (Sandbox Code Playgroud)
不起作用,不保存到数据库:
def person = new Person(...)
def book = new Book(...)
person.addToBooks(book)
person.save()
Run Code Online (Sandbox Code Playgroud)
为什么是这样?
我正在从Groovy控制台运行它.我试过打电话ctx.sessionFactory.currentSession.clear()哪个没用.
已解决:我附在人物上的书中有错误.通过电话person.hasErrors(),getErrors()我能够看到我的书未通过验证.这是从Grails控制台运行的,因此没有任何验证错误消息.
java ×9
jsp ×3
jstl ×3
spring ×3
grails ×1
jackson ×1
jakarta-ee ×1
java-ee ×1
javabeans ×1
json ×1
jsp-tags ×1
properties ×1
reflection ×1
sorting ×1
web ×1