2017-08-13 100 views
1

我是java spring框架的新手,我试图调用一个方法或在启动时初始化一个方法,所以当应用程序启动时,我将能够从数据库在我的jsp页面上。我尝试了@EventListener,@PostConstruct,但没有任何工作适合我。Java初始化,在启动时调用方法

这是我WebConfigura类

@Configuration 
@ComponentScan("com.store.spring") 
@EnableWebMvc 
public class WebConfiguration extends WebMvcConfigurerAdapter { 

@Bean 
public DataSource dataSource() { 
    final JndiDataSourceLookup lookup = new JndiDataSourceLookup(); 
    lookup.setResourceRef(true); 
    DataSource dataSource = lookup.getDataSource("jdbc/product_db"); 

    return dataSource; 
} 

@Bean 
public UrlBasedViewResolver resolver() { 
    UrlBasedViewResolver viewResolver = new UrlBasedViewResolver(); 
    viewResolver.setPrefix("/WEB-INF/views/"); 
    viewResolver.setSuffix(".jsp"); 
    viewResolver.setViewClass(JstlView.class); 

    return viewResolver; 
} 

@Override 
public void addViewControllers(ViewControllerRegistry registry) { 
    registry.addViewController("/").setViewName("index"); 
} 

@Override 
public void addResourceHandlers(ResourceHandlerRegistry registry) { 
    registry.addResourceHandler("/resource/css/**").addResourceLocations("/css/").setCachePeriod(31556926); 
} 

@Override 
public void configureDefaultServletHandling (DefaultServletHandlerConfigurer configurer) { 
    configurer.enable(); 
} 

@Override 
public void addInterceptors(InterceptorRegistry registry) { 
    registry.addInterceptor(new HeaderInterceptor()); 
} 
} 

这是Web应用程序初始化器类

@Component 
public class WebInitializer implements WebApplicationInitializer { 

@Override 
public void onStartup(ServletContext servletContext) throws ServletException { 
    AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); 
    context.register(WebConfiguration.class); 
    // Add context.setCongiglocation("/WEB-INF/spring/dispatcher-config.xml"); 

    ServletRegistration.Dynamic registration = servletContext.addServlet("dispatcher", new DispatcherServlet(context)); 
    registration.addMapping("/"); 
    registration.setLoadOnStartup(1); 

    ContextLoaderListener listener = new ContextLoaderListener(context); 
    servletContext.addListener(listener); 
} 
} 

这是控制器类

@Controller 
public class ProductController { 

@Autowired 
private ProductService productService; 

@RequestMapping("/index") 
public String index(Model model) { 
    List<Product> listProduct = productService.getPrducts(); 
    model.addAttribute("listProduct", listProduct); 

    return "index"; 
} 

// Request mapping using SQL tag 
@RequestMapping("/location") 
public String addLocation(Model model) { 
    List<Product> listProduct = productService.getPrducts(); 
    model.addAttribute("listProduct", listProduct); 

    return "location"; 
} 

@RequestMapping("/entry") 
public String entry() { 
    return "Entry"; 
} 

@RequestMapping("/aboutUs") 
public String aboutUS() {return "AboutUs";} 

@RequestMapping("/deal") 
public String deal() { 
    return "Deal"; 
} 

@RequestMapping("/listProduct") 
public String listOrganizationService(Model model) { 
    List<Product> listProduct = productService.getPrducts(); 
    model.addAttribute("listProduct", listProduct); 

    return "ListOrganizations"; 
} 
} 

这是标题拦截器类

public class HeaderInterceptor extends HandlerInterceptorAdapter { 

@Override 
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) 
     throws Exception { 
    request.setAttribute("title", "We hope you have a scary and fun filled halloween"); 
    String location = request.getParameter("locationName"); 
    if (location != null) { 
     request.setAttribute("locationName", location); 
    } 
    return true; 
} 
} 

这是我想初始化方法或呼叫在启动。或者至少 一些东西接近这个

@RequestMapping("/index") 
public String index(Model model) { 
List<Product> listProduct = productService.getPrducts(); 
model.addAttribute("listProduct", listProduct); 

return "index"; 
} 

我使用的index.jsp调用listProduct打印我的数据出来。注意index.jsp是应用程序启动时调用的第一个jsp。我希望它AB能够自动打印这些数据进行

<div class="main-container" style="text-align: center"> 
<c:forEach var="row" items="${listProduct}"> 
    <div class="detail"> 
     <td>ID: <c:out value="${row.id}"></c:out></td> 
     <br/> 
     <td>Name: <c:out value="${row.productName}"></c:out></td> 
     <br/> 
     <td>Quantity: <c:out value="${row.quantity}"></c:out></td> 
     <br/> 
     <td>Price: <c:out value="${row.price}"></c:out></td> 
     <br/> 
     <td><div class="few-line"><c:out value="${row.description}"></c:out></div></td> 
    </div> 
</c:forEach> 

我希望有人能够帮助我。

例如,我有这个代码我WebInitializer类里面,当我运行它,我得到一个错误信息

@EventListener(ContextRefreshedEvent.class) 
public void contextRefreshedEvent(Model model) { 
    List<Product> productList = productService.getPrducts(); 
    model.addAttribute("productList", productList) 
} 

产生的原因:java.lang.IllegalArgumentException异常:参数type sunmatch at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect。 DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 在java.lang.reflect.Method.invoke(Method.java:498) 在org.springframework.context.event.ApplicationListenerMethodAdapter.doInvoke(ApplicationListenerMethodAdapter.java:256) 。 .. 56更多 org.apache.catalina.core.StandardContext。startInternal一个或多个 听众未能启动。 org.apache.catalina.core.StandardContext.startInternal语境[] 启动失败,原因是 org.springframework.web.context.support.AnnotationConfigWebApplicationContext.doClose 结束Root WebApplicationContext的前期差错:启动日期[太阳8月13日10:43 :51 EDT 2017];上下文结构的根

这些都是我的插件和依赖我的罐子

<build> 
    <plugins> 
     <plugin> 
      <artifactId>maven-compiler-plugin</artifactId> 
      <version>3.6.1</version> 
      <configuration> 
       <source>1.8</source> 
       <target>1.8</target> 
      </configuration> 
     </plugin> 
     <plugin> 
      <artifactId>maven-war-plugin</artifactId> 
      <version>3.0.0</version> 
      <configuration> 
       <warSourceDirectory>WebContent</warSourceDirectory> 
      </configuration> 
     </plugin> 
    </plugins> 
</build> 
<dependencies> 
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc --> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-webmvc</artifactId> 
     <version>4.3.10.RELEASE</version> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-jdbc</artifactId> 
     <version>4.3.10.RELEASE</version> 
    </dependency> 
    <!-- https://mvnrepository.com/artifact/jstl/jstl --> 
    <dependency> 
     <groupId>jstl</groupId> 
     <artifactId>jstl</artifactId> 
     <version>1.2</version> 
    </dependency> 

    <dependency> 
     <groupId>mysql</groupId> 
     <artifactId>mysql-connector-java</artifactId> 
     <version>6.0.6</version> 
    </dependency> 
    <dependency> 
     <groupId>commons-dbcp</groupId> 
     <artifactId>commons-dbcp</artifactId> 
     <version>1.4</version> 
    </dependency> 
</dependencies> 
+2

欢迎来到Stack Overflow!我会想象这95%的代码与你的问题无关。请创建一个[**最小**,完整且可验证的示例](http://stackoverflow.com/help/mcve),以说明您的问题。 –

+0

“没有什么对我有用的。” - 这看起来像什么? – duffymo

回答

0

Java的春天初始化,呼吁启动的方法

您可以标注一个春天Bean的方法@PostConstruct让Spring在调用它之后调用它。您也可以使用@Autowired注解一个方法,让Spring在构建bean时调用它。我们通常使用其中之一在启动时调用方法,例如将数据从数据库加载到缓存中。

相关问题