我的控制器有一个Autowired字段" resultados"
@Controller
public class MyController {
@Autowired
private CursoDelegate cursoDelegado;
@Autowired
private List<Articulo> resultados;
@RequestMapping(value = "consultarArticulos.html")
public ModelAndView consultarArticulos() {
ModelAndView modelAndView= new ModelAndView("consultarArticulos");
modelAndView.addObject("resultados", resultados);
return modelAndView;
}
我在配置类中定义了bean。方法&#34; cursoDelegado.consultarArticulos()&#34;返回三个元素的集合,但bean始终为空。还有其他课程,但这里不需要
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.myapp")
public class MyConfig extends WebMvcConfigurerAdapter {
@Bean
public List<Articulo> resultados()
{
try {
return cursoDelegado.consultarArticulos();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
}
抱歉,我没有粘贴所有代码,但应用程序运行并声明了所有bean。问题是&#34; resultados&#34;
我更改了代码:
@Autowired //MyControllerClass
private @Resource(name="bean1")List<Articulo> resultados;
@Bean(name="bean1")//MyConfigClass
public List<Articulo> resultados()
现在好了。接口列表有任何问题,但我不知道原因。如果有人能解释我的问题:)
答案 0 :(得分:0)
@Bean
public List<Articulo> resultados(CursoDelegate cursoDelegado)
{
try {
List<Articulo> resultados = new ArrayList<Articulo>();
resultados = cursoDelegado.consultarArticulos();
return resultados;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
答案 1 :(得分:0)
cursoDelegado ,因此 cursoDelegado.consultarArticulos()将始终抛出NullPointerException。你抓住这个,所以它在你的应用程序中是不可见的。控制台输出( e.printStackTrace )应该在服务器日志中可见。
最好直接在注入 cursoDelegado 的控制器中使用 cursoDelegado.consultarArticulos()。
答案 2 :(得分:0)
<?php
$cat = get_terms('category');
foreach ($cat as $catVal) {
echo '<h2>'.$catVal->name.'</h2>';
$postArg = array('post_type'=>'post','posts_per_page'=>-1,'order'=>'desc',
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'term_id',
'terms' => $catVal->term_id
)
));
$getPost = new wp_query($postArg);
global $post;
if($getPost->have_posts()){
echo '<ul>';
while ( $getPost->have_posts()):$getPost->the_post();
echo "<li>".$post->post_title."</li>";
endwhile;
echo '</ul>';
}
}
?>