小编nil*_*gun的帖子

在组件上反应onClick事件

我有一个名为React的组件<SensorList />,它有许多子代<SensorItem />(另一个React组件).我希望能够从内部声明onClick每个事件.我尝试过以下操作:<SensorItem /><SensorList />

sensorSelected: function(sensor) {
    console.log('Clicked!');
},

render: function() {
    var nodes = this.state.sensors.map(function(sensor) {
        return (
            <SensorItem onClick={ this.sensorSelected } />
        );
    }.bind(this));

    return (
        <div className="sensor-list">
            { nodes }
        </div>
    );
}
Run Code Online (Sandbox Code Playgroud)

毋庸置疑,我没有得到任何"点击!" 进入我的控制台.Chrome中的React检查器指示onClick已注册事件,并具有上述功能主体.

因此,我得出结论,我无法onClick在实际<SensorItem />标签上注册事件(但我不知道为什么会这样).我怎么去实现这个呢?

javascript events reactjs

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

spring mvc rest service redirect/forward/proxy

我使用spring mvc框架构建了一个Web应用程序来发布REST服务.例如:

@Controller
@RequestMapping("/movie")
public class MovieController {

@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public @ResponseBody Movie getMovie(@PathVariable String id, @RequestBody user) {

    return dataProvider.getMovieById(user,id);

}
Run Code Online (Sandbox Code Playgroud)

现在我需要部署我的应用程序,但是我遇到以下问题:客户端无法直接访问应用程序所在的计算机(有防火墙).因此,我需要在代理机器上(可由客户端访问)调用实际的休息服务的重定向层.

我尝试使用RestTemplate进行新的调用:例如:

@Controller
@RequestMapping("/movieProxy")
public class MovieProxyController {

