Spring MVC 小案例
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <bean id="flightService" class="com.flight.e2.DummyFlightService" /> </beans>
---------------------------------------------
spring-servlet.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <bean name="/home" class="com.flight.controler.HomeControler"> <property name="flightService" ref="flightService"></property> </bean> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"/> <property name="suffix" value=".jsp"/> </bean> </beans>
-------------------------------------------
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <display-name>FlightSpring</display-name> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>spring</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>spring</servlet-name> <url-pattern>/spring/*</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
------------------------------------
HomeControler.java
package com.flight.controler;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;
import com.flight.e2.FlightService;
public class HomeControler extends AbstractController {
private static final int FIVE_MINUTES = 5 * 60;
private FlightService flightService;
public HomeControler() {
setSupportedMethods(new String[] { METHOD_GET });
setCacheSeconds(FIVE_MINUTES);
}
public void setFlightService(FlightService flightService) {
this.flightService = flightService;
}
@Override
protected ModelAndView handleRequestInternal(HttpServletRequest arg0,
HttpServletResponse arg1) throws Exception {
ModelAndView mav = new ModelAndView("home");
mav.addObject("specials", flightService.getSpecialDeals());
return mav;
}
}
--------------------------------------------
FlightService.java
package com.flight.e2;
import java.util.List;
import com.flight.e1.SpecialDeal;
public interface FlightService {
List<SpecialDeal> getSpecialDeals();
List<Flight> findFlights(FlightSearchCriteria search);
}
-----------------------------------------
DummyFlightService.java
package com.flight.e2;
import java.util.List;
import com.flight.e1.SpecialDeal;
public class DummyFlightService implements FlightService {
public DummyFlightService() {
System.out.println("fs created###########333");
}
@Override
public List<SpecialDeal> getSpecialDeals() {
System.out.println("-----------Sorry, no special deals now---------");
return null;
}
@Override
public List<Flight> findFlights(FlightSearchCriteria search) {
System.out.println("-----------Sorry, no flights now ---------");
return null;
}
}
-----------------------------------
home.jsp
<?xml version="1.0" encoding="ISO-8859-1" ?>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Flight Booking Service</title>
</head>
<body>
<h1>Welcome to the Flight Booking Service</h1>
<p>We have the following specials now:</p>
<ul>
<c:forEach items="${specials}" var="special">
<li>${special.departFrom.name} - ${special.arriveAt.name} from $${special.cost }</li>
</c:forEach>
</ul>
<p><a href="search">Search for a flight.</a></p>
</body>
</html>