2012-05-31 26 views
7

Tôi mới vào mùa xuân và bắt đầu với các hướng dẫn đơn giản. Tôi định nghĩa đơn giản jsp và điều khiển, sau đó ánh xạ nó tài liệu xml và chạy nó, nhưng chỉ có những gì tôi có cưa là một trang WEV của tôi không có giá trị mà tôi đã được thông qua năm khiển:Mùa xuân: làm thế nào để gọi điều khiển đơn giản?

@Controller 
public class HomeController { 

@Autowired 
private ExampleService exampleService; 

@RequestMapping(value = "/", method = RequestMethod.GET) 
public String home(Model model) { 
    model.addAttribute("serverTime", exampleService.getSystemTime()); 
    model.addAttribute("appVersion", exampleService.getAppVersion()); 
    return "home"; 
} 
} 

@Component 
public class ExampleService { 

@Value("#{appProperties.appVersion}") 
private String appVersion; 

/** 
* Returns formatted system time. 
* 
* @return 
*/ 
public String getSystemTime() { 
DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG); 
return dateFormat.format(new Date()); 
} 

public String getAppVersion() { 
return appVersion; 
} 
} 

<?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_2_5.xsd" id="WebApp_ID" version="2.5"> 
<display-name>Spring</display-name> 

<context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value>/WEB-INF/spring-config.xml</param-value> 
<!--  <param-value>/WEB-INF/jdbc-config.xml</param-value> --> 
</context-param> 

<listener> 
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
</listener> 

<servlet> 
    <servlet-name>appServlet</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    <init-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>/WEB-INF/servlet-context.xml</param-value> 
    </init-param> 
    <load-on-startup>1</load-on-startup> 
</servlet> 

<servlet-mapping> 
    <servlet-name>appServlet</servlet-name> 
    <url-pattern>/</url-pattern> 
</servlet-mapping> 

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

<annotation-driven /> 

<resources mapping="/resources/**" location="/resources/" /> 

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

<context:component-scan base-package="com.home.spring" /> 

enter image description here

<?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:util="http://www.springframework.org/schema/util" 
xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
    http://www.springframework.org/schema/util 
    http://www.springframework.org/schema/util/spring-util-3.0.xsd"> 

<!-- Root Context: defines shared resources visible to all other web components --> 

<util:properties id="appProperties" location="properties.properties"/> 

Tôi đã học được một vài hướng dẫn và đọc một phần web của tài liệu chính thức có liên quan đến công việc của tôi nhưng tôi chưa hiểu đâu là vấn đề trong mã của tôi.

Tôi bị kẹt với nó. Nếu bạn có bất kỳ ý tưởng xin vui lòng chia sẻ nó với tôi. Cảm ơn.

+0

Có bạn đã có một cái nhìn tại các ứng dụng petclinic (http://static.springsource.org/docs /petclinic.html)? Có lẽ điều đó sẽ giúp bạn. – Christian

+0

shwo trang jsp quá, mà tất cả nhìn sfine - không có lỗi? – NimChimpsky

+0

Bạn có thể hiển thị cho chúng tôi lỗi trong bảng điều khiển nếu có. AS jsp của bạn có vẻ tốt với tôi. Lỗi có thể cung cấp cho chúng tôi một số gợi ý – dharam

Trả lời

1

Làm việc tốt cho tôi. Cùng một mã. Bạn có thể xin vui lòng gửi jsp quá.

Tôi có jsp dưới đây: Vui lòng cho chúng tôi biết nếu jsp của bạn có vẻ tương tự.

<head> 
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 
<title>Insert title here</title> 
</head> 
<body> 
${serverTime} 
</body> 
</html> 

EDIT: loại bỏ tất cả mọi thứ từ web.xml và đặt dưới đây:

<context-param> 
<param-name>contextConfigLocation</param-name> 
<param-value>/WEB-INF/appServlet-servlet.xml</param-value> 
</context-param> 
<listener> 
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
</listener> 
<servlet> 
<servlet-name>appServlet</servlet-name> 
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
</servlet> 

<servlet-mapping> 
<servlet-name>appServlet</servlet-name> 
<url-pattern>*.do</url-pattern> 
</servlet-mapping> 
+0

Có vẻ tương tự nhưng tôi cũng có một số mã khi bắt đầu jsp. Nhưng mã của bạn cũng không hoạt động –

+0

nội dung của tệp spring-config.xml là gì? Vui lòng thử xml trong EDIT – dharam

+0

Nó không hoạt động, kết quả không thay đổi. –

Các vấn đề liên quan