나는 Spring에서 강의를하고 있고, Spring MVC 프로그램은 JSP 페이지에서 표준 'Hello World'메시지를 출력한다. IDE에서 응용 프로그램을 실행하기 위해 Eclipse 3.6과 GlassFish 3.1을 사용하고 있습니다. 다음은 파일이 우려됩니다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.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
01 스프링 빈은 JSP에서 액세스 할 수 있다면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.
에 성공 출력을 제공 (googled 하고이 점에서 웹에 대한 몇 가지 연구를 한)와 w 놀랍게도 (실제로는 exposedContextBeanNames
및 exposeContextBeansAsAttributes
속성의 형태로 제공되는 봄 디자이너가 InternalResourceViewResolver
).
[sidebar] 이 사이트는 이미이 주제에 대해 많은 질문을 가지고 있습니다. 나는 내 문제를 해결하는 데 도움이 될 것이라고 생각했지만 그렇지 않은 몇 가지를 생각해 보았습니다. 그래서, 분명한 질문에 대해 용서해주십시오! 그래서
에서, 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; } }
디스패처 서블릿 설정 파일 : 디스패처 - 서블릿. xxx
<?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을 소개했다.자바
<?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>
그러나 (서비스 콩)
package service;
public class HelloWorldService {
public String dispMessage() {
String msg = "Hello World! My First Web App Using Spring MVC.";
return msg;
}
}
applicationContext.xml 프로젝트를 실행에 http://localhost:8080/FirstWebAppUsingSpringMVC/helloworld.htm
에 출력이
[email protected]
가 왜 '안녕하세요입니다 '메시지가 표시되지 않습니까? 어디서 잘못 했습니까? .........이 문제를 해결하기 위해 노력했지만 해결 방법을 찾지 못했습니다.
포럼 회원/전문가가 동일한 문제를 해결하는 데 도움이 될 수 있으면 감사히 생각합니다.
감사합니다, 그래서 여기에 주석을 편집 할 너무 늦었 user1586954
이 (toString 것 같은데) helloWorldService를 사용하면 toString을 구현하지 않았으므로 객체 표현을 가져올 수 있습니다. 그러나 실제로 보이는 콩에 실제로 액세스하고 있습니다. 그게 좋은 생각인지 여부는 또 다른 문제입니다 .... –