Tag: 弹簧

SpringMVC RequestMapping GET参数

如何使RequestMapping在URL中处理GET参数? 例如,我有这个url http://localhost:8080/userGrid?_search=false&nd=1351972571018&rows=10&page=1&sidx=id&sord=desc (来自jqGrid) 我的RequestMapping应该如何? 我想要使​​用HttpReqest获取参数 试过这个: @RequestMapping("/userGrid") public @ResponseBody GridModel getUsersForGrid(HttpServletRequest request) 但它不起作用。

如何使用@PathVariable对Spring MVC控制器进行unit testing?

我有一个类似于这个简单的注释控制器: @Controller public class MyController { @RequestMapping("/{id}.html") public String doSomething(@PathVariable String id, Model model) { // do something return "view"; } } 我想用这样的unit testing来testing它: public class MyControllerTest { @Test public void test() { MockHttpServletRequest request = new MockHttpServletRequest(); request.setRequestURI("/test.html"); new AnnotationMethodHandlerAdapter() .handle(request, new MockHttpServletResponse(), new MyController()); // assert something } } 问题是AnnotationMethodHandlerAdapter.handler()方法抛出一个exception: java.lang.IllegalStateException: Could not […]

将applicationContext分割为多个文件

将Spring的configuration拆分为多个xml文件的正确方法是什么? 目前我有 /WEB-INF/foo-servlet.xml /WEB-INF/foo-service.xml /WEB-INF/foo-persistence.xml 我的web.xml有以下内容: <servlet> <description>Spring MVC Dispatcher Servlet</description> <servlet-name>intrafest</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value> /WEB-INF/foo-*.xml </param-value> </init-param> <load-on-startup>2</load-on-startup> </servlet> <context-param> <param-name>contextConfigLocation</param-name> <param-value> /WEB-INF/foo-*.xml </param-value> </context-param> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> 实际问题: 这种方法是否正确/最好 ? 我真的需要在DispatcherServlet 和 context-param部分中指定configuration位置吗? 我需要记住什么才能够从foo-service.xml引用在foo-servlet.xml定义的bean? 这是否与在web.xml指定contextConfigLocation有关? 更新1: 我正在使用Spring框架3.0。 这是我的理解,我不需要做这样的资源导入: <import resource="foo-services.xml"/> 这是一个正确的假设吗?

如何防止Spring 3.0 MVC @ModelAttributevariables出现在URL中?

使用Spring MVC 3.0.0.RELEASE,我有以下控制器: @Controller @RequestMapping("/addIntake.htm") public class AddIntakeController{ private final Collection<String> users; public AddIntakeController(){ users = new ArrayList<String>(); users.add("user1"); users.add("user2"); // … users.add("userN"); } @ModelAttribute("users") public Collection<String> getUsers(){ return this.users; } @RequestMapping(method=RequestMethod.GET) public String setupForm(ModelMap model){ // Set up command object Intake intake = new Intake(); intake.setIntakeDate(new Date()); model.addAttribute("intake", intake); return "addIntake"; } @RequestMapping(method=RequestMethod.POST) public […]

弹簧启动默认H2 jdbc连接(和H2控制台)

我只是试图看到一个embedded式H2数据库的H2数据库内容,当我没有在我的application.properties中指定任何东西时,spring-boot会创build一个embedded式H2数据库,并以mvn spring:run开头。 我可以看到hibernateJPA创build表,但如果我尝试访问下面的URL在数据库中的h2控制台没有表。 http://localhost:8080/console/ 我看到这样的build议: 查看由Spring启动的embedded式H2数据库的内容 但是我不知道在spring-boot中把build议的XML放在哪里,即使我这样做了,我也不希望h2console在外部数据库configuration的时候可用,所以我更需要处理这个与一些有条件的代码(或者也许只是允许弹簧自动处理它在最理想的情况下,其中我只包括H2时,mavenconfiguration文件被激活)。 有没有人有一些示例代码显示如何让H2控制台在启动工作(也是如何找出弹簧使用的jdbc连接string是什么)?

有没有可能使空的RequestParam值使用defaultValue?

如果我有一个请求映射类似于以下内容: @RequestMapping(value = "/test", method = RequestMethod.POST) @ResponseBody public void test(@RequestParam(value = "i", defaultValue = "10") int i) { } 然后通过以下方式调用此请求: http://example.com/test?i= 我收到错误消息 无法将types'java.lang.String'的值转换为types'int'; 嵌套exception是java.lang.NumberFormatException:对于inputstring:“”' 我可以通过停止JavaScript客户端发送空参数,或接受string值,只有parsing,如果他们没有发现是空白的解决这个问题。 更新 :spring的更高版本现在实现最初所需的行为。 我刚刚在春季4.3.5testing过,发现现在的行为实际上将空值转换为默认值,而不会引发NumberFormatException , 我原来的映射现在工作正常。 我不确定这个行为改变的spring是哪个版本。

只从Spring MVC 3控制器返回string消息

任何人都可以告诉我如何可以从控制器返回string消息? 如果我只是从一个控制器方法返回一个string,然后springmvc把它视为一个jsp视图名称。

Mockito,JUnit和Spring

我今天才开始学习Mockito。 我写了一些简单的testing(使用JUnit,见下文),但我不知道如何在Spring的pipe理bean中使用模拟对象。 与Spring合作的最佳实践是什么? 我应该如何向我的bean注入嘲弄的依赖关系? 你可以跳过这个直到回到我的问题 。 首先,我学到了什么。 这是非常好的文章Mocks不是解释基础的存根 (Mock的检查行为validation不是状态validation )。 那么这里有一个很好的例子,在这里Mockito 嘲笑与mockito我们有解释,Mockito的模拟对象都是模拟和存根(stub) 。 这里的Mockito和这里的Matchers你可以find更多的例子。 这个testing @Test public void testReal(){ List<String> mockedList = mock(List.class); //stubbing //when(mockedList.get(0)).thenReturn("first"); mockedList.get(anyInt()); OngoingStubbing<String> stub= when(null); stub.thenReturn("first"); //String res = mockedList.get(0); //System.out.println(res); //you can also verify using argument matcher //verify(mockedList).get(anyInt()); verify(mockedList); mockedList.get(anyInt()); } 工作得很好。 回到我的问题。 这里注入Mockito到一个Spring bean有人尝试使用Springs ReflectionTestUtils.setField() ,但在这里比Spring集成testing,创build模拟对象,我们build议改变 Spring的上下文。 我真的不明白最后两个链接…有人可以解释我有什么问题与Mockito的? 这个解决scheme有什么问题? […]

Spring Boot JPA – configuration自动重新连接

我有一个不错的小弹簧引导jpa web应用程序。 它被部署在亚马逊豆茎上,并使用亚马逊rds来保存数据。 然而,经常会有这样一个例外,那里不是那么经常使用的: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server was 79,870,633 milliseconds ago. The last packet sent successfully to the server was 79,870,634 milliseconds ago. is longer than the server configured value of 'wait_timeout'. You should consider either expiring and/or testing connection validity before use in your application, increasing the server […]

我应该为业务层使用EJB3还是Spring?

我的团队正在开发一个带有Web前端的新的面向服务的产品。 在讨论我们将运用JBoss应用程序服务器以及Flex前端(使用Adobe AIR进行可能的桌面部署)以及用于连接客户端和服务器的Web服务时,我们将使用哪些技术。 当谈到用于我们的业务逻辑的服务器技术时,我们已经陷入僵局。 EJB3和Spring之间的争论很大,我们最大的担心是可扩展性和性能,以及代码库的可维护性。 这是我的问题: 什么是针对或反对EJB3与Spring的争论? 我可以期待什么陷阱? 我在哪里可以find很好的基准信息?