    private String address= "http://xxx.xxx.xxx.xxx:xx/MyApp";

    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public @ResponseBody Movie getMovie(@PathVariable String id,@RequestBody user,final HttpServletResponse response,final HttpServletRequest request) {

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        RestTemplate restTemplate = new RestTemplate();
        return restTemplate.exchange( address+ request.getPathInfo(), request.getMethod(), new HttpEntity<T>(user, headers), Movie.class);

}
Run Code Online (Sandbox Code Playgroud)

这没关系,但我需要重写控制器中的每个方法以使用resttemplate.此外,这会导致代理计算机上的冗余序列化/反序列化.

我尝试使用restemplate编写泛型函数,但它没有用完:

@Controller …
Run Code Online (Sandbox Code Playgroud)

java spring-mvc resttemplate spring-boot

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

如何在呈现React组件时添加逻辑if语句?

如果我的代码看起来像这样......

var Lounge = React.createClass({displayName: "Lounge",
  render: function() {
    return (
            React.createElement("a", {href:"/lounge/detail/" + this.props.id +  "/"},
            React.createElement("div", {className: "lounge"},
            React.createElement("h2", {className: "loungeAuthor"},
              this.props.author.name
            ),
            React.createElement("p", {className: "loungeArticle"},
              this.props.article
            ),
            React.createElement("img", {className: "loungeImage", src: this.props.image})
          )
        )
    );
  }
});
Run Code Online (Sandbox Code Playgroud)

如果图像数据存在,我需要检查以仅渲染"img"组件.有人知道使用React解决这个问题的最佳方法吗?

reactjs

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

HTML + React:单选按钮卡在最初设置为"已检查"的按钮上

我有一组两个单选按钮,其中一个在加载页面时被检查.看来我无法将其改为第二个.如果重要的话,HTML由reactjs构建.chrome上的标记看起来像:

<fieldset data-reactid=".0.0.0.0.0.0.1">
<label for="coop-radio-btn" data-reactid=".0.0.0.0.0.0.1.0">
    <span data-reactid=".0.0.0.0.0.0.1.0.0">To a Cooperative</span>
    <input type="radio" name="requestOnBehalfOf" value="coop" id="coop-radio-btn" data-reactid=".0.0.0.0.0.0.1.0.1">
</label>
<label for="union-radio-btn" data-reactid=".0.0.0.0.0.0.1.1">
    <span data-reactid=".0.0.0.0.0.0.1.1.0">Additional Request</span>
    <input type="radio" name="requestOnBehalfOf" value="union" checked="" id="union-radio-btn" data-reactid=".0.0.0.0.0.0.1.1.1">
</label>
</fieldset>
Run Code Online (Sandbox Code Playgroud)

html javascript html5 radio-button reactjs

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

将AJAX结果作为道具传递给子组件

我正在尝试在React中创建一个博客.在我的主ReactBlog组件中,我正在对节点服务器进行AJAX调用以返回一组帖子.我想将此帖子数据作为道具传递给不同的组件.

特别是,我有一个名为PostViewer的组件,它将显示帖子信息.我希望它默认显示从其父级通过props传入的帖子,否则显示通过状态调用设置的数据.

目前,我的代码的相关部分看起来像这样.

var ReactBlog = React.createClass({
  getInitialState: function() {
    return {
      posts: []
    };
  },
  componentDidMount: function() {
    $.get(this.props.url, function(data) {
      if (this.isMounted()) {
        this.setState({
          posts: data
        });
      }
    }.bind(this));
  },
  render: function() {
    var latestPost = this.state.posts[0];
    return (
      <div className="layout">
        <div className="layout layout-sidebar">
          <PostList posts={this.state.posts}/>
        </div>
        <div className="layout layout-content">
          <PostViewer post={latestPost}/>
        </div>
      </div>
    )
  }
});
Run Code Online (Sandbox Code Playgroud)

和子组件:

var PostViewer = React.createClass({
  getInitialState: function() {
    return {
      post: this.props.post
    }
  },
  render: function() {
    /* handle check for …
Run Code Online (Sandbox Code Playgroud)

javascript ajax asynchronous reactjs

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

Hibernate随机"Session is closed error"包含2个数据库

我要求在单个DAO类中使用2个不同的数据库.其中一个数据库是读/写,而另一个是只读的.

我为这些数据库创建了2个数据源,2个会话工厂和2个事务管理器(读/写数据库的事务管理器是平台事务管理器).我正在使用@Transactional服务方法来配置Spring进行事务管理.

当我们调用DAO类时,我们会得到随机 Session is closed!异常sessionFactory.getCurrentSession()(我不能总是生成它,它有时可以正常工作,有时会出错):

org.hibernate.SessionException: Session is closed!
at org.hibernate.internal.AbstractSessionImpl.errorIfClosed(AbstractSessionImpl.java:133)
at org.hibernate.internal.SessionImpl.setFlushMode(SessionImpl.java:1435)
at org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:99)
at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:1014)
Run Code Online (Sandbox Code Playgroud)

我没有要求使用全局事务(XA),我只想查询2个不同的数据库.

我已经读过这个帖子,它建议像我们现在一样在DAO层中注入两个独立的会话工厂:处理多个数据库连接的会话工厂

另外,根据这个答案,AbstractRoutingDataSource对单个Dao类不起作用:https://stackoverflow.com/a/7379048/572380

来自my dao的示例代码如下所示:

Criteria criteria = sessionFactory1.getCurrentSession().createCriteria(MyClass.class);
criteria.add(Restrictions.eq("id", id));
criteria.list();

criteria = sessionFactory2.getCurrentSession().createCriteria(MyClass2.class); // generates random "Session is closed!" error.
criteria.add(Restrictions.eq("id", id));
criteria.list();
Run Code Online (Sandbox Code Playgroud)

我也尝试过使用"doInHibernate"方法.但传递给它的会话也随机抛出"Session is closed!" 例外:

    @Autowired
    protected HibernateTemplate hibernateTemplate;

    @SuppressWarnings("unchecked")
    protected List<Map<String, Object>> executeStaticQuery(final String sql) {
        HibernateCallback<List<Map<String, Object>>> hibernateCallback = new HibernateCallback<List<Map<String, Object>>>() {
            @Override
            public List<Map<String, …
Run Code Online (Sandbox Code Playgroud)

java session spring dao hibernate

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

如何编写单元测试来检查复制构造函数与类属性同步?

最近,我们的系统中出现了一个错误,该错误是由于忘记在复制构造函数中分配新添加的类属性而引起的。

例如:

public class MyClass {

    private Long companyId;
    private Long classId;
    private Double value;   <=  newly added

    public MyClass(MyClass myClass) {
        this.setCompanyId(myClass.getCompanyId());
        this.setClassId(myClass.getClassId());
        this.setValue(myClass.getValue());   <= we forget this line
    }
Run Code Online (Sandbox Code Playgroud)

我想编写一个单元测试,保证在添加新属性时捕获丢失的副本分配。我应该如何进行?

java junit unit-testing copy-constructor

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

Microsoft 安全公告 ADV190023(LDAP 通道绑定和 LDAP 签名)的 Spring LDAP 影响

我们正在使用 Spring Security Ldap 库 (v4.0.4) 从我们客户端的 Active Directory (ldap://domain:389) 中获取用户列表,并对他们进行身份验证以登录到我们的 Web 应用程序。

微软最近发布了启用 LDAP 通道绑定和 LDAP 签名的公告:https : //portal.msrc.microsoft.com/en-us/security-guidance/advisory/ADV190023

“LDAP 通道绑定和 LDAP 签名提供了提高 LDAP 客户端和 Active Directory 域控制器之间通信安全性的方法。Active Directory 域控制器上存在一组不安全的 LDAP 通道绑定和 LDAP 签名默认配置,允许 LDAP 客户端与其通信没有强制执行 LDAP 通道绑定和 LDAP 签名。这可以打开 Active Directory 域控制器以提升特权漏洞。”

我们被问到在他们的服务器上启用 LDAP 通道绑定和 LDAP 签名是否会影响我们的流程。我在文档中找不到关于这些的信息:https : //docs.spring.io/spring-security/site/docs/current/reference/htmlsingle/#ldap

Spring Security Ldap 库 (v4.0.4) 是否支持这些?如果是这样,我们是否应该更改任何配置以确保事情不受影响?

spring active-directory spring-security spring-ldap spring-security-ldap

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

Spring ampq使用者 - 第一次尝试时"找不到当前线程的会话"错误

我有一个Spring MVC应用程序.我想用spring ampq来执行我的异步任务.我的REST后端使用@Transactional注释来管理事务.在完成它的工作之后,它将任务推送到出发,然后返回.然后,消费者接收任务,消费者在第一次尝试时收到"org.hibernate.HibernateException:No session for current thread"错误.然后spring-ampq重试将任务发送给消费者,这次它可以工作,没有任何会话错误.

如何在第一次尝试中使此方案有效?

我的spring ampq配置与此类似(http://projects.spring.io/spring-amqp/):

<rabbit:connection-factory id="connectionFactory" />

<rabbit:template id="amqpTemplate" connection-factory="connectionFactory"
    exchange="myExchange" routing-key="foo.bar"/>

<rabbit:admin connection-factory="connectionFactory" />

<rabbit:queue name="myQueue" />

<rabbit:topic-exchange name="myExchange">
    <rabbit:bindings>
        <rabbit:binding queue="myQueue" pattern="foo.*" />
    </rabbit:bindings>
</rabbit:topic-exchange>


<rabbit:listener-container connection-factory="connectionFactory"  advice-chain="retryInterceptor">
    <rabbit:listener ref="foo" method="listen" queue-names="myQueue" />
</rabbit:listener-container>

<bean id="foo" class="foo.Foo" />

<bean id="retryInterceptor" class="org.springframework.amqp.rabbit.config.StatefulRetryOperationsInterceptorFactoryBean">
    <property name="messageRecoverer" ref="rejectAndDontRequeueRecoverer"/>
    <property name="retryOperations" ref="retryTemplate"/>
    <property name="messageKeyGeneretor" ref="bodyBasedKeyGenerator"/>
</bean>
<bean id="bodyBasedKeyGenerator" class="com.mydomain.util.BodyBasedKeyGenerator"/>
<bean id="rejectAndDontRequeueRecoverer" class="org.springframework.amqp.rabbit.retry.RejectAndDontRequeueRecoverer"/>

<bean id="retryTemplate" class="org.springframework.retry.support.RetryTemplate">
    <property name="backOffPolicy">
        <bean class="org.springframework.retry.backoff.ExponentialBackOffPolicy">
            <property name="initialInterval" value="5000"/>
            <property name="maxInterval" value="120000"/> …
Run Code Online (Sandbox Code Playgroud)

hibernate transactions spring-mvc spring-transactions spring-amqp

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

何时创建新的Flux商店

我想知道在使用API​​时在Flux中创建商店时的最佳实践或惯例是什么

假设我们有一个'项目'列表,API调用最终会填充_projects中名为ProjectStore商店

然后,当用户选择项目时,您希望加载项目特定数据.您是否会将此添加到与_activeProject相同的ProjectStore中,或者为它创建一个单独的Store?

当您在该项目中加载Todo时也是如此.将它们放入TodoStore有意义的,但是项目中Todos中的特定Todo呢?

我希望上面有道理:)

javascript flux reactjs

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

在安装之前反应处理空对象的最佳方法是什么?

我有一个React类,它想要渲染如下所示的对象:

data: {
    title: "haha",
    description: {
        country: "US",
        year: "1996"
    }
}
Run Code Online (Sandbox Code Playgroud)

但是,当React想要呈现它时,它会产生错误.

未捕获错误:不变违规:receiveComponent(...):只能更新已安装的组件

我认为问题出在getInititalState中,我将我的数据声明为空对象,所以当我在超时后得到完整的数据对象时,React会尝试将我的数据对象映射到组件,但它会给出错误.

但有一件有趣的事情是,我没有问题访问this.props.title.title,但不是this.props.title.description.country,它会给出undefined

但是,当我调试它时,我可以看到我的对象.但是React无法访问它!

我的猜测是当React从空对象初始化时,它只会使用数据对象的第1级和第2级初始化虚拟DOM.

这个原因,当我尝试访问this.props.data.data.title时可以,但不是this.props.data.data.description.country

以下是我的代码

var BookBox = React.createClass({
    getInitialState: function() {
        return { data: {} };
    },
    componentWillMount: function() {
        var that = this;
        setTimeout(function() {
            console.log('timeout');
            that.setState({
                data: {
                    title: "haha",
                    description: {
                        country: "US",
                        year: "1996"
                    }
                }
            });
        }, 2000);
    },
    render: function() {
        return (
            <div>
                <h1>{this.state.data.title}</h1>
                <TestBox title={this.state.data} /> …
Run Code Online (Sandbox Code Playgroud)

javascript reactjs

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

我是否应该担心在使用Flyway时创建幂等迁移?

我一直在阅读一篇关于Flyway的博客文章,名为Lessons Learned Using Flyway DB with Distributed Version Control.作者的一个建议是创建幂等迁移.

引用文章:

在一个完美的世界中,每次迁移只会针对每个数据库运行一次.

在一个完美的世界里,就是这样.

实际上,在某些情况下,您需要针对同一数据库重新运行迁移.这通常是由于迁移过程中某个位置失败导致您必须回溯成功迁移的步骤才能使数据库恢复工作状态.当发生这种情况时,以幂等方式编写迁移非常有用.

假设我使用的是支持DDL事务的数据库,那么在创建这些迁移sqls时我是否应该担心幂等性?

flyway

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