JavaSpring初始化,在启动时调用方法
我是JavaSpring框架的新手,我正在尝试在启动时调用一个方法或初始化一个方法,这样当应用程序启动时,我将能够在jsp页面上看到数据库中的数据。我尝试过@EventListener、@PostConstruct,但没有任何效果 这是我的网络配置课程 这是Web应用程序初始值设定项类 这是控制器类 这是我希望在启动时初始化或调用的方法。或者至少 一些与此相近的东西JavaSpring初始化,在启动时调用方法,java,spring,jsp,spring-mvc,Java,Spring,Jsp,Spring Mvc,我是JavaSpring框架的新手,我正在尝试在启动时调用一个方法或初始化一个方法,这样当应用程序启动时,我将能够在jsp页面上看到数据库中的数据。我尝试过@EventListener、@PostConstruct,但没有任何效果 这是我的网络配置课程 这是Web应用程序初始值设定项类 这是控制器类 这是我希望在启动时初始化或调用的方法。或者至少 一些与此相近的东西 @RequestMapping(“/index”) 公共字符串索引(模型){ List listProduct=productSe
@RequestMapping(“/index”)
公共字符串索引(模型){
List listProduct=productService.getPrducts();
model.addAttribute(“listProduct”,listProduct);
返回“索引”;
}
我正在使用index.jsp调用listProduct来打印数据。请注意,index.jsp是应用程序启动时调用的第一个jsp。我希望它能够自动打印这些数据
身份证件:
姓名:
数量:
价格:
我希望有人能帮助我
例如,我的WebInitializer类中有这段代码,当我运行它时,我收到一条错误消息
@EventListener(ContextRefreshedEvent.class)
public void上下文刷新事件(模型){
List productList=productService.getPrducts();
model.addAttribute(“productList”,productList)
}
原因:java.lang.IllegalArgumentException:参数类型不匹配
在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处
位于sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
在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 Context[]
由于以前的错误,启动失败
org.springframework.web.context.support.AnnotationConfigWebApplicationContext.doClose
正在关闭根WebApplicationContext:启动日期[星期日8月13日10:43:51]
美国东部时间2017年];上下文层次结构的根
这些是我的插件和jar的依赖项
maven编译器插件
3.6.1
1.8
1.8
maven战争插件
3.0.0
网络内容
org.springframework
SpringWebMVC
4.3.10.1发布
org.springframework
SpringJDBC
4.3.10.1发布
jstl
jstl
1.2
mysql
mysql连接器java
6.0.6
公共dbcp
公共dbcp
1.4
JavaSpring初始化,在启动时调用方法
您可以使用
@PostConstruct
注释Springbean的方法,让Spring在构造后调用它。您还可以使用@Autowired
注释一个方法,让Spring在构建该bean期间调用它。我们通常使用其中一种方法在启动时调用方法,例如将数据从数据库加载到缓存中。欢迎使用堆栈溢出!我想这段代码的95%与您的问题无关。请创建一个演示您的问题的示例。“没有任何东西对我有效。”-这看起来像什么?
@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());
}
}
@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";
}
<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>
@EventListener(ContextRefreshedEvent.class)
public void contextRefreshedEvent(Model model) {
List<Product> productList = productService.getPrducts();
model.addAttribute("productList", productList)
}
<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>