Tag: spring mvc

如何使用spring MVC在JSP中包含js和CSS

我想在我的jsp中包含js和css文件,但我无法这样做。 我是Spring MVC概念的新手。 很长一段时间,我一直在研究同一个话题。 我的索引页面是这样的 body { background-image: url(“LoginPageBackgroundImage.jpg”); } Please login in google Chrome Welcome to my Twitter Clone Login User Name Password New User? Signup 我的spring-dispatcher-servlet.xml是这样的。 <!—-> <!– –> /WEB-INF/ .jsp 我的控制器是这样的。 package com.csc.student; 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; @Controller public class StudentInfoController { @RequestMapping(value = “/indexPage.html”, method = RequestMethod.GET) […]

使用Hibernate JPA 2.1将应用程序部署到IBM WebSphere会产生NullPointerException

我们正在尝试使用Hibernate将Java 7 Spring MVC应用程序部署到IBM WebSphere 8.5.5.2服务器中。 应用程序在Tomcat服务器上运行良好,但我们无法在WebSphere中运行它。 堆栈跟踪输出如下: [3/26/15 13:49:53:552 MDT] 00000066 AutowiredAnno I org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor JSR-330 ‘javax.inject.Inject’ annotation found and supported for autowiring [3/26/15 13:49:54:299 MDT] 00000066 LocalContaine I org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean createNativeEntityManagerFactory Building JPA container EntityManagerFactory for persistence unit ‘default’ [3/26/15 13:49:54:339 MDT] 00000066 LogHelper Z org.hibernate.jpa.internal.util.LogHelper logPersistenceUnitInformation HHH000204: Processing PersistenceUnitInfo [ name: default …] [3/26/15 […]

在SpringMVC中使用@ResponseBody返回JsonObject

我在SpringMVC项目中使用新的Java API(JSR 353)来实现JSON。 我们的想法是生成一些Json数据并将其返回给客户端。 控制器我看起来有点像这样: @RequestMapping(“/test”) @ResponseBody public JsonObject test() { JsonObject result = Json.createObjectBuilder() .add(“name”, “Dade”) .add(“age”, 23) .add(“married”, false) .build(); return result; } 当我访问它时,而不是获得JSON的预期表示,我得到这些: {“name”:{“chars”:”Dade”,”string”:”Dade”,”valueType”:”STRING”},”age”:{“valueType”:”NUMBER”,”integral”:true},”married”:{“valueType”:”FALSE”}} 为什么是这样? 到底是怎么回事? 如何让它正确返回预期的JSON?

使用Spring Rest模板+ Spring Web MVC进行多部分文件上载

我试图使用RestTemplate上传一个文件与以下代码。 MultiValueMap multipartMap = new LinkedMultiValueMap(); multipartMap.add(“file”, new ClassPathResource(file)); HttpHeaders headers = new HttpHeaders(); headers.setContentType(new MediaType(“multipart”, “form-data”)); HttpEntity<MultiValueMap> request = new HttpEntity<MultiValueMap>(multipartMap, headers); System.out.println(“Request for File Upload : ” + request); ResponseEntity result = template.get().exchange( contextPath.get() + path, HttpMethod.POST, request, byte[].class); 我有MultipartResolver bean和Controller代码 @RequestMapping(value = “/{id}/image”, method = RequestMethod.POST) @ResponseStatus(HttpStatus.NO_CONTENT) @Transactional(rollbackFor = Exception.class) public byte[] […]

注册为Spring bean时,过滤调用两次

我想将@Autowire与Filter一起使用。 所以我在SecurityConfig定义了我的filter,如下所示: @Override protected void configure(HttpSecurity http) throws Exception { http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS); http.addFilterBefore(getA(), BasicAuthenticationFilter.class); http.csrf().disable(); } @Bean public A getA(){ return new A(); } 这个filterA扩展了Spring的GenericFilterBean 。 当我调用控制器时,我得到低于输出,这显示filter命中两次。 filter A before filter A before mycontroller invoke filter A after filter A after 我的观察是,这个额外的调用使用Spring容器调用,因为如果filter没有注册为bean,它只会被命中一次。 是什么原因,我该如何解决?

是什么导致“java.lang.IllegalStateException:BindingResult和bean name’命令的普通目标对象’都不可用作请求属性”?

对于这些类型的问题,这是一个广泛的规范问答post。 我正在尝试编写一个Spring MVC Web应用程序,用户可以在其中添加电影名称到内存中的集合。 它的配置是这样的 public class Application extends AbstractAnnotationConfigDispatcherServletInitializer { protected Class[] getRootConfigClasses() { return new Class[] {}; } protected Class[] getServletConfigClasses() { return new Class[] { SpringServletConfig.class }; } protected String[] getServletMappings() { return new String[] { “/” }; } } 和 @Configuration @ComponentScan(“com.example”) public class SpringServletConfig extends WebMvcConfigurationSupport { @Bean public InternalResourceViewResolver […]

Spring Security:多个HTTP配置不起作用

我正在尝试使用Spring Security,我有一个用例,我想要保护不同的登录页面和不同的URL集。 这是我的配置: @Configuration @Order(1) public static class ProviderSecurity extends WebSecurityConfigurerAdapter{ @Override protected void configure(HttpSecurity http) throws Exception { http .authorizeRequests() .antMatchers(“/”, “/home”).permitAll() .antMatchers(“/admin/login”).permitAll() .antMatchers(“/admin/**”).access(“hasRole(‘BASE_USER’)”) .and() .formLogin() .loginPage(“/admin/login”).permitAll() .defaultSuccessUrl(“/admin/home”) .failureUrl(“/admin/login?error=true”).permitAll() .usernameParameter(“username”) .passwordParameter(“password”) .and() .csrf() .and() .exceptionHandling().accessDeniedPage(“/Access_Denied”); } } @Configuration @Order(2) public static class ConsumerSecurity extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { […]

如何使用hibernate生成Custom Id,同时它必须是表的主键

这是我的pojo课程 @Entity public class Department { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) @Column(name=”Department_Id”) private Integer deptId; @Column(name=”Department_Name”,unique=true,nullable=false) private String deptName; @Column(name=”Department_Description”) @NotNull private String deptDesc; //geters and setters 我想要的是department_id必须是此Department表的主键,此键的条目必须为DEP0001,DEP0002,DEP0003