小编Jor*_*oel的帖子

lambda表达式中使用的变量应该是final或者有效的final

lambda表达式中使用的变量应该是final或者有效的final

当我尝试使用calTz它时显示此错误.

private TimeZone extractCalendarTimeZoneComponent(Calendar cal, TimeZone calTz) {
    try {
        cal.getComponents().getComponents("VTIMEZONE").forEach(component -> {
            VTimeZone v = (VTimeZone) component;
            v.getTimeZoneId();
            if (calTz == null) {
                calTz = TimeZone.getTimeZone(v.getTimeZoneId().getValue());
            }
        });
    } catch (Exception e) {
        log.warn("Unable to determine ical timezone", e);
    }
    return null;
}
Run Code Online (Sandbox Code Playgroud)

java lambda

107
推荐指数
5
解决办法
14万
查看次数

返回for循环或外循环

今天有人告诉我return在Java中使用关键字.我编写了一个简单的for循环来验证数组中的某些东西.假设array是一个长度为n的数组,这是我的代码:

for (int i = 0; i < array.length; ++i) {
    if (array[i] == valueToFind) {
        return true;
    }
}
return false;
Run Code Online (Sandbox Code Playgroud)

现在有人告诉我这不是很好的编程,因为我n在循环中使用语句,这会导致垃圾收集失灵.因此,更好的代码将是:

int i = 0;
while (i < array.length && array[i] != valueToFind) {
    ++i;
}
return i != array.length;
Run Code Online (Sandbox Code Playgroud)

问题是我无法正确地阐述为什么第一个for循环不是一个好习惯.有人可以给我一笔费用吗?

java for-loop while-loop

26
推荐指数
2
解决办法
7万
查看次数

获取EntityManagerFactory的最佳实践

在Web应用程序(jsp/servlets)中获取EntityManagerFactory的最佳方法是什么?这是一个好方法何时应该创建/打开EntityManagerFactory实例?或者从JNDI或其他东西获得它更好吗?

java jpa jndi

23
推荐指数
1
解决办法
4万
查看次数

在Java中合并两个对象

我有两个相同类型的对象.

Class A {
  String a;
  List b;
  int c;
}

A obj1 = new A();
A obj2 = new A();

obj1 => {a = "hello"; b = null; c = 10}
obj2 => {a = null; b = new ArrayList(); c = default value}
Run Code Online (Sandbox Code Playgroud)

你能否告诉我将这些对象组合成单个对象的最佳方法是什么?

obj3 = {a = "hello"; b = (same arraylist from obj2); c = 10}
Run Code Online (Sandbox Code Playgroud)

java reflection

11
推荐指数
6
解决办法
5万
查看次数

设置 Spring 安全性以在未通过身份验证的情况下将用户重定向到登录页面

我有一个具有 Spring 安全性的 Spring 启动应用程序。

我的问题与类似,但在我的情况下,login如果用户在尝试访问应用程序的任何页面时未通过身份验证,我想将用户重定向到该页面。

下图显示了应用程序的架构:

应用程序架构

我的配置类如下所示:

@EnableWebSecurity
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    public void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .antMatchers("/**").hasAnyRole("USER")
                .and().formLogin().loginPage("/login").permitAll()
                .and().authorizeRequests().antMatchers("/resources/**").permitAll().anyRequest().permitAll();
    }

    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication().withUser("user").password("password").roles("USER");
    }

}
Run Code Online (Sandbox Code Playgroud)

使用此配置,将不会加载任何资源。如果用户未通过身份验证并且同时加载了我的资源文件夹,我该如何配置我的项目以将用户重定向到登录页面?

java spring spring-security spring-boot

8
推荐指数
2
解决办法
1万
查看次数

JPA从具有多对多关联的集合中进行选择

我有2个具有多对多关联的实体类.

ModPm:

@Entity
@Table(name = "MOD_PM")
public class ModPm extends WebPageObject implements Serializable, IDBNamedEntity {

    private static final long serialVersionUID = 1L;

