我正在Spring学习一门课程,我的Spring MVC程序在jsp页面上打印标准的“Hello World”消息。我正在使用eclipse 3.6和GlassFish 3.1从IDE中运行应用程序。以下是有关文件:
controller.HelloWorldController.java
package controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class HelloWorldController {
@RequestMapping("/helloworld")
protected ModelAndView HelloWorld() throws Exception {
String hello = "Hello World! My First Web App Using Spring MVC.";
return new ModelAndView("helloworld","hello",hello);
}
}
Dispatcher servlet配置文件:dispatcher-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: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="controller" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
申请入口点:index.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>My First Web Application Using Spring MVC</title>
</head>
<body>
<h3><a href="helloworld.htm">Greet the World!</a></h3>
</body>
</html>
的helloWorld.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>My First Web Application Using Spring MVC</title>
</head>
<body>
${hello}
</body>
</html>
的web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<display-name>FirstWebAppUsingSpringMVC</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
</web-app>
并在http://localhost:8080/FirstWebAppUsingSpringMVC/helloworld.htm.
我很好奇是否可以从jsp访问Spring bean(谷歌搜索并在网上进行一些研究)并且惊讶于他们确实可以完成(由Spring设计者以{{1的形式提供) {} {}}和exposedContextBeanNames
的{{1}}属性。
[边栏] 我知道这个网站已经有很多关于这个主题的问题。我已经通过了一些我认为可以帮助我解决问题但没有解决问题的问题。所以,请原谅我明白的问题! 的 [/侧边栏]
因此,要实现exposeContextBeansAsAttributes
属性,我修改了
controller.HelloWorldController.java
InternalResourceViewResolver
Dispatcher servlet配置文件:dispatcher-servlet.xml
exposedContextBeanNames
的helloWorld.jsp
package controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;
import service.HelloWorldService;
public class HelloWorldController extends AbstractController {
//property helloWorldService that references Service bean HelloWorldService
private HelloWorldService helloWorldService;
//defined handleRequestInternal method of the AbstractController class
//that returns a new ModelAndView object
//Instance of ModelAndView adds logical viewName and model data as arguments
@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request,
HttpServletResponse response) throws Exception {
// TODO Auto-generated method stub
//view passed as string to new ModelAndView object
ModelAndView mv = new ModelAndView("helloworld");
//instance of Service class accessing its object dispMessage()
mv.addObject("helloworld", helloWorldService.dispMessage());
return mv;
}
//generate setter and getter for the property
//implicitly injecting business component within Controller
public void setHelloWorldService(HelloWorldService helloWorldService) {
this.helloWorldService = helloWorldService;
}
/**
* @return the helloWorldService
*/
public HelloWorldService getHelloWorldService() {
return helloWorldService;
}
}
的web.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: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">
<!-- defined handler to map url pattern to Controller bean on the basis of controller class -->
<bean id="controllerclasshandlermapping"
class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping">
</bean>
<!-- defined view resolver to resolve Controller class HelloWorldController to helloworld.jsp -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/"
p:suffix=".jsp">
<!-- exposing Controller bean helloWorldController to the view -->
<property name="exposedContextBeanNames">
<list>
<value>helloWorldController</value>
</list>
</property>
</bean>
<!-- Explicit mapping to Controller bean helloWorldController which references Service bean thru its property
from the applicationContext.xml -->
<bean id= "helloWorldController"
class="controller.HelloWorldController" p:helloWorldService-ref="helloWorldService"/>
</beans>
并介绍了
service.HelloWorldService.java(服务bean)
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>My First Web Application Using Spring MVC</title>
</head>
<body>
<!--${beanName.beanProperty}-->
${helloWorldController.helloWorldService}
</body>
</html>
的applicationContext.xml
<context-param>
<param-name>contextConfigLocation</param-name>
//changed path to applicationContext.xml to reference Service class
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
但是,在运行项目时,package service;
public class HelloWorldService {
public String dispMessage() {
String msg = "Hello World! My First Web App Using Spring MVC.";
return msg;
}
}
上的输出是
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema
/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema
/tx/spring-tx-3.0.xsd">
<!--Service bean defined-->
<bean name="helloWorldService" class="service.HelloWorldService"/>
</beans>
为什么不显示“Hello World”消息?我哪里出错了?.......一直在努力解决这个问题但无法找到解决办法。
非常感谢论坛成员/专家是否可以帮助解决这个问题。
由于 user1586954
答案 0 :(得分:1)
编辑评论太晚了,所以这里有各种答案。
看起来你的helloWorldService正在调用toString(),所以你只是得到了对象表示,因为你没有实现toString。但你实际上是在访问bean。
这是否是个好主意是另一回事。
最好只使用Model将数据返回到视图(你已经在做),这样你的JSP就足够了:$ {helloworld}