Spring MVC - Example
實戰演練(一)SimpleMVC (Practical MVC Example 1)
View + Controller
mvc-dispatcher-servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<context:component-scan base-package="controller" />
<mvc:annotation-driven />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
JSPController.java
package controller;
import java.text.DateFormat;
import java.util.Date;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class JSPController {
//一般情況下 沒有使用ResponseBody時 return的值 會對應.jsp檔
@RequestMapping(value="/jsp/hello", method=RequestMethod.GET)
public String hello(Model model) {
model.addAttribute("message", "Hello,JSP controller");
Date date = new Date();
DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG);
String formattedDate = dateFormat.format(date);
model.addAttribute("serverTime", formattedDate );
return "hello";//會去找hello.jsp
}
@RequestMapping(value="/jsp/hi", method=RequestMethod.GET)
public String hi(Model model) {
model.addAttribute("message", "Hi,JSP controller");
return "hi";//會去找會去找hi.jsp
}
//path與variable 一樣 則不用多作設定 會直接對應
@RequestMapping(value="/jsp/{message}", method=RequestMethod.GET)
public String pathVars(@PathVariable String message) {
// No need to add @PathVariables "message" to the model
// They will be merged in the model before rendering
return "hi";
}
//利用ModelAndView指定JSP ex. ModelAndView("JSP名稱", "變數名稱" ,"值")
@RequestMapping("/jsp/yo")
public ModelAndView sayHi(){
String response = "Yo,this is simple example.";
return new ModelAndView("hello", "message", response);
}
@RequestMapping("/jsp/hey")
public ModelAndView sayHey(){
String response = "Hey,this is simple example.";
return new ModelAndView("hi", "message", response);
}
//當設為void 沒有return 路徑直接對應直接對應.jsp檔名(自動加上前後文)
//可以自己把value從 show改成 /jsp/show 看會發生什麼事情
@RequestMapping(value="show", method=RequestMethod.GET)
public void usingRequestToViewNameTranslator(Model model) {
model.addAttribute("name", "Phelim");
model.addAttribute("color", "Blue");
}
}
hello.jsp
<property name="prefix" value="/jsp/" />
<property name="suffix" value=".jsp" />
<center><h3>This is "hello.jsp" page</h3></center>hi.jsp
您從Controller輸入的文字為:${message} ${serverTime}
<center><h3>This is "hi.jsp" page</h3></center>show.jsp
您從Controller輸入的文字為:${message}
<center><h3>This is "show.jsp" page</h3></center>
${name}, Welcome to here.<br />
Your favorite color is <font color='${color}'>${color}</font>
實戰講解
來講講 mvc-dispatcher-servlet.xml 之中的<property name="prefix" value="/jsp/" />
<property name="suffix" value=".jsp" />
字面上來看,是指前文與後文
這邊是配合Controller中 return 的值model.addAttribute("message", "Hi,JSP controller");
假設 return "hello"
那他會去找 hello.jsp 那麼 哪裡找呢?
http://localhost:8080/SimpleMVC/jsp/hello.jsp
Model
可以寫入.jsp檔所需要的值
ex: model.addAttribute("message", "Hi,JSP controller");
利用ModelAndView指定JSP
ex. ModelAndView("JSP名稱", "變數名稱" ,"值")
http://localhost:8080/SimpleMVC/jsp/


沒有留言:
張貼留言