2013年6月24日 星期一

實戰演練(三)SimpleMVC (Practical MVC Example 3)

JSON and XML



You can preview Practical MVC Example 2
實戰演練(二)SimpleMVC (Practical MVC Example 2)

add 3 .jar file



XMLController.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.ResponseBody;
import model.Coffee;
@Controller
@RequestMapping("/xml")
public class XMLController {
@RequestMapping(value="{name}", method = RequestMethod.GET ,
produces="application/xhtml+xml;charset=UTF-8")
public @ResponseBody Coffee getCoffeeInXML(@PathVariable String name) {
Coffee coffee = new Coffee(name, "100");
return coffee;
}
}
Coffee.java (Model)
package model;
import javax.xml.bind.annotation.XmlRootElement;
import com.sun.xml.internal.txw2.annotation.XmlElement;
@XmlRootElement(name = "coffee")
public class Coffee {
String name;
String quanlity;

public String getName() {
if(name.equals("latte"))
return name;
else
return name+" is not coffee";
}
@XmlElement
public void setName(String name) {
this.name = name;
}
public String getQuanlity() {
return "プライス: "+quanlity;
}
@XmlElement
public void setQuanlity(String quanlity) {
this.quanlity = quanlity;
}
public Coffee(String name, String quanlity) {
this.name = name;
this.quanlity = quanlity;
}
public Coffee() {
}
}

JSONController.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.ResponseBody;
import model.Shop;
import model.User;
@Controller
@RequestMapping("/json")
public class JSONController {
@RequestMapping(value = "{name}", method = RequestMethod.GET)
public @ResponseBody
Shop getShopInJSON(@PathVariable String name) {
Shop shop = new Shop();
shop.setName(name);
shop.setStaffName(new String[] { "mkyong1", "mkyong2" });
return shop;
}
@RequestMapping(value="submit",method=RequestMethod.POST,
       produces="application/json;charset=UTF-8")
public @ResponseBody User getUser(String name,String birth,String phone){
User usr=new User();
usr.setName(name);
usr.setBirth(birth);
usr.setPhone(phone);
return usr;
}
}

Shop.java (Model)
package model;
public class Shop {
String name;
String staffName[];
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String[] getStaffName() {
return staffName;
}
public void setStaffName(String[] staffName) {
this.staffName = staffName;
}
public Shop() {
}
}
User.java (Model)
package model;
import java.util.Calendar;
public class User {  
String name;
String birth;
String phone;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getBirth() {
return birth;
}
public void setBirth(String birth) {
this.birth = birth;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getToday(){
Calendar cal = Calendar.getInstance();
//當前年
int yy=cal.get(Calendar.YEAR);
//當前月
int mm=(cal.get(Calendar.MONTH))+1;
//當前日
int dd=cal.get(Calendar.DAY_OF_MONTH);
//當前時 24時制
int hh = cal.get(Calendar.HOUR_OF_DAY);
//當前分
int minute = cal.get(Calendar.MINUTE);
//當前秒
int second = cal.get(Calendar.SECOND);
   return yy+"年 "+mm+"月 "+dd+"日  "+hh+"時"+minute+"分"+second+"秒";
}
}
If you want use POST RequestMethod.
You must having form(post) like this.
<form action="json/submit" method="post">
姓名:<input type="text" name="name" /><br />
生日:<input type="text" name="birth" /><br />
電話:<input type="text" name="phone" /><br />
<input type="submit" name="list" value="傳送"> <input type="reset" value="重置">
</form>  

實戰講解


這邊好像沒什麼好講的,從程式碼上來看蠻直觀的。
稍微講一下 有form的這個部份
正常的輸出情況
Request:
 Response:

但要是輸入的文字 混合著不同字型
Request:
 Response:


撰寫時用UTF8 輸出時也用UTF8 可是值卻是亂碼 Why?
有發現嗎 在撰寫時 我的input name="name" 而在Controller也只寫了String name
為什麼對應的上?其實那是spring.web 的的lib已經幫你寫好了
不過他卻把編碼給寫死了,所以一旦 你有從form 把值傳進Controller時
會經過 Spring的內碼 先被轉成ISO-8859才被傳進Controller
然後~就變亂碼啦~

解決方法
在web.xml加入以下一段
<!-- 字符集 過濾器  -->
    <filter>
        <filter-name>CharacterEncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>CharacterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
這段就是將spring.web傳進去的編碼從預設的ISO-8859改成UTF-8
如此一來 再執行相同的程式 就搞定了


沒有留言:

張貼留言