标签: spring-3

Spring MVC 3.0:如何绑定到持久对象

我正在使用Spring MVC,我希望它能从数据库中绑定一个持久对象,但我无法弄清楚如何设置我的代码以在绑定之前调用DB.例如,我正在尝试将"BenefitType"对象更新到数据库,但是,我希望它从数据库中获取对象,而不是创建新对象,因此我不必更新所有字段.

    @RequestMapping("/save")
public String save(@ModelAttribute("item") BenefitType benefitType, BindingResult result)
{
    ...check for errors
    ...save, etc.
}
Run Code Online (Sandbox Code Playgroud)

data-binding spring persistence spring-mvc spring-3

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

spring 3.0 MVC似乎忽略了messages.properties

Spring 3.0 MVC

首先,我没有找到任何关于messages.properties @ springsource的文档. 我发现有关覆盖错误消息的所有内容都在各种论坛上.如果有人提到了messages.properties的记录,那就太棒了.也许messages.properties不是来自spring而是java规范?

我已经尝试遵循关于绑定之前的JSR-303类型检查的建议 我的目标是用我自己的用户友好错误消息替换一些类型不匹配错误消息

我的情况如下:

模型

public class Test {

    private int numberbomb;

    public int getNumberbomb() {
        return numberbomb;
    }

    public void setNumberbomb(int numberbomb) {
        this.numberbomb = numberbomb;
    }
}
Run Code Online (Sandbox Code Playgroud)

myservlet.xml

<mvc:annotation-driven/>
Run Code Online (Sandbox Code Playgroud)

JSP

<form:form id="test" method="post" modelAttribute="test">

<form:errors path="*"/>

<form:label path="numberbomb">numberbomb</form:label>
<form:input path="numberbomb"/>

</form:form>
Run Code Online (Sandbox Code Playgroud)

类\ messages.properties

typeMismatch=bad value you bad bad person
test.numberbomb=you are driving me crazy with these bad inputs
Run Code Online (Sandbox Code Playgroud)

表格输出

无法将类型为java.lang.String的属性值转换为属性numberbomb所需的int类型; 嵌套异常是org.springframework.core.convert.ConversionFailedException:无法将类型java.lang.String中的值"three"转换为int类型; 嵌套异常是java.lang.NumberFormatException:对于输入字符串:"three"

我的控制器中的BindingResult.toString()

字段'numberbomb'上对象'test'中的字段错误:被拒绝的值[3]; 码[typeMismatch.test.numberbomb,typeMismatch.numberbomb,typeMismatch.int,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable:codes [test.numberbomb,numberbomb]; 参数[]; …

forms model-view-controller spring-3

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

Spring 3.0 junit 测试 DispatcherServlet

我正在尝试使用 junit 测试我的应用程序。

因此,我设置了以下课程:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "/META-INF/spring/applicationContext-test.xml" )
@TransactionConfiguration
@Transactional
public class DispatcherServletTest extends AbstractJUnit4SpringContextTests {

    private MockHttpServletRequest request;
    private MockHttpServletResponse response;

    private DispatcherServlet dispatcher;

    @Before
    public void setUp() throws Exception {
            request = new MockHttpServletRequest();
            response = new MockHttpServletResponse();

            MockServletConfig config = new MockServletConfig("myapp");
            config.addInitParameter("contextConfigLocation","classpath*:webmvc-config.xml");

            dispatcher = new DispatcherServlet();
            dispatcher.init(config);
    }
    //test cases
Run Code Online (Sandbox Code Playgroud)

}

所以问题是,我的调度程序 servlet 似乎无法向我的任何控制器发送任何请求。

我认为配置中有一些东西 - contextConfigurationLocation。看起来他可以找到文件(否则会抛出异常),但不会加载任何配置

记录者说:

org.springframework.web.servlet.PageNotFound - 未找到带有 URI [http://localhost:8080/myapp/abc] 的 HTTP 请求的映射

但我完全不知道出了什么问题......

我将不胜感激任何帮助!

提前致谢

spring dispatcher junit4 spring-3

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

在Spring 3.1中,<mvc:interceptors>可以与@Configuration一起使用

我从Spring 3.0.5迁移到3.1,因为我需要自定义RequestMappingHandlerMapping.我在扩展RequestMappingHandlerMapping的插件中遇到问题 - 我有现有的servlet-conetxt.xml,我添加了带有@Configuration注释的WebConfig.但是,我总是得到错误ambiguos映射(因为在ExtendedRequestMappingHandlerMapping中定义的新注释不是takign生效).

我在servlet-context.xml中定义了各种级别的拦截器,我想保留在XML配置中.我想用.

有没有办法使用servlet-context.xml的连接,同时扩展RequestMappingHandlerMapping.如果必须使用@COnfiguration完成 - 我可以同时使用@COnfiguration和servlet-context.xml吗?任何帮助将不胜感激,因为我已经尝试了很长时间.

<context-param>
    <param-name>contextClass</param-name>
    <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
</context-param>

<context-param>
 <param-name>contextConfigLocation</param-name>
 <param-value>com.test.config</param-value>
</context-param>
Run Code Online (Sandbox Code Playgroud)

configuration spring-mvc spring-annotations spring-3

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

使用Property占位符表示Bean ID

在Spring 3.1中,是否可以使用属性占位符来解析bean的'id'属性?

我想,例如,在我的context.xml文件中执行以下操作:

<context:property-placeholder/>

<int:channel id="${channel.name}"/>
Run Code Online (Sandbox Code Playgroud)

spring spring-integration spring-3

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

WEB-INF和/ src/main/resources文件夹有什么区别?

在使用hibernate的Spring 3应用程序中,哪些文件"特定"需要在WEB-INF中?我也在使用Maven.

在应用程序在生产环境中运行之前,是否需要将/ src/main/resources中的某些文件复制到WEB-INF?

谢谢!

resources tomcat web-applications web-inf spring-3

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

如何使用Spring 3.2.1,Jboss AS7,Aspectj 1.7.1 java 1.7确定jms连接工厂的实现接口?

我们正在使用JBOSS AS7在java 1.6中开发一个项目,我们使用其中:Aspectj和HornetQ.我们需要升级到java 1.7,所以我们使用ASpectj 1.7.1.在部署期间,我们得到以下异常:

Caused by: org.springframework.beans.factory.BeanCreationException:
  Error creating bean with name 'eventsJmsTemplate' defined in class path resource [com/company/project/jms/jms.xml]: 
  Cannot resolve reference to bean 'jmsConnectionFactory' while setting bean property 'connectionFactory'; nested exception is
   org.springframework.beans.factory.BeanCreationException: 
  Error creating bean with name 'jmsConnectionFactory': 
  Post-processing of the FactoryBean's object failed; 
    nestedexception is java.lang.IllegalArgumentException: 
      warning can't determine implemented interfaces of missing type 
    com.company.project.aspects.MBeanAttributesAdvice [Xlint:cantFindType]
Run Code Online (Sandbox Code Playgroud)

mbean是:

<jee:jndi-lookup id="jmsConnectionFactory" jndi-name="java:/JmsXA" />
Run Code Online (Sandbox Code Playgroud)

在其他项目中,当我们使用JPA数据源时,我们遇到了相同的异常:

<jee:jndi-lookup id="dataSource" jndi-name="java:jboss/datasources/table" />
<bean id="jpaDialect" class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
Run Code Online (Sandbox Code Playgroud)

我们设法通过在jboss-deployment-structure上添加模块依赖来解决问题:org.jboss.ironjacamar.jdbcadapters

<jboss-deployment-structure>
    <deployment>
        <exclusions>
            <module name="org.slf4j" …
Run Code Online (Sandbox Code Playgroud)

aspectj hornetq java-7 spring-3 jboss7.x

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

如何在spring中使用java枚举作为bean

我想要实现的目标是将枚举注入 Spring 管理的 bean。

我不希望我的 bean 被配置为 XML(除了这个不工作的枚举,没有理由这样做)。

我有一个简单的 Maven 项目:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>net.betlista</groupId>
    <artifactId>test.spring.enum-injection</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>3.1.3.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>3.1.3.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>1.5.4</version>
        </dependency>

        <!-- testing -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>3.1.3.RELEASE</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <properties>
        <junit.version>4.11</junit.version>
    </properties>
</project>
Run Code Online (Sandbox Code Playgroud)

我创建了简单的测试

import net.betlista.spring.enum_injection.EnumInjectionComponent;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;


@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class TestClass {

    @Autowired
    EnumInjectionComponent component; …
Run Code Online (Sandbox Code Playgroud)

java spring spring-3

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

PropertyPlaceholderConfigurer 与 ReloadableResourceBundleMessageSource

在 Google 上搜索如何在 Spring 3 中配置属性文件,我得到了许多不同的答案。我发现,ReloadableResourceBundleMessageSourcePropertyPlaceholderConfigurer可用于从属性文件获取的属性。有人可以解释一下这些之间的区别吗?

properties-file spring-3

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

对使用工厂方法创建的 bean 使用 spring @Transactional?

我正在使用 spring3。我有以下课程。

运输工具

package com.net.core.ioc;

public interface Transport {
  public void doSave();
}
Run Code Online (Sandbox Code Playgroud)

Car.java

package com.net.core.ioc;

    public class Car implements Transport{
     String name;
     public Car(String name){
         this.name=name;
      }
        public void doSave(){
            //saving logic using name
        }
    }
Run Code Online (Sandbox Code Playgroud)

总线.java

package com.net.core.ioc;

    public class Bus implements Transport {
      String id;
      public Bus(String id){
         this.id=id;
      }
        public void doSave() {
            //saving logic using id
        }
Run Code Online (Sandbox Code Playgroud)

SpringService.java

package com.net.core.ioc;

    public class ServiceLocator {
        private static ServiceLocator service = new ServiceLocator ();

        //Static factory method …
Run Code Online (Sandbox Code Playgroud)

java spring spring-3

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