Tag: spring mvc

在spring mvc中创建自定义注释并获取httpservletrequest对象

我想创建自定义注释并使用HttpServletRequest对象将该注释放在方法级别上。 到目前为止我这样做了: 创建注释 @Target(value={ElementType.METHOD,ElementType.PARAMETER}) @Retention(value=RetentionPolicy.RUNTIME) @Documented @Inherited @Mapping public @interface CheckSession{ boolean isAuthenticate() default false; } 创建处理程序类 @Component public class CheckSessionClass implements HandlerMethodReturnValueHandler,HandlerMethodArgumentResolver { @Override public Object resolveArgument(MethodParameter arg0, ModelAndViewContainer arg1, NativeWebRequest arg2, WebDataBinderFactory arg3) throws Exception { logger.info(“……MY ANNOTATION CALLEDD…..resolveArgument”); return null; } @Override public boolean supportsParameter(MethodParameter arg0) { logger.info(“……MY ANNOTATION CALLEDD…..supportsParameter”); return false; […]

Spring 3简单的无扩展url映射与基于注释的映射 – 不可能?

我正在使用Spring 3,并尝试使用注释来设置一个简单的Web应用程序来定义控制器映射。 如果没有使用* .form或* .do对所有url进行操作,这似乎是非常困难的 由于网站的某些部分需要受密码保护,因此这些url都在/安全之下。 web.xml中有一个保护该根目录下的所有内容。 我想将所有Spring控制器映射到/ secure / app /。 示例url将是: /安全/应用/的LandingPage /安全/应用/编辑/客户/ {ID} 我会用适当的jsp / xml /其他方法来处理。 所以,在web.xml中我有这个: dispatcher org.springframework.web.servlet.DispatcherServlet 1 dispatcher /secure/app/* 在despatcher-servlet.xml中我有这个: 在Controller包中,我有一个控制器类: package controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest; @Controller @RequestMapping(“/secure/app/main”) public class HomePageController { public HomePageController() { } @RequestMapping(method = RequestMethod.GET) public ModelAndView […]

JSON plus spring mvc 3.2 error 415(不支持的媒体类型)

我做错了什么? 我尝试使用Spring mvc和JSON。 当我尝试调试我的代码时,我看起来javascript工作,但不适用于控制器。 在浏览器中,我收到错误415 Unsupported Media Type。 脚本: $(document).ready(function() { $(‘#newSmartphoneForm’).submit(function(event) { var producer = $(‘#producer’).val(); var model = $(‘#model’).val(); var price = $(‘#price’).val(); var json = { “producer” : producer, “model” : model, “price”: price}; $.ajax({ url: $(“#newSmartphoneForm”).attr( “action”), data: JSON.stringify(json), type: “POST”, beforeSend: function(xhr) { xhr.setRequestHeader(“Accept”, “application/json”); xhr.setRequestHeader(“Content-Type”, “application/json”); }, success: function(smartphone) […]

如何在关机时等待RestTemplate响应?

我正在使用带RestTemplate将POST请求发送到网络服务器。 当我的应用程序关闭时(例如从tomcat取消部署),应该延迟关闭,直到收到所有挂起的响应(在超时内)。 restTemplate在引擎盖下使用HttpComponentsClientHttpRequestFactory 。 问题:如何告诉spring延迟关机? @PreDestroy可能是一种可能,但我如何检测restTemplate上的待处理请求?

Spring @Autowired – 实例化新bean

需要一些Spring自动assembly和范围的帮助。 这是基本的app结构: 我有一个CustomHttpClient,注释为@Component,还从application.properties文件中提取一些与配置相关的属性(通过@Value注释)。 CustomHttpClient由我的应用程序中的多个服务使用。 每当我使用CustomHttpClient时,我通过以下方式自动assembly该实例: @Autowired private CustomHttpClient httpClient; 我使用拦截器来修改CustomHttpClient中的一些变量,如下所示: public class MyInterceptor extends HandlerInterceptorAdapter { @Autowired CustomHttpClient httpClient; @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { httpClient.setSomeProperty(newValue); … 现在,这是问题所在。 如果我按照上面的描述设置了所有内容,那么每当我通过拦截器更改CustomHttpClient的任何设置时,只要VM正在运行,就会为所有其他客户端保留该新值。 因此,当我运行httpClient.setSomeProperty()时 – 该设置现在已永久保存。 即使我从另一个客户端连接到该应用程序。 基本上我需要的是两件事: 仍然能够通过拦截器覆盖CustomHttpClient的默认设置(请求拦截器,通过配置)。 确保为每个请求创建一个新的CustomHttpClient实例(在拦截器执行其魔法之后)。 我尝试将CustomHttpClient的范围更改为@Scope(“prototype”),但这样我就无法再使用拦截器更改CustomHttpClient的设置。

使用AcceptHeaderLocaleResolver和i18n的Spring Security

我卡住了,可能在文档中遗漏了一些东西或犯了一些小错误。 Spring Security 3.0.5集成在我的Spring MVC 3.0.5应用程序中。 AcceptHeaderLocaleResolver用于区域设置检测和本地化工作正常,但安全性错误消息除外。 我从spring安全包复制了messages.properties并重命名并添加到带有值列表的现有“messageSource”bean(ResourceBundleMessageSource) 。 如前所述,所有文本和消息都已正确本地化,除了安全接缝以使用硬编码的英语消息。 任何想法如何解决这个问题? 更新: 我的xy-servlet.xml包含: … … defaultMessages securityMessages 和文件 defaultMessages.properties defaultMessages_en.properties defaultMessages_de.properties defaultMessages_sl.properties 和 securityMessages.properties securityMessages_en.properties securityMessages_de.properties securityMessages_sl.properties 但defaultMessages工作正常。 securityMessages没有。 我对所有securityMessages文件进行了小的更改,但忽略它们并显示硬编码的英语消息。 更新v2:我的dispatcher-servlet.xml: defaultMessages securityMessages org/springframework/security/messages_de

错误消息的顺序不正确

在我的春季MVCvalidation中,我的错误消息的顺序随机变化,我希望消息的顺序与它们在页面上显示的顺序相同。 我的AccountForm.java类看起来像: @NotNull(message = “Account name cannot be empty.”) @Size(min=3, max=50, message=”Account name must be between 3 and 50 characters long.”) private String accountName; @NotNull(message = “Company name cannot be empty.”) @Size(min=3, max=50, message=”Company name must be between 3 and 50 characters long.”) private String companyName; 我还在控制器中添加了一些自定义错误: public ModelAndView create(@Valid AccountForm accountForm, BindingResult bindingResult) { […]

Spring BindingResult错误

我收到以下错误消息: java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name ‘billingInfoCommand’ available as request attribute 我的JSP看起来像: 我的控制器看起来像: public class BillingInfoController extends FusionFormController { protected final Log logger = LogFactory.getLog(getClass()); private FieldAccessService fieldAccessService; public BillingInfoController(){ setCommandClass(CommonCommand.class); setCommandName(“billingInfoCommand”); } protected Map referenceData(HttpServletRequest request,Object command, Errors errors) throws Exception { CommonCommand commonCommand=(CommonCommand)command; HashMap refDataMap=new HashMap(); refDataMap.put(“billingTypes”,this.getLookupValsAsMap(“getBillingTypes”)); Long […]

没有XML Spring ApplicationContext

我正在尝试使用和不使用spring xml配置创建项目。 首先,我创建我的xml配置: 我创建了我的非XML配置(我不知道是否是正确的基础,因为我不知道如何调用它) package br.com.caelum.spring.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import br.com.caelum.stock.ProductManager; import br.com.caelum.stock.dao.MemoryProductDAO; import br.com.caelum.stock.dao.Persistable; import br.com.caelum.stock.model.Product; @Configuration public class AppConfig { @Bean public Persistable memoryProductDAO(){ return new MemoryProductDAO(); } @Bean public ProductManager memoryProductManager(){ return new ProductManager(memoryProductDAO()); } } 比我创建了一个JUnit测试: package br.com.caelum.stock; import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.assertThat; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; […]

整合springMVC和extjs

我在我当前的j2ee项目中使用springMVC和hibernate。 截至目前的视图由普通的jsp组成,使用JSTL使事情变得简单。 看看extjs项目,我相信它可以用作视图的替代品。 我一直在看extjs文档,但坦率地说,我不知道如何在spring整合它。 有没有人有任何教程,博客,书籍推荐,让我开始这个? 或者另一方面,如果从不同的java MVC框架更好地管理extjs,那么请提出相同的建议。