求解,jsp页面读取不到modelandview的数据!-问答-阿里云开发者社区-阿里云

开发者社区> 问答> 正文

求解,jsp页面读取不到modelandview的数据!

a123456678 2016-03-18 14:11:05 2786

java 代码 ,结果显示的页面在另一个方法下的返回值处:

 public ModelAndView getAllMobile(String result1) {
         //Map<String, String> model =new  HashMap<String, String>();
        ModelAndView modelAndView = new ModelAndView();
    //     modelAndView.setViewName("shuchu");
        Connection conn=null;
        try {            
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","");
            String strSql = "select * from mobile where number=?" ;        
            PreparedStatement ps = conn.prepareStatement(strSql);
            ps.setString(1, result1);                    
            ResultSet result = ps.executeQuery();
            
        while(result.next()){
            System.out.println(result.getString("number"));
            //model.addAttribute("aa",result.getString("number"));
             //model.addAttribute("bb",result.getString("local"));
             //model.addAttribute("cc",result.getString("type"));
             //model.addAttribute("dd",result.getString("description"));
            // model.addAttribute("ee",result.getString("code"));
            //model.put("ee",result.getString("code") );
            modelAndView.addObject("ee", result.getString("code"));
            System.out.println(result.getString("code"));
        }
            conn.close();
        } catch (SQLException e) {        
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return modelAndView; 
}

JSP页面,因为大体相似,就把与程序对应的贴出来:

<%@ page isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>  
<tr>
              <td>所属地区邮编:</td>
              <td>${ee}${code}e</td>
          </tr>      

每次结果都是显示"e",我都醉了

jsp数据 jsp取不到 model页面 view页面 demo页面
分享到
取消 提交回答
全部回答(1)
  • a123456678
    2019-07-17 19:06:33

    改了你的部分代码,自己添加了数据库表,我这边可以显示。

    controller控制器代码

    public class TestController implements Controller {
     
        @Override
        public ModelAndView handleRequest(HttpServletRequest req, HttpServletResponse resp) {
            //参数通过request获取,请求http://localhost:8080/xx/test?phoneNumber=18888888888
            String phoneNumber = req.getParameter("phoneNumber");
            ModelAndView modelAndView = new ModelAndView();
            Connection conn;
            //链接数据库,查询数据,设值
            try {
                Class.forName("com.mysql.jdbc.Driver");
                conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "123456");
                String strSql = "select * from mobile where number=?" ;
                PreparedStatement ps = conn.prepareStatement(strSql);
                ps.setString(1, phoneNumber);
                ResultSet result = ps.executeQuery();
     
                while(result.next()){
                    System.out.println(result.getString("number"));
                    modelAndView.addObject("ee", result.getString("code"));
                    System.out.println(result.getString("code"));
                }
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
            //spring mvc 的jsp路径
            modelAndView.setViewName("task/hello");
            return modelAndView;
        }
    }
    0 0
数据库
使用钉钉扫一扫加入圈子
+ 订阅

分享数据库前沿,解构实战干货,推动数据库技术变革

相似问题
最新问题
推荐课程