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",我都醉了
改了你的部分代码,自己添加了数据库表,我这边可以显示。
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;
}
}
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。