CommandLineRunner 里得到的数据页面接收不到

方法没问题,但获取到的值传不过去

 @Component
public class JobCLR implements CommandLineRunner  {
     protected final Logger logger = LoggerFactory.getLogger(JobCLR.class);
  @Autowired
        private JobService js;
      @Override
        public void run(String... args) throws Exception {
           System.out.println(">>>>>>>>>>>>>>>服务启动执行,执行加载数据等操作<<<<<<<<<<<<<");
          /* List<String> jobsListindex=js.queryHotJob(1);*/
        List<Jobs>  jobsList= js.selectByHeat1();
           ModelAndView mav =new ModelAndView("/");   
           mav.addObject("jobsList",jobsList);

           for(Jobs l :jobsList) {
           System.out.println(l);

           }
        } 

}

页面

 <a class="zxzw_fen clearfix"
                        th:classappend="${ jobsListindex.index } % 2 == 0  ? '' : 'mr_0'"
                        href="" th:each="jobsList,jobsListindex:${jobsList}">
         <div class="zxzw_fen_01 clearfix">

    <b th:utext="|<i>|+${jobsList.jobPosition}+|</i>&nbsp;[|+${jobsList.jobAddress}+|]&nbsp;&nbsp;${jobsList.jobReleaseTime}|"></b>
                 <span th:text="${jobsList.jobSalary}">24-36万</span>
         </div>
         <div class="zxzw_fen_02 clearfix">
                 <span  th:text="${jobsList.jobEmployAddress}"></span>
         <i  th:text="${jobsList.jobWelfare}">绩效奖金</i> <i>带薪年假</i>
                        </div>
                    </a>

3个回答

以配置文件为application.properties视图为jsp举例

** 在application.properties配置springmvc**

#springmvc
spring.mvc.view.prefix                                    = /WEB-INF/views/
spring.mvc.view.suffix                                    = .jsp

index.jsp 代码举例(哈哈,最简单jsp)

 ${data}

springboot启动代码举例


@Controller
@SpringBootApplication
public class Application implements CommandLineRunner
{
    private static final Logger LOGGER = LoggerFactory.getLogger(Application.class);

    //注入service 举例
    @Autowired
    CustomerService customerService;

    public static void main(String[] args)
    {
        SpringApplication.run(Application.class, args);
    }

    @Override
    public void run(String... args)
        throws IOException
    {
        LOGGER.info("★★★★★★★★  now open Browser ★★★★★★★★ ");
        //windows系统下自动打开浏览器,可根据需要调整url
        Runtime.getRuntime().exec("cmd.exe /c start /min http://127.0.0.1:8080/");
    }

    @RequestMapping(value = "/")
    public ModelAndView index()
    {
        ModelAndView modelAndView = new ModelAndView("index");
        modelAndView.addObject("data", customerService.getAllCustomers());
        return modelAndView;
    }
}

运行效果 图片说明

回答完毕, 希望帮到你 !
本人码云 https://gitee.com/00fly/ 欢迎访问!

qq_37852473
qq_37852473 谢谢老哥
接近 2 年之前 回复

思路二:** jsp里面直接操作**(已经调试通过)

<%@page import="com.fly.hbn.service.CustomerService"%>
<%@page import="org.springframework.context.ApplicationContext"%>
<%@page import="org.springframework.web.context.support.WebApplicationContextUtils"%>
<%
    ApplicationContext content = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
    CustomerService customerService = content.getBean(CustomerService.class);
    request.setAttribute("data2", customerService.getAllCustomers());
%>

<hr>
${data2}

运行结果
图片说明

回答完毕,希望能帮到你!

如果是springboot项目的话其实可以

 @RequestMapping(value="/") 

以达到一开始页面便能加载数据
虽然low了点

qq_16127313
爱码少年 controller可移到别的地方, 这边这是方便演示,重点是打开浏览器url代码
接近 2 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