Tag: spring mvc

找不到springframework的标签库描述符

我试图遵循spring JPetStore的例子,但是在引用lib标签spring的行中的JSP页面中出现错误: 找不到“http://www.springframework.org/tags”的标记库描述符 这个库的URL是什么? 有没有办法避免直接依赖这个URL? 提前致谢

为什么使用基于java的配置的Spring应用程序无法正常工作

我最近开始使用Spring框架开发一个带有ojective的项目来开发它,没有XML配置文件,只有Java代码。 在当前时刻,我将以下文件添加到我的项目中: WebAppConfig.java @EnableWebMvc @ComponentScan(value=”org.webapp”) @Configuration public class WebAppConfig extends WebMvcConfigurerAdapter { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler(“/css/**”).addResourceLocations(“/css/”).setCachePeriod(31556926); registry.addResourceHandler(“/css/**”).addResourceLocations(“/fonts/”).setCachePeriod(31556926); registry.addResourceHandler(“/img/**”).addResourceLocations(“/image/”).setCachePeriod(31556926); registry.addResourceHandler(“/js/**”).addResourceLocations(“/js/”).setCachePeriod(31556926); } @Override public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { configurer.enable(); } @Bean public InternalResourceViewResolver getInternalResourceViewResolver() { InternalResourceViewResolver resolver = new InternalResourceViewResolver(); resolver.setPrefix(“/WEB-INF/jsp/”); resolver.setSuffix(“.jsp”); return resolver; } } SecurityConfig.java @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter […]

在Spring中运行时注册bean(原型)

只需要社区评估的东西。 以下是一段代码,它是一个创建特定类型实例的简单工厂。 该方法将在上下文中将bean注册为原型并返回实例。 这是我第一次在运行时配置bean。 你能评价并提供反馈意见吗? 先谢谢你。 package au.com.flexcontacts.flexoperations; import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.config.ConstructorArgumentValues; import org.springframework.beans.factory.support.DefaultListableBeanFactory; import org.springframework.beans.factory.support.GenericBeanDefinition; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.context.support.AbstractApplicationContext; import au.com.flexcontacts.exceptions.SyncClassCreactionError; /** * @author khushroo.mistry * Class purpose: Simple Factory to create an * instance of SynchroniseContactsService and register it in the Spring IoC. */ public final class FLEXSyncFactory implements […]

Multipart文件上传Spring Boot

我使用Spring Boot并希望使用Controller来接收多部分文件上传。 发送文件时,我不断收到错误415不支持的内容类型响应,并且永远不会到达控制器 There was an unexpected error (type=Unsupported Media Type, status=415). Content type ‘multipart/form-data;boundary=—-WebKitFormBoundary1KvzQ1rt2V1BBbb8’ not supported 我尝试使用form:action在html / jsp页面中以及在使用RestTemplate的独立客户端应用程序中发送。 所有尝试都给出了相同的结果 multipart/form-data;boundary=XXXXX not supported. 从多部分文档看来,边界参数必须添加到分段上传,但这似乎与接收”multipart/form-data”的控制器不匹配 我的控制器方法设置如下 @RequestMapping(value = “/things”, method = RequestMethod.POST, consumes = “multipart/form-data” , produces = { “application/json”, “application/xml” }) public ResponseEntity submitThing(HttpServletRequest request, @PathVariable(“domain”) String domainParam, @RequestParam(value = “type”) String thingTypeParam, @RequestBody […]

具有Spring Security和Java配置的自定义validation管理器

我正在使用Spring Security与SpringMVC创建一个Web应用程序(为了清楚起见,我将其称为WebApp),它与现有应用程序相对应(我将其称为BackendApp)。 我想将身份validation职责委派给BackendApp(这样我就不需要同步这两个应用程序)。 为了实现这一点,我希望WebApp(运行Spring安全性)通过REST与用户在表单中提供的用户名和密码与BackendApp进行通信,并根据BackendApp的响应是200 OK还是401 Unauthorized进行身份validation。 我知道我需要编写一个自定义的身份validation管理器来执行此操作,但我是一个非常新的spring,无法找到有关如何实现它的任何信息。 我相信我需要做这样的事情: public class CustomAuthenticationManager implements AuthenticationManager{ @Override public Authentication authenticate(Authentication authentication) throws AuthenticationException { String username = authentication.getName(); String pw = authentication.getCredentials().toString(); // Code to make rest call here and check for OK or Unauthorised. // What do I return? } } 如果成功,我是否设置authentication.setAuthenticated(true),否则设置为false,那就是它? 编写完成后,如何使用java配置文件配置spring security以使用此身份validation管理器? 在此先感谢您的任何帮助。

如何在Spring MVC中将请求映射到HTML文件?

基本配置文件看起来不直观。 如果我创建简单的hello world示例,然后将home.jsp重命名为home.html并编辑servlet-context.xml文件 至 我开始出错了 WARN : org.springframework.web.servlet.PageNotFound – No mapping found for HTTP request with URI [/myapp/WEB-INF/views/home.html] in DispatcherServlet with name ‘appServlet’ 为什么? 什么suffix属性意味着什么? UPDATE 我的控制器如下。 如您所见,它不包含文件扩展名 @Controller public class HomeController { private static final Logger logger = LoggerFactory.getLogger(HomeController.class); /** * Simply selects the home view to render by returning its name. */ @RequestMapping(value […]

如何从BindingResult获取控制器中的错误文本

我有一个返回JSON的控制器。 它采用一种forms,通过弹簧注释validation自己。 我可以从BindingResult获取FieldError列表,但它们不包含JSP将在标记中显示的文本。 如何通过JSON发送错误文本? @RequestMapping(method = RequestMethod.POST) public @ResponseBody JSONResponse submit(@Valid AnswerForm answerForm, BindingResult result, Model model, HttpServletRequest request, HttpServletResponse response) { if (result.hasErrors()) { response.setStatus(HttpServletResponse.SC_BAD_REQUEST); JSONResponse r = new JSONResponse(); r.setStatus(JSONResponseStatus.ERROR); //HOW DO I GET ERROR MESSAGES OUT OF BindingResult??? } else { JSONResponse r = new JSONResponse(); r.setStatus(JSONResponseStatus.OK); return r; } } JSONREsponse类只是一个POJO […]

Spring 3.0使用jackson消息转换器进行JSON响应

我将我的messageconverter配置为jackson的 class Foo{int x; int y} 并在控制器中 @ResponseBody public Foo method(){ return new Foo(3,4) } 从那个我期望从服务器返回一个JSON字符串{x:’3’,y:’4′},没有任何其他配置。 但得到我的ajax请求的404错误响应 如果使用@ResponseBody注释该方法,则将返回类型写入响应HTTP正文。 返回值将使用HttpMessageConverters转换为声明的方法参数类型。 我错了吗 ? 或者我应该使用序列化程序将我的响应对象转换为Json字符串,然后将该字符串作为响应返回。(我可以正确地进行字符串响应)或者我应该进行其他配置吗? 比如为Foo类添加注释 这是我的conf.xml

Spring部分更新对象数据绑定

我们正在尝试在Spring 3.2中实现一个特殊的部分更新function。 我们使用Spring作为后端,并有一个简单的Javascript前端。 我无法找到满足我们要求的直接解决方案,即update()函数应该采用任意数量的字段:值并相应地更新持久性模型。 我们对所有字段进行内联编辑,因此当用户编辑字段并确认时,id和修改后的字段将作为json传递给控制器​​。 控制器应该能够从客户端(1到n)接收任意数量的字段并仅更新这些字段。 例如,当id == 1的用户编辑他的displayName时,发布到服务器的数据如下所示: {“id”:”1″, “displayName”:”jim”} 目前,我们在UserController中有一个不完整的解决方案,如下所述: @RequestMapping(value = “/{id}”, method = RequestMethod.POST ) public @ResponseBody ResponseEntity update(@RequestBody User updateUser) { dbUser = userRepository.findOne(updateUser.getId()); customObjectMerger(updateUser, dbUser); userRepository.saveAndFlush(updateUuser); … } 这里的代码有效,但有一些问题: @RequestBody创建一个新的updateUser ,填写id和displayName 。 CustomObjectMerger将此updateUser与数据库中相应的CustomObjectMerger合并,更新updateUser包含的唯一字段。 问题是Spring使用默认值和其他自动生成的字段值填充updateUser的某些字段,这些字段值在合并时会覆盖我们在dbUser有效数据。 明确声明它应该忽略这些字段不是一种选择,因为我们希望我们的update也能够设置这些字段。 我正在寻找一些方法让Spring自动合并显式发送到update()函数的信息到dbUser (不重置默认/自动字段值)。 有没有简单的方法来做到这一点? 更新:我已经考虑过以下选项,它几乎可以满足我的要求,但并不完全。 问题是它将更新数据作为@RequestParam并且(AFAIK)不执行JSON字符串: //load the existing user into the model for injecting […]

org.hibernate.HibernateException:找不到/hibernate.cfg.xml

我正在尝试使用spring 3 mvc的hibernate但是此刻我抛出了这个exception。 我想我需要在某处定义我的hibernate.cfg.xml ,但不确定在哪里? 我基本上按照这个例子http://www.nabeelalimemon.com/blog/2010/05/spring-3-integrated-with-hibernate-part-a/特别是看到这行代码,假设“神奇地” “使用这个找到我的hibernate.cfg文件: return new Configuration().configure().buildSessionFactory(); 我猜这不对吗? 我目前在src/com/jr/hibernate/有我的hibernate.cfg文件 下面是我的cfg文件: com.mysql.jdbc.Driver jdbc:mysql://localhost:3306/racingleague username password true 1 org.hibernate.dialect.MySQLDialect thread org.hibernate.cache.NoCacheProvider true update 我的hibernate utils类: package com.jr.utils; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; public class HibernateUtils { private static final SessionFactory sessionFactory = buildSessionFactory(); public static SessionFactory buildSessionFactory() { try { // Create the SessionFactory from […]