我是java spring框架的新手,我试图在启动时调用方法或初始化方法,所以当应用程序启动时,我将能够在jsp页面上看到数据库中的数据。我试过@EventListener,@ PostConstruct,但没有什么对我有用。
这是我的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 Application Initializer类
@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。我希望它能够自动打印出这些数据
<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:参数类型不匹配 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at 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 关闭Root WebApplicationContext:启动日期[Sun Aug 13 10:43:51 EDT 2017];上下文层次结构的根
这些是我的jar的插件和依赖项
<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>
答案 0 :(得分:0)
Java Spring初始化,在启动时调用方法
你可以用Artist
注释一个Spring bean的方法,让Spring在构造之后调用它。您还可以使用@PostConstruct
注释一个方法,以便在构造该bean期间让Spring调用它。我们通常使用其中一个来在启动时调用方法,例如将数据从数据库加载到缓存中。