jsp传值到后台springmvc接收
@RequestMapping("/logindo")
public String dologin(String username, String password, Map<String, Object> map) {
User user=userService.getUser(username);
if(user!=null){
if(user.getName().equals(username)&&user.getPassword().equals(password)){
return "dashboard";
}else{
return "login";
}
}
return "error";
}
springmvc接口传值
@RequestMapping(value = "/newsadda")
public void newsadd1(HttpServletRequest request, HttpServletResponse response) {
User user=new User();
user.setId(1);
user.setAddress("QQQQQ");
user.setAge("22");
user.setName("admin");
user.setPhone("111111111");
user.setPassword("admin");
ResponseUtils.renderJson(response, user.toString());
}
通过调用newsadda这个接口就能获取到user.toString的字符串。
springmvc后台传值到jsp
@RequestMapping(value = "/news")
public String news(HttpServletRequest request, ModelMap model) {
List<Essay> essay = essayService.getEssays();
List<HashMap<String, String>> myList = new ArrayList<HashMap<String, String>>();
for (int i = 0; i < essay.size(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
map.put("essayname", essay.get(i).getEssayname());
map.put("time", essay.get(i).getTime());
map.put("title", essay.get(i).getTitle());
map.put("content", essay.get(i).getContent());
map.put("clickamount", essay.get(i).getClickamount());
myList.add(map);
}
model.addAttribute("list", myList);
return "news";
然后jsp页面信息处理
<%@ page import="java.util.List"%>
<%@ page import="java.util.HashMap"%>
<%
List<HashMap<String, String>> list = (List<HashMap<String, String>>) request.getAttribute("list");
%>
然后是显示问题
<table id="myTable" border="0" width="100">
<tr>
<td>文章标题</td>
<td>日期</td>
<td>作者</td>
</tr>
<%
for (int i = 0; i < list.size(); i++) {
%>
<tr>
<td>
<%
out.print(list.get(i).get("title"));
%>
</td>
<td>
<%
out.print(list.get(i).get("time"));
%>
</td>
<td>
<%
out.print(list.get(i).get("essayname"));
%>
</td>
</tr>
<%
}
%>
</table>
jsp页面显示完自动执行js方法
window.onload = function() {
fun();//执行操作
}