我想通過使用Spring框架servlet.xml配置文件注入屬性值在我的控制器內。我的控制器開始這樣注入彈簧定義propery到控制器
package lv.lu.meetings.portal.mvc.controller;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpSession;
import lv.lu.meetings.domain.jpa.User;
import lv.lu.meetings.domain.jpa.meeting.Attendance;
import lv.lu.meetings.domain.jpa.meeting.Invite;
import lv.lu.meetings.domain.jpa.meeting.InviteStatus;
import lv.lu.meetings.domain.jpa.meeting.Meeting;
import lv.lu.meetings.domain.jpa.notification.Notification;
import lv.lu.meetings.domain.redis.Friend;
import lv.lu.meetings.interfaces.service.NotificationService;
import lv.lu.meetings.interfaces.service.UserService;
import lv.lu.meetings.portal.mvc.WebConst;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
/**
* Controller for displaying application home page.
*
* Supports multiple tab views: Main, Friends, Notifications, Meetings.
*/
@Controller
public class HomePageController {
// limit meeting count
// defined in meetings-servlet.xml
private String limit;
public String getLimit() {
return limit;
}
public void setLimit(String limit) {
this.limit = limit;
}
我的會議-servlet.xml文件就是這樣
<?xml version="1.0" encoding="UTF-8"?>
<!-- Spring Web application configuration file -->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
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/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>
<!-- Support for component autowiring -->
<context:component-scan base-package="lv.lu.meetings"/>
<!-- URL mapping for annotation-based Spring Web MVC controllers -->
<bean id="urlMapping" class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<property name="interceptors">
<list>
<ref bean="loginInterceptor"/>
</list>
</property>
</bean>
<!-- Limit output data -->
<bean id="HomePageController" class="lv.lu.meetings.portal.mvc.controller.HomePageController">
<property name="limit" value="10" />
</bean>
我收到提示
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'urlMapping' defined in ServletContext resource [/WEB-INF/meetings-servlet.xml]: Initialization of bean failed; nested exception is java.lang.IllegalStateException: Cannot map handler 'HomePageController' to URL path [/home]: There is already handler of type [class lv.lu.meetings.portal.mvc.controller.HomePageController] mapped.
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:529)
這肯定不便易於配置,但我對於Java和Spring框架本身來說是新的,所以我們對此表示讚賞。謝謝!
權,我也有\t @RequestMapping(value =「/ home/meetings」,method = RequestMethod.GET) public String displayMeetings(ModelMap model,HttpSession session){mapping我想使用我定義的屬性。我怎樣才能只選擇一個?我無法刪除 –
Slammer
@Slammer然後擺脫'HomePageController'的''聲明並找到另一種設置限制的方法。一種是我描述的方式。 –
是的,但提供了屬性佔位符示例的語法,它似乎只對jstl有效 – Slammer