SpringMVC Controller 传MAP值到JSP页面,取不到值,在线等待

张沛洁 2018-04-17 04:36:19
spring-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd" >

<!-- <import resource="classpath*:Spring-RabbitMQ.xml" /> -->

<context:component-scan base-package="com.cict.controller" />


<!-- 激活annotation功能 -->
<context:annotation-config />

<mvc:annotation-driven/>

<!-- 激活annotation功能 -->
<!-- <context:spring-configured /> -->

<bean id="InternalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>

</beans>

Controller.java
@RequestMapping(value = "Hello.do",method=RequestMethod.POST)
public String SendMessage(@RequestParam(value = "txtDisplay") String disPlay
,HttpServletRequest request) throws Exception {

Map<String, String> map = new HashMap<String, String>();

logger.info(disPlay);
logger.info(request.getMethod());
logger.info(request.getParameter("txtDisplay"));
String ip = null;
InetAddress iaAddress = null;
iaAddress = InetAddress.getLocalHost();
ip = iaAddress.getHostAddress();
logger.info(ip);

map.put("name", disPlay);
map.put("ip", ip);

return "Hello";
}

Hello.jsp
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<%-- <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> --%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>Insert title here</title>
</head>
<body>
<%-- Hello ${map.name },your ip is ${map.ip } --%>
${name},${ip}
</body>
</html>
...全文
950 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
冰思雨 2018-04-24
  • 打赏
  • 举报
回复
添加getMap()函数,试一试。
阿闰 2018-04-18
  • 打赏
  • 举报
回复
Map<String, List<Object>> map = secondCustomerService.selectDictionaryData(); view.addObject("map", map); 下面是页面 <c:forEach items="${map.scywqy }" var="mp"> <option value="${mp.dictdatavalue }">${mp.dictdataname }</option> </c:forEach>
卡布奇诺er 2018-04-18
  • 打赏
  • 举报
回复
@RequestMapping(value = "Hello.do",method=RequestMethod.POST) public String SendMessage(@RequestParam(value = "txtDisplay") String disPlay ,HttpServletRequest request, Model model) throws Exception { Map<String, String> map = new HashMap<String, String>(); logger.info(disPlay); logger.info(request.getMethod()); logger.info(request.getParameter("txtDisplay")); String ip = null; InetAddress iaAddress = null; iaAddress = InetAddress.getLocalHost(); ip = iaAddress.getHostAddress(); logger.info(ip); map.put("name", disPlay); map.put("ip", ip); model.addAttribute("map", map); return "Hello"; } 试一下这个
诺丽果 2018-04-18
  • 打赏
  • 举报
回复
你的map并没有返回给前台啊,使用model.addAttribute("map", map);,但是还是建议将map存到list,再返回给前台,前台接受的时候使用<c:forEach循环取值。
张沛洁 2018-04-18
  • 打赏
  • 举报
回复
感谢:wasd986523 、 卡布奇诺er 两位大师的指点 我做个小的总结: 对于springmvc @controller控制层实现业务逻辑传值到JSP前端,待展示的JSP页面必须要加上下述页头,否则不管是ModelAndView、Map、ModelMap还是Model,都是取不到值的。 <%@ page isELIgnored="false" %>
张沛洁 2018-04-17
  • 打赏
  • 举报
回复
@SessionAttributes(value={"name","ip"},types={String.class,String.class}) public class MessageController { private Logger logger = Logger.getLogger(MessageController.class.getName()); @RequestMapping(value = "Rabbitmq.do") public String SendMessage(@RequestParam(value = "txtDisplay") String disPlay ,HttpServletRequest request ,HttpServletResponse response) throws Exception { logger.info(disPlay); logger.info(request.getMethod()); logger.info(request.getParameter("txtDisplay")); String ip = null; InetAddress iaAddress = null; iaAddress = InetAddress.getLocalHost(); ip = iaAddress.getHostAddress(); logger.info(ip); String tmp = disPlay; return tmp; } /*@RequestMapping(value = "Hello.do",method=RequestMethod.POST) public String SendMessage(@RequestParam(value = "txtDisplay") String disPlay ,HttpServletRequest request) throws Exception { ModelMap map = new ModelMap(); logger.info(disPlay); logger.info(request.getMethod()); logger.info(request.getParameter("txtDisplay")); String ip = null; InetAddress iaAddress = null; iaAddress = InetAddress.getLocalHost(); ip = iaAddress.getHostAddress(); logger.info(ip); map.addAttribute("name", disPlay); map.addAttribute("ip", ip); return "Hello"; }*/ @RequestMapping(value = "Hello.do",method=RequestMethod.POST) public String SendMessage(@RequestParam(value = "txtDisplay") String disPlay ,HttpServletRequest request) throws Exception { Map<String, String> map = new HashMap<String, String>(); logger.info(disPlay); logger.info(request.getMethod()); logger.info(request.getParameter("txtDisplay")); String ip = null; InetAddress iaAddress = null; iaAddress = InetAddress.getLocalHost(); ip = iaAddress.getHostAddress(); logger.info(ip); map.put("name", disPlay); map.put("ip", ip); return "Hello"; } 还是不行,显示这样的 Hello ${sessionScope.name },your ip is ${sessionScope.ip }
卡布奇诺er 2018-04-17
  • 打赏
  • 举报
回复
https://blog.csdn.net/gezi2015129/article/details/61919894 看一下这个,你要把map放入上下文中,可以再方法里加入response,然后response.setAttribute 或者注解@SessionAttributes
张沛洁 2018-04-17
  • 打赏
  • 举报
回复
怎么个方法?
卡布奇诺er 2018-04-17
  • 打赏
  • 举报
回复
你的map没有放到上下文里啊

81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