spring 2.5後有個好用的東西 "@Autowired"  用途就是我們在bean設定的值,可以直接用@Autowired來取

Ex:

(一)

首先xml內要匯入已下資訊

xxx.spring.xml:

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

<bean id="seasons_list"
    class="java.util.ArrayList">
    <constructor-arg>
        <list>
           <value>Spring</value>
           <value>Summer</value>
           <value>Autumn</value>
           <value>Winter</value>
        </list>
    </constructor-arg>
</bean>

xxx.java

private ArrayList seasons_list;
   @Autowired
   public void setSeasons_list(ArrayList seasonsList) {
        seasons_list = seasonsList;
}

(二)

   xxx.spring.xml:

    <bean id="car" class="Car"scope="singleton">
       <property name="brand"value="红旗 CA72"/>
       <property name="price" value="2000"/>
    </bean>

   xxx.java:

       @Autowired
    private Car car;

 

 

@RequestParam和@PathVariable

 

 

 

 

@RequestParam(value="abc", required=false) int abc

 

指網頁傳過來的requestParam 

 

@PathVariable取到的值是URI參數ex:?aaa=11

 

@PathVariable String aaa

 

 

 

第二個範例參考網址:http://java.chinaitlab.com/Spring/951955.html

=================================================================================================================

首先:

webconfig:


  1. <servlet>
  2.     <servlet-name>SpringFrontCtrler</servlet-name>
  3.        <servlet-class>
  4.           org.springframework.web.servlet.DispatcherServlet
  5.         </servlet-class>
  6. <init-param>
  7.       <param-name>contextConfigLocation</param-name>
  8.       <param-value>/WEB-INF/configs/*.spring.xml</param-value>   (也就是一開始會跑這裡的相關.spring.xml)
  9. </init-param>
  10.       <load-on-startup>1</load-on-startup>
  11. </servlet>

 

 *.spring.xml  共三支

1.app-config.spring.xml

2.mvc-config.spring.xml

3.service-config.spring.xml

 

app-config.spring.xml:

<bean id="appConfig"
class="duke.soccer.config.AppConfig">
<property name="dataDirectory">
<value>D:\workspace\private\gjun\edu_materials\WebAppTechCamp-Spring\solution\Spring3\lab05\DukeSoccerLeague\WebContent\WEB-INF\data</value>
</property>
</bean>

先解釋這段:

也就是說 會跑去"duke.soccer.config.AppConfig" 這個class

然後並傳直給對應的dataDirectory內值就是<value>內的 D:\workspace\private\gjun\edu_materials\WebAppTec......

 

duke.soccer.config.AppConfig (AppConfig.java) :

public class AppConfig implements IAppConfig{
private String dataDirectory;

public String getDataDirectory() {
return dataDirectory;
}

public void setDataDirectory(String dataDirectory) {
this.dataDirectory = dataDirectory;
}

}

 

service-config.spring.xml

<bean id="leagueService"
class="duke.soccer.service.LeagueService"
lazy-init="true">
<constructor-arg>
<value>#{appConfig.dataDirectory}</value>
</constructor-arg>
</bean>

appConfig.dataDirectory  對應到了 app-config.spring.xml 內的bean中Id為appConfig 剛剛有回傳 dataDirectory(還是只model??問一下)

同樣的 傳至 duke.soccer.service.LeagueService 內 帶值為 appConfig.dataDirectory 

 

duke.soccer.service.LeagueService:

public class LeagueService {

/** The cache of League objects. */
private static final List<League> LEAGUES_CACHE = new LinkedList<League>();

private String dataDirectory;

public LeagueService(String dataDirectory) {
this.dataDirectory = dataDirectory;
// Make sure that the leagues cache has been initialized
synchronized ( LEAGUES_CACHE ) {
if ( LEAGUES_CACHE.isEmpty() ) {
cacheLeagues();
}
}
}

/**
* This method returns a complete set of leagues.
*/
public List<League> getAllLeagues() {
// Return an immutable List; which makes this read-only
return Collections.unmodifiableList(LEAGUES_CACHE);
}

// more code ......

 

dataDirectory  又對應到LeagueService.java 內的  dataDirectory

於是又繼續往下跑完程式

 

 

mvc-config.spring.xml

 

  1. <bean name="/list_leagues.do"
  2.       class="duke.soccer.web.ctrler.spring.ListLeaguesCtrler">
  3.     <property name="leagueSvc">
  4.         <ref bean="leagueService"/>
  5.      </property>
  6.      <property name="view">
  7.         <value>list_leagues</value>
  8.      </property> 
  9. </bean> 
  10.  
  11. <bean id="viewResolver"
  12.      class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  13.      <property name="prefix">
  14.     <value>/jsp/</value>
  15.       </property> 
  16.       <property name="suffix">
  17.     <value>.jsp</value>
  18.   </property>
  19. </bean>

 

從"/list_leagues.do" 開始看,也就是我們的導頁就由這來控制了

下面的"leagueSvc" 也就是去ListLeaguesCtrler.jsp 設定"leagueSvc"值是去參考 bean="leagueService"

而"view"就是設定 ListLeaguesCtrler.jsp 內的view 設定為"list_leagues"

值得一提的是下方的 InternalResourceViewResolver 

先看下方ListLeaguesCtrler.jsp 內

@Override
public ModelAndView handleRequest(HttpServletRequest request,
   HttpServletResponse response) throws Exception {
        // TODO Auto-generated method stub
        List leagueList = leagueSvc.getAllLeagues();
       ModelAndView mv = new ModelAndView(view,"leagueList",leagueList); 
       return mv;

  

ModelAndView mv = new ModelAndView(view,"leagueList",leagueList);   這段的view

也就是我們所傳的"list_leagues"    ,回傳回來後(return mv) 後

  1. <property name="prefix">
  2.     <value>/jsp/</value>
  3.       </property> 
  4.       <property name="suffix">
  5.     <value>.jsp</value>
  6.   </property>
  7. </bean>

就會被放入/jsp/ 路徑內

然後變成   XXXX.jsp 而 XXXX也就是view中的"list_leagues"  = list_leagues.jsp

 

============================================================================================

 @Controller

也就是controller 另一種更方便的用法:

而在spring.xml內也務必加上下面紅字的部分:

[

<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="duke.soccer.web.ctrler.spring"/> 

]

接著我們只要正常的使用.do即可

Ex:

index.jsp:

<ul>
<li><a href='add_league_form.do'>Add a new league</a></li>
</ul>

 

XXX.java:

@Controller
public class AddLeaguesCtrler {
   @RequestMapping("/add_league_form")
   public ModelAndView input(){
      String inputView = "add_league";
      Map models = new HashMap(); 
      models.put("seasons_list", seasons_list);
      ModelAndView mv = new ModelAndView(inputView,models);
   return mv;
}

private ArrayList seasons_list;
@Autowired
public void setSeasons_list(ArrayList seasonsList) {
    seasons_list = seasonsList;
}

private LeagueService leagueService;
@Autowired
public void setLeagueService(LeagueService leagueService) {
     this.leagueService = leagueService;
}

@RequestMapping("/add_league")
public ModelAndView add(League league){
   League pleague = leagueService.createLeague(league.getYear(),
   league.getSeason(),
   league.getTitle());
   String successView = "success";
   ModelAndView mv = new ModelAndView(successView,"league",pleague);
   return mv;
}

 以上程式說明:

首先@Controller部分,類別掛上後可看當所對應的@RequestMapping,也就是xxx.do所對應的

以第一個("/add_league_form")來看,對應的就是add_league_form.do

 

以上就是spring mvc的 一些檔案基本設定方式

 

 

 

 

 

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 JoshS 的頭像
    JoshS

    JoshS的部落格

    JoshS 發表在 痞客邦 留言(0) 人氣()