我正在做一個Spring課程,我的Spring MVC程序在jsp頁面上打印標準的'Hello World'消息。我使用eclipse 3.6和GlassFish 3.1從IDE內運行應用程序。以下是有關的文件:從jsp訪問Spring beans
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);
}
}
調度servlet配置文件:調度員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>
入口點的應用:指數.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進行訪問(在這方面用google搜索並做了一些研究)和w令人驚訝的是,他們確實可以完成(如由彈簧設計者以exposedContextBeanNames
和exposeContextBeansAsAttributes
屬性InternalResourceViewResolver
的性質提供的)。
[側欄] 我知道這個網站已經有很多關於這個主題的問題。我已經通過了一些我認爲可以幫助我解決問題但沒有解決的問題。所以,請原諒我問明顯!
[/側邊欄]
因此,實施exposedContextBeanNames
財產,我修改
controller.HelloWorldController.java
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;
}
}
調度servlet配置文件:調度員的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">
<!-- 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>
的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>
<!--${beanName.beanProperty}-->
${helloWorldController.helloWorldService}
</body>
</html>
的web.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>
,並介紹
service.HelloWorldService。爪哇(服務豆)
package service;
public class HelloWorldService {
public String dispMessage() {
String msg = "Hello World! My First Web App Using Spring MVC.";
return msg;
}
}
的applicationContext.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: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>
但是,在運行該項目,上http://localhost:8080/FirstWebAppUsingSpringMVC/helloworld.htm
輸出
[email protected]
爲什麼是「你好世界'消息不顯示?我哪裏出錯了......一直試圖解決這個問題,但無法找到解決方案。
非常感謝,如果論壇成員/專家可以幫助解決相同的問題。
感謝 user1586954
看起來像的toString()被調用上你的helloWorldService,所以你只是得到對象表示,因爲你還沒有實現toString。但你實際上正在訪問這個bean。這是否是一個好主意是另一回事...... – 2013-04-05 10:26:15