什么是最好的方法 ?
只需循环并放置键和零,或者是另一个更优雅或现有的库方法.如果它有任何有用的功能,我也使用谷歌的guava java库?
想要检查是否有类似于列表的复制方法或Map的putAll方法,但仅用于键.
我使用的是Spring,这里是一个控制器:
@Controller
public class PersonController {
@Resource(name="PersonService")
private PersonService personService;
@RequestMapping(value = "/Person", method = RequestMethod.GET)
public String getPersons(Model model) {
// Retrieve all persons by delegating the call to PersonService
List<Person> persons = personService.getAll();
// Attach persons to the Model
model.addAttribute("persons", persons);
//then return to view jsp
}
Run Code Online (Sandbox Code Playgroud)
这是一项服务:
@Service("personService")
@Transactional
public class PersonService {
public List<Person> getAll() {
//do whatever
}
}
Run Code Online (Sandbox Code Playgroud)
但是,要正确使用DI,我应该更改控制器以使用接口(?),如下所示:
@Controller
public class PersonController {
@Resource(name="personService")
private IPersonService personService; //Now an interface
}
Run Code Online (Sandbox Code Playgroud)
例如,这将允许我使用两个服务,一个测试,一个实时.我可以通过添加/删除服务上的注释来改变:
@Service("personService") …Run Code Online (Sandbox Code Playgroud) 我试图使用spring security来记住我的功能
<bean id="userService" class="mypath.service.UserDetailsServiceImpl" />
<security:http auto-config='true'>
<security:intercept-url pattern="/Login" filters="none" />
<security:form-login login-page='/Login' authentication-failure-url="/Login?login_error=1"/>
<security:remember-me data-source-ref="dataSource" />
</security:http>
Run Code Online (Sandbox Code Playgroud)
但是,我似乎在remember-me元素中的某处指定了userService?我该怎么做呢.
我在启动tomcat时遇到的错误是
More than one UserDetailsService registered.
Please use a specific Id reference in <remember-me/> <openid-login/> or <x509 /> elements.
Run Code Online (Sandbox Code Playgroud) 这是我的json字符串,我在java中访问:
json =
[
{"id":"123456","Data":"skill2","dmlcrud":false},
{"id":"123456","Data":"skill3","dmlcrud":true},
{"id":"123456","Data":"skill14","dmlcrud":true},
{"id":"123321","Data":"skill1","dmlcrud":false},
{"id":"123321","Data":"skill14","dmlcrud":false}
]
Run Code Online (Sandbox Code Playgroud)
我现在想把它放在一个集合中,理想上/理论上我想做:
List<Person> personList = new Gson().fromJson(json, Person.class);
Run Code Online (Sandbox Code Playgroud)
和personList.size()将= 5.然后我将遍历personList并执行我的相关操作.
但是,我的理解是我需要创建一个容器类,它本身包含人员列表?所以不是为了简洁而删除公共getter/setter,也可能是语法错误.
Class Person {
private integer id;
private String Data;
private Boolean dmlCrud ;
}
Run Code Online (Sandbox Code Playgroud)
我真的需要类似的东西吗?
Class container{
List<Person> personList;
static class Person {
private integer id;
private String Data;
private Boolean dmlCrud ;
}
}
Run Code Online (Sandbox Code Playgroud)
然而,我需要改变javascript json是一些不同的东西吗?这似乎很有问题,因为我使用JSON.stringifier从javascript数组创建json字符串.
任何帮助感激不尽.
编辑
我使用的解决方案是添加
public List<Person> personList;
Run Code Online (Sandbox Code Playgroud)
到person类并改变它的json对象
{ "personList" :
[
{"id":"123456","Data":"skill2","dmlcrud":false},
{"id":"123456","Data":"skill3","dmlcrud":true},
{"id":"123456","Data":"skill14","dmlcrud":true},
{"id":"123321","Data":"skill1","dmlcrud":false},
{"id":"123321","Data":"skill14","dmlcrud":false}
]
}
Run Code Online (Sandbox Code Playgroud)
那么gson调用就可以了
Person person = new …Run Code Online (Sandbox Code Playgroud) 我的最新规范是允许创建招聘广告;所以我想要html textarea中的基本富文本编辑功能。据我所知,似乎没有明显的赢家?到目前为止,我发现:
哪个获得投票,还是我错过了获胜者?我已经在使用jquery和jquery-ui,因此任何与它们平滑集成的东西都将特别好。
是否可以通过CDN获得任何信息,或者我必须自己下载并提供该库?
在这里得到答案后,我试图在我的网站上允许一个公共页面,但它似乎不起作用.当它重定向到登录页面.但是,如果我更改为filters = none它可以工作,并且页面是可见的,没有重定向.
我的appContext.xml看起来像这样(当它不起作用时),/MyPath相关部分:
<security:intercept-url pattern="/**" access="ROLE_USER" />
<security:intercept-url pattern="/Admin/**" access="ROLE_ADMIN" />
<security:intercept-url pattern="/MyPath/**" access="IS_AUTHENTICATED_ANONYMOUSLY, IS_AUTHENTICATED_FULLY, IS_AUTHENTICATED_REMEMBERED" />
Run Code Online (Sandbox Code Playgroud)
这有效,但我需要安全功能,所以不能使用它:
<security:intercept-url pattern="/**" access="ROLE_USER" />
<security:intercept-url pattern="/Admin/**" access="ROLE_ADMIN" />
<security:intercept-url pattern="/MyPath/**" filters="none" />
Run Code Online (Sandbox Code Playgroud) 这是我的表格:
<form:form modelAttribute="fooDTO">
fooCountry:
<form:select path="country">
<form:options items="${countries}" itemLabel="shortName" itemValue="id"/>
</form:select>
Run Code Online (Sandbox Code Playgroud)
这是支持pojo:
public class FooDTO
{
private Country country;
//getters and setters present
}
Run Code Online (Sandbox Code Playgroud)
所选选项默认为fooDTO中的国家/地区值,这很好.但是提交表单时绑定失败了 - 我得到了上述错误,我是否必须在活页夹中注册自定义编辑器,还是有更简单的方法?国家几乎和你期望的一样,国家确实是控制器中填充的国家名单......
控制器签名(我也尝试过请求者):
@RequestMapping(value = "/Lame", method = RequestMethod.POST)
public
@ResponseBody
boolean getLame(@RequestParam String strToMatchA, @RequestParam String strToMatchB) {}
Run Code Online (Sandbox Code Playgroud)
这就是我的json:
{
"strToMatchA": "EN",
"strToMatchB": "lon"
}
Run Code Online (Sandbox Code Playgroud)
不工作,我收到错误:
org.springframework.web.bind.MissingServletRequestParameterException: Required String parameter 'strToMatchA' is not present
Run Code Online (Sandbox Code Playgroud)
从方法签名中删除第一个参数然后使其工作(方法被正确调用),我该怎么办?
当我更改要注释的方法参数时,@RequestBody我得到以下错误:
java.io.IOException: Stream closed
Run Code Online (Sandbox Code Playgroud) 我的大脑开始思考这个问题,它是如此简单:
@ManyToMany(mappedBy = "following", cascade = CascadeType.ALL)
private Set<User> followers = new HashSet<User>();
@ManyToMany(mappedBy = "followers", cascade = CascadeType.ALL)
private Set<User> following = new HashSet<User>();
Run Code Online (Sandbox Code Playgroud) 通常情况下,当我知道属性名称时,我会使用注释填充字段:
@Value("${myproperties.myValue}")
private String myString
Run Code Online (Sandbox Code Playgroud)
但是,我现在想要遍历文件中的所有属性,当它们的名称未知时,并存储值和名称.spring和java的最佳方法是什么?
java ×8
spring ×5
javascript ×3
json ×2
spring-mvc ×2
gson ×1
guava ×1
hashmap ×1
hibernate ×1
html ×1
jquery ×1
many-to-many ×1
properties ×1
unit-testing ×1