为什么我的模型和视图对象没有在JSP页面中显示。

时间:2016-04-22 08:19:35

标签: spring jsp spring-mvc model-view-controller

您好我在这里使用spring mvc。示例代码。

  @Controller  
  public class WelcomeController {  

      @RequestMapping("/welcome")  
      public ModelAndView welcome() {  
          String message = "welcome to my tutorial";  
          return new ModelAndView("welcome", "message", message);  
      }  
  }  

但是这里的消息无法通过jsp页面获取;

    welcome.jsp 
   message:${message}

只有我收到消息: 没有显示错误信息。

Here Web.xml file
<?xml version="1.0" encoding="UTF-8"?>  
<web-app version="2.5"   
xmlns="http://java.sun.com/xml/ns/javaee"   
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  
<servlet>  
<servlet-name>Dispatcherspring</servlet-name>  
<servlet-class>org.springframework.web.servlet.DispatcherServlet
</servlet-class><load-on-startup>1</load-on-startup>  
</servlet>  
<servlet-mapping>  
<servlet-name>Dispatcherspring</servlet-name>  
<url-pattern>*.html</url-pattern>  
</servlet-mapping>  
</web-app> 
 Here my Spring file
<?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:p="http://www.springframework.org/schema/p"  
xmlns:context="http://www.springframework.org/schema/context"  
xsi:schemaLocation="http://www.springframework.org/schema/beans  
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
http://www.springframework.org/schema/context  
http://www.springframework.org/schema/context/spring-context-3.0.xsd">  
<context:component-scan  base-package="package-name" />  
<bean  
class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
    <property name="prefix" value="/WEB-INF/jsp/" />  
    <property name="suffix" value=".jsp" />  
   </bean>  
   </beans>  

我发布了xml和web.xml文件

2 个答案:

答案 0 :(得分:0)

仅仅因为您没有控制器的@Requestmapping。 因此,Spring无法联系您的控制器并致电欢迎服务。

@controller
@RequestMapping
Class WelcomeController{
    @RequestMapping(value="/welcome", method=RequestMethod.GET)
    public void getStudent(){
   }
}

答案 1 :(得分:0)

    My controller is
    @Controller
    public class HouseController {

        @Autowired
        private HouseService houseService;

        @GetMapping(value = "/house")
        public ModelAndView get() {
            ModelAndView modelAndView = new ModelAndView();
            List<HouseDto> listHouse = houseService.listHouse();
            modelAndView.addObject("listHouse", listHouse);
            modelAndView.setViewName("house/listHouse");
            return modelAndView;
        }
    }

    <%@ page contentType="text/html; charset=UTF-8" isELIgnored="false"%>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
    <%@ page contentType="text/html; charset=UTF-8" isELIgnored="false"%>

    ###JSP
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html lang="en">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    </head>
    <body>
        <div>
                <c:forEach var="house" items="${listHouse}" varStatus="">
                            <tr>
                                <td>${house.name}</td>
                            </tr>
                        </c:forEach>
         </div>
    <body>

 #<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    its work for me