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:
<servlet>- <servlet-name>SpringFrontCtrler</servlet-name>
- <servlet-class>
- org.springframework.web.servlet.DispatcherServlet
- </servlet-class>
- <init-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>/WEB-INF/configs/*.spring.xml</param-value> (也就是一開始會跑這裡的相關.spring.xml)
- </init-param>
- <load-on-startup>1</load-on-startup>
- </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
- <bean name="/list_leagues.do"
- class="duke.soccer.web.ctrler.spring.ListLeaguesCtrler">
- <property name="leagueSvc">
- <ref bean="leagueService"/>
- </property>
- <property name="view">
- <value>list_leagues</value>
- </property>
- </bean>
- <bean id="viewResolver"
- class="org.springframework.web.servlet.view.InternalResourceViewResolver">
- <property name="prefix">
- <value>/jsp/</value>
- </property>
- <property name="suffix">
- <value>.jsp</value>
- </property>
- </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) 後
- <property name="prefix">
- <value>/jsp/</value>
- </property>
- <property name="suffix">
- <value>.jsp</value>
- </property>
- </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的 一些檔案基本設定方式