    public final static String Q_GET_WITHOUT_STATUS_FOR_SCOPE = "ModPm.getWithoutStatusForScope";

    @Id
    private long id;
    ....
    @ManyToMany
    @JoinTable(name = "MOD_PM_SCOPE_TYPES",
            joinColumns = @JoinColumn(name = "PM_ID"),
            inverseJoinColumns = @JoinColumn(name = "SCOPE_TYPE_ID")
    )
    private List<ModScopeType> modScopeTypes;
Run Code Online (Sandbox Code Playgroud)

ModScopeType:

@Entity
@Table(name = "MOD_SCOPES")
@Cacheable
public class ModScopeType extends WebPageObject implements Serializable {

    private static final long serialVersionUID = 1L;

    public final static String Q_GET_ALL = "ModScopeType.getAll"; …
Run Code Online (Sandbox Code Playgroud)

java collections many-to-many jpa

6
推荐指数
1
解决办法
2万
查看次数

我们如何在Spring Hibernate中解决DataIntegrityViolationException?

我正在使用Spring Hibernate和HibernateSupportDao,当我尝试发送多行数据存储在我的数据库中时,它会DataIntegrityViolationException在特定行引发一个.同一个方案在同一个表上保留在任务中.即使我对当前任务使用相同的代码,我也不会遇到问题.为什么DataIntegrityViolationException抛出,我该如何纠正?

spring hibernate

5
推荐指数
1
解决办法
5万
查看次数

Thymeleaf 和 #fields.hasErrors

我正在为学校做这项作业。使用 SpringMVC、Hibernate JPA 和 Thymeleaf。下面的代码涉及一个名为“ stringGrade ”的特定属性。我想使用 Hibernate Validator 验证该字段中的输入。我似乎无法让 Thymeleaf 读懂这个表达式。在视图中循环的 arrayList 的名称属性为“deliverables[0].stringGrade”,依此类推,具体取决于有多少个。我尝试使用“ Deliverables[ ${stat.index} ].name ”,这会导致 Thymeleaf 失败并出现以下错误:

HTTP Status 500 - 请求处理失败;嵌套异常是 org.thymeleaf.exceptions.TemplateProcessingException:评估 SpringEL 表达式的异常:“#fields.hasErrors('deliverables[0].stringGrade')” (menuItems/inputGrades:33)

我想做的就是让 Thymeleaf 能够使用 #fields.HasErrors 和 #fields.error 读取值。下面是相关的代码:

成绩计算器型号:

public class GradeCalculator {

private ArrayList<Deliverable> deliverables;
Run Code Online (Sandbox Code Playgroud)

可交付模型:

@Entity
@Table(name="Deliverables")
public class Deliverable implements Serializable {


@NotEmpty(message = "Required")
@Size(min = 1, max = 100, message = "Must be between 1 and 100")
@Digits(integer = 3, fraction = 0, message = "Must be …
Run Code Online (Sandbox Code Playgroud)

spring spring-el thymeleaf

5
推荐指数
1
解决办法
2万
查看次数

使用 Spring boot 将数据插入关系表(多对多)

我制作了一个包含一些用户的 Spring Boot 应用程序。这些用户可以属于 0 个、一个或多个组(为了更好的可视化,我省略了一些代码行):

@Entity
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    private String name;

    @ManyToMany
    @JoinTable(
            name = "group_user",
            joinColumns = {@JoinColumn(name = "user_id")},
            inverseJoinColumns = {@JoinColumn(name = "group_id")}
    )
    private List<Group> groups = new ArrayList<>();

    public User(String name, List<Group> groups) {
        this.name = name;
        this.groups = groups;
    }
}
Run Code Online (Sandbox Code Playgroud)

一个组可以包含 0 个、一个或多个用户。

@Entity
public class Group {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    private String name;

    @ManyToMany(mappedBy = "groups")
    private List<User> …
Run Code Online (Sandbox Code Playgroud)

java mysql many-to-many jpa spring-boot

5
推荐指数
1
解决办法
2万
查看次数