Tag: spring mvc

Spring @Value注释总是评估为null?

所以,我有一个简单的属性文件,其中包含以下条目: my.value=123 another.value=hello world 使用PropertyPlaceHolderConfigurer加载此属性文件,该文件引用上面的属性文件。 我有以下类,我正在尝试加载这些属性,如下所示: public class Config { @Value(“${my.value}”) private String mValue; @Value(“${another.value}”) private String mAnotherValue; // More below… } 问题是, mValue和mAnotherValue总是为空…但在我的控制器中,该值正在加载正常。 是什么赋予了?

Spring – 将一个URL重写为另一个URL

我有一个包含Flash横幅的Spring 2.5应用程序。 我没有Flash组件的源代码,但它有硬编码到以.html结尾的某些页面的链接。我希望能够将这些.html页面重定向到现有的jsp页面。 我怎样才能让Spring将几个.html页面解析为.jsp页面? 我的项目看起来像: WebContent | -sample.jsp -another.jsp WEB-INF | -myapp-servlet.xml -web.xml 我想localhost:8080/offers.html重定向到localhost:8080/sample.jsp 我可以用Spring做到这一点吗? 我已经在myapp-servlet.xml中定义了一个SimpleUrlHandlerMapping和UrlFilenameViewController,它必须继续为它已经存在的页面提供服务。 在我的web.xml中,我有 myapp *.htm 更新 这是URL映射器。 如果我添加控制器,如何返回WebContent目录中的jsp视图,因为视图解析器包含/ WEB-INF / jsp目录。 page1Controller page2Controller

如何让Spring MVC在JUnit测试中调用validation?

我有一个名为Browser的POJO,我用Hibernate Validator注释进行了注释。 import org.hibernate.validator.constraints.NotEmpty; public class Browser { @NotEmpty private String userAgent; @NotEmpty private String browserName; … } 我编写了以下unit testing,试图validation我的Controller方法是否捕获了validation错误。 @Test public void testInvalidData() throws Exception { Browser browser = new Browser(“opera”, null); MockHttpServletRequest request = new MockHttpServletRequest(); BindingResult errors = new DataBinder(browser).getBindingResult(); // controller is initialized in @Before method controller.add(browser, errors, request); assertEquals(1, errors.getErrorCount()); […]

在@RequestMapping方法中使用spring mvc中的哪种返回类型?

我知道在Spring的mvc中@RequestMapping方法中的@Controller类我可以返回 串 模型 的ModelAndView 我不明白这些行为之间的差异。 你能解释一下吗?

在Spring MVC中使用@JsonView

我正在使用以下bean定义来使我的spring应用程序在JSON中进行通信 这个消息转换器bean是否可以使用@JsonView注释?

Spring MVC @RequestMapping注释的不区分大小写映射

可能重复: 如何在带有注释映射的Spring MVC中使用不区分大小写的URL 我有控制器在其中有多个@RequestMapping注释。 @Controller public class SignUpController { @RequestMapping(“signup”) public String showSignUp() throws Exception { return “somejsp”; } @RequestMapping(“fullSignup”) public String showFullSignUp() throws Exception { return “anotherjsp”; } @RequestMapping(“signup/createAccount”) public String createAccount() throws Exception { return “anyjsp”; } } 如何将这些@RequestMapping映射到不区分大小写。 即如果我使用“/ fullsignup”或“/ fullSignup”,我应该得到“anotherjsp”。 但现在这种情况并没有发生。 只有“/ fullSignup”才能正常工作。 我试过扩展RequestMappingHandlerMapping但没有成功。 我也尝试过AntPathMatcher,就像在这个论坛上提到的另一个问题,但它也不适用于@RequestMapping注释。 调试控制台 服务器启动时的输出控制台。 我添加了两张显示问题的图片。 我已经尝试了下面提到的两种解决方案。 控制台说它映射了小写的URL,但是当我请求访问带有小写url的方法时,它表明存储值的原始映射包含MixCase URLS。

Spring welcome-file-list正确映射

我知道在spring我必须定义welcome-file,它应该在WEB-INF文件夹之外,所以我这样定义: web.xml中: index.jsp spring / 但实际上我的真实代码是在WEB-INF / jsp / contact.jsp中 所以我总是这样做: 在我的控制器中,这意味着: @RequestMapping(“/index”) public String listContacts(Map map) { map.put(“contact”, new Contact()); map.put(“contactList”, contactService.listContact()); return “contact”; } 我怎么能这样做,欢迎文件总是进入我的索引映射,这导致contact.jsp? 如果这令人困惑,请随意提问…

如何为单页AngularJS应用程序实现基本的Spring安全性(会话管理)

我目前正在构建一个单页AngularJS应用程序,它通过REST与后端进行通信。 结构如下: One Spring MVC WebApp项目,包含所有AngularJS页面和资源以及所有REST控制器。 一个真正的后端,具有用于后端通信的服务和存储库,如果您愿意,还可以使用API​​。 REST调用将与这些服务进行通信(第二个项目作为第一个项目的依赖项包含在内)。 我一直在考虑这个问题,但我似乎无法找到任何可以帮助我的东西。 基本上我只需要这个应用程序的一些安全性。 我想要某种非常简单的会话管理: 用户登录,会话ID创建并存储在网站上的JS / cookie中 当用户重新加载页面/稍后返回时,需要进行检查以查看会话ID是否仍然有效 如果会话ID无效,则呼叫不应到达控制器 这是基本会话管理的一般概念,在Spring MVC webapp中实现这一点的最简单方法是什么(没有JSP,只有角度和REST控制器)。 提前致谢!

无法获得通用的ResponseEntity ,其中T是generics类“SomeClass ”

请帮我一个ResponseEntity ,其中T本身就是一个generics类型。 正如我现在所看到的,现在spring的RestTemplate不支持这个。 我正在使用Spring MVC 3.1.2版 这是我想要使用的代码:代码: ResponseEntity<CisResponse> res = this.restTemplate.postForEntity( this.rootURL, myRequestObj, CisResponse.class); 我收到这个错误: Type mismatch: cannot convert from ResponseEntity to ResponseEntity<CisResponse> 这是明显的错误,但我今天如何解决它? 比我想要获得我的通用响应类型: CisResponse myResponse= res.getBody(); CisResponseEntity entity = myResponse.getEntityFromResponse(); 现在,我使用这个解决方案,使用postForObject()而不是postForEntity() : CisResponse response = this.restTemplate.postForObject( this.rootURL,myRequestObj, CisResponse.class);

Spring Boot安全性CORS

我对弹簧安全URL的CORSfilter有问题。 它不会在属于spring sec(登录/注销)的URL上设置Access-Control-Allow-Origin和其他公开的标头,也不会被Spring Security过滤。 这是配置。 CORS: @Configuration @EnableWebMvc public class MyWebMvcConfig extends WebMvcConfigurerAdapter { ********some irrelevant configs************ @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping(“/*”).allowedOrigins(“*”).allowedMethods(“GET”, “POST”, “OPTIONS”, “PUT”) .allowedHeaders(“Content-Type”, “X-Requested-With”, “accept”, “Origin”, “Access-Control-Request-Method”, “Access-Control-Request-Headers”) .exposedHeaders(“Access-Control-Allow-Origin”, “Access-Control-Allow-Credentials”) .allowCredentials(true).maxAge(3600); } } 安全: @Configuration @EnableWebSecurity public class OAuth2SecurityConfiguration extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { […]