小编You*_*sef的帖子

jsp页面中的必需属性

嗨,我正在使用spring mvc,我在客户端寻找验证我的表单的方法,所以我发现了这个代码为html 5.

<form:input path="login" type="text" required />
Run Code Online (Sandbox Code Playgroud)

我把我的表单的输入元素中的必需属性放在我的jsp页面中,但我得到500 http错误.

Etat HTTP 500 - /WEB-INF/pages/index.jsp (line: 44, column: 48) Symbole égal (equal) attendu

org.apache.jasper.JasperException: /WEB-INF/pages/index.jsp (line: 44, column: 62) Symbole égal (equal) attendu
Run Code Online (Sandbox Code Playgroud)

html5 jsp spring-mvc

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

DTO中的动态字段类型

我正在使用,Spring-MVC并且具有如下所示的DTO结构,JSON用于从客户端(foo实体)接收数据,并使用以下命令将其保存到数据库中JPA

public class FooDTO {

    public Integer id;
    public String label;
    public Double amount;
    public List<Integer> states;
    ...
Run Code Online (Sandbox Code Playgroud)

但是当客户想要编辑foo实体时,我必须像下面这样构造它

public class FooDTO {

    public Integer id;
    public String label;
    public Double amount;
    public List<SimpleDto> states;
    ...
Run Code Online (Sandbox Code Playgroud)

SimpleDto

public class SimpleDto {
    public Integer value;
    public String label;
}
Run Code Online (Sandbox Code Playgroud)

区别只是states有时List<SimpleDto>和有时是类型,List<Integer>而我不想创建另一个dto。

那么如何在dto(json)中实现动态字段类型呢?

P.S JSON data is handled by com.fasterxml.jackson.core

java spring spring-mvc jackson

3
推荐指数
2
解决办法
759
查看次数

DataSourceTransactionManager 和 ResourcelessTransactionManager 有什么区别?

是有什么区别org.springframework.jdbc.datasource.DataSourceTransactionManagerorg.springframework.batch.support.transaction.ResourcelessTransactionManager

<bean id="batchTransactionManager" class=
  "org.springframework.batch.support.transaction.ResourcelessTransactionManager" />
  
<bean id="batchJobLauncher"
  class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
    <property name="jobRepository" ref="batchJobRepository" />
    <property name="taskExecutor">
        <bean class="org.springframework.core.task.SimpleAsyncTaskExecutor" />
    </property>
</bean>
Run Code Online (Sandbox Code Playgroud)

在上面的代码中使用resourcelesstransactionmanager是否正确?或者我应该使用 datasourcetransactionmanager 吗?有什么区别以及何时使用它们?

spring transactions transactionmanager spring-batch spring-transactions

3
推荐指数
1
解决办法
2764
查看次数

用嵌套构造函数查询

当我使用custum DTO尝试使用嵌套构造函数进行此查询时。

select new DTO.foo ( new DTO.boo( ... ) , new DTO.boo( ... ) ) from Foo
Run Code Online (Sandbox Code Playgroud)

我收到此错误:

org.hibernate.hql.internal.ast.QuerySyntaxException: 
unexpected token: new near line 1, column 23 [ select new DTO.foo ( new DTO.boo( ... ) , .....
Run Code Online (Sandbox Code Playgroud)

解决方案
由于在构造函数中不能包含构造函数,因为它在中是非法的,因此JPQL我们在此处采用相同的方法来解决此问题:https : //stackoverflow.com/a/12742926/1383538

java hibernate jpa

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

@MockBean 注入正确,但测试类上有 null 值

我正在使用 Cucumber 和 Spring Boot 进行测试

@CucumberContextConfiguration
@ActiveProfiles(profiles = { "cucumber" })
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE)
@ExtendWith(SpringExtension.class)
public class FooTest {

    @Autowired
    BatchService batchService;

    @MockBean
    S3ClientService s3ClientService;

    @MockBean
    HttpClientService httpClientService;

    @SpyBean
    UndueService undueService;


    @Given("^foo cucumber test$")
    public void foo_cucumber_test() {
        System.out.println("Foo Test");
    }
}
Run Code Online (Sandbox Code Playgroud)

当我在 @Given 方法上使用断点运行/调试测试时

在此输入图像描述

我得到了这个奇怪的行为,@Mockbean/@SpyBean被正确注入,但在测试类中它的值是null!我无法运行 Mockito 函数verifywhen

但是当我在没有黄瓜的情况下进行测试时

@Test
void fooTest() {
    System.out.println("Foo Test");
}
Run Code Online (Sandbox Code Playgroud)

效果很好!这些值不是null

java spring cucumber mockito cucumber-java

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

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

使用spring-data进行通用搜索

我使用spring数据,我觉得非常有趣,但有一个问题,我想要一个通用的方式来搜索实体的领域.

我有一个有很多领域的实体

public class Lostcard implements java.io.Serializable {
    private Integer id;
    private String nom;
    private String prenom;
    private String cin;
    @DateTimeFormat(pattern = "MM/dd/yyyy")
    private Date dateDeclaration;
    @DateTimeFormat(pattern = "MM/dd/yyyy")
    private Date dateDuplicata;
    private String annexeAdmin;
[...]
Run Code Online (Sandbox Code Playgroud)

所以我想这样做:

public interface LostcardRepository extends JpaRepository<Lostcard, Integer> {

List<Lostcard> findByNom(String nom);
List<Lostcard> findByPrenom(String prenom);
List<Lostcard> findByCin(String cin);
    [...]
}
Run Code Online (Sandbox Code Playgroud)

有没有像findByProperty(String属性,Object value)这样的通用方法?

spring spring-data

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

在锚标记之前执行函数并在之后加载它

如何在单击锚标记之前<a href="/foo" id="foo">Foo</a>和单击之后执行函数?

我试试这个:

$(document).on('click', '#foo', function(e) {
      // stuff to do
      return false;
}
Run Code Online (Sandbox Code Playgroud)

该函数已执行,但链接 /foo 未像默认行为那样加载。

javascript jquery

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

使用 SpringBoot 外部化 Spring Kafka 配置

我有这个卡夫卡配置类

@Configuration
public class KafkaConfiguration {
    @Value("${KAFKA_SERVERS}")
    private String kafkaServers;


    @Bean
    ConcurrentKafkaListenerContainerFactory<String, String> kafkaListenerContainerFactory() {
        ConcurrentKafkaListenerContainerFactory<String, String> factory = new ConcurrentKafkaListenerContainerFactory<>();
        factory.setConsumerFactory(this.consumerFactory());
        factory.setMessageConverter(new StringJsonMessageConverter());
        return factory;
    }

    public ConsumerFactory<String, String> consumerFactory() {
        return new DefaultKafkaConsumerFactory<>(this.consumerConfigs());
    }

    public Map<String, Object> consumerConfigs() {

        Map<String, Object> props = new HashMap<>();
        props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, kafkaServers);
        props.put(ConsumerConfig.GROUP_ID_CONFIG, "foo-group-id");
        props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "latest");
        props.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, true);
        props.put(ConsumerConfig.AUTO_COMMIT_INTERVAL_MS_CONFIG, "100");
        props.put(ConsumerConfig.SESSION_TIMEOUT_MS_CONFIG, "15000");
        props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
        props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
        return props;
    }

    public Map<String, Object> producerConfigs() {
        Map<String, Object> props = new HashMap<>();
        props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, kafkaServers);
        props.put(ProducerConfig.RETRIES_CONFIG, …
Run Code Online (Sandbox Code Playgroud)

java apache-kafka spring-boot spring-kafka

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

组件扫描问题@Transactional不适用于@Controller

我认为@Transactional不适用于其他注释,@Controller或者我有问题和困惑,component-scan因为我得到这个错误:

org.hibernate.HibernateException: No Session found for current thread
Run Code Online (Sandbox Code Playgroud)

这是我的配置文件:

MVC-调度-servlet.xml中

<context:component-scan base-package="controller"/>
<mvc:annotation-driven />
Run Code Online (Sandbox Code Playgroud)

Hibernate.xml

   <context:component-scan base-package="controller"/>

   <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
      ....
   </bean>

   <bean id="dataSource" class="org.apache.tomcat.dbcp.dbcp2.BasicDataSource">
      ...
   </bean>

   <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
      ...
   </bean>

   <tx:annotation-driven transaction-manager="transactionManager" />
Run Code Online (Sandbox Code Playgroud)

我的测试控制器

@Transactional
@Controller
public class Test {

    @Autowired
    private SessionFactory sessionFactory;

    @RequestMapping(value="/") 
    public String test() {
        ...
        sessionFactory.getCurrentSession().get(Region.class,1);
        ...
    }
    ...
}
Run Code Online (Sandbox Code Playgroud)

我的web.xml

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>hibernate.xml</param-value>
</context-param>


<servlet>
    <servlet-name>mvc-dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>mvc-dispatcher-servlet.xml</param-value>
    </init-param> 
    <load-on-startup>1</load-on-startup>
</servlet> …
Run Code Online (Sandbox Code Playgroud)

spring hibernate spring-mvc

0
推荐指数
1
解决办法
1843
查看次数