2013年6月21日 星期五

實戰演練(一)SimpleMVC (Practical MVC Example 1)

Spring MVC - Example




First, you must create New Project.
File -> New -> Dynamic Web Project -> Finish

create 

"index.jsp" in WebContent
"jsp" folder in WebContent
"web.xml" in WebContent/WEB-INF

web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>SimpleMVC</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
 index.jsp
<h1>Welcome to  Homepage</h1>
Just like this.
Run on server.

Step 1. SimpleController

web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>SimpleMVC</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
    <servlet-name>mvc-dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>mvc-dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
  </context-param>

  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
</web-app>
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 />
</beans>

create 2 package controller and model in src
add .jar in /WEB-INF/lib
create SimpleController in controller package

SimpleController.java
package controller;
import org.springframework.stereotype.Controller;
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.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping("/simple")
public class SimpleController {
@RequestMapping()
public @ResponseBody String simple(){
return "<font size=8>This is simple page</font>";
}
@RequestMapping("/test")
public @ResponseBody String test(){
return "<font size=8>ResponseBody is skip View</font>";
}

//變數路徑
@RequestMapping(value="{var}")
public @ResponseBody String whitVAR(@PathVariable String var){
return "Your var is \""+var+"\"";
}
//必須要有值才能執行
@RequestMapping(value="param" ,params="name" ,method=RequestMethod.GET)
public @ResponseBody String param(@RequestParam String name){
return "Your name is: "+name;
}
//瀏覽器默認的Response編碼
@RequestMapping("/charset/accept")
public @ResponseBody String responseAcceptHeaderCharset() {
return "\u3053\u3093\u306b\u3061\u306f\u4e16\u754c\uff01 (\"Hello world!\" in Japanese)";
}
//produces 更改Response的編碼為 text/plain; charset=UTF-8
@RequestMapping(value="/charset/produce",method=RequestMethod.GET ,produces="text/plain;charset=UTF-8")
public @ResponseBody String responseProducesConditionCharset() {
return "\u3053\u3093\u306b\u3061\u306f\u4e16\u754c\uff01 (\"Hello world!\" in Japanese)";
}
}



like this


實戰講解

首先先來了解web.xml檔
<servlet> <servlet-mapping> <context-param> 為一組
主要功能在於抓取  servlet設定檔 mvc-dispatcher-servlet.xml

mvc-dispatcher-servlet.xml
<context:component-scan base-package="controller" />
此段設定 會去你指定的package找取你的Controller

<mvc:annotation-driven />
Eclipsee的預設編碼為MS950 而且是寫死的
此段是讓編碼部份可以活性化 (別人已經寫好的驅動)
你的Controller中 有更改到編碼 ex: produces="text/plain;charset=UTF-8"
若是沒有加入這段 那輸出的值 還是會被硬改成 MS950

接下來講解Controller的部份

@Controller 

由<context:component-scan base-package="controller" />可以知道
他會去controller這個package找我們所寫的Controller 但一個package會有很多class
到底哪些是Controller呢?就由這個標籤來告訴電腦,我是Controller!

@RequestMapping

RequestMapping 有六種屬性
1. value
指定request的路徑

2. method
指定接收的method (GET,POST,PUT,DELETE)

3. comsumes
指定request提交內容(Content-Type) ex: application/json

4. produces
指定返回內容的Type,僅支援request-header=Accept

5. params
指定request必須包含所給的參數才能運行

6. headers 
指定request必須包含所給的header值才能運行


@ResponseBody

直接透過html,將return的值直接呈現給client。
ex: http://localhost:8080/SimpleMVC/simple

@PathVariable 變數路徑

變數路徑會對應 Mapping中的 {var}  有兩種對應方法
1. @PathVariable (value="abcd") 對應 {abcd}
2. @PathVariable String AA 對應 {AA}
ex: http://localhost:8080/SimpleMVC/simple/boy

@RequestParam

同@RequestMapping中的params效果,Mapping必須擁有指定參數才執行
ex: http://localhost:8080/SimpleMVC/simple/param?name=Phelim

沒有留言:

張貼留言