服务端最最基本的有了,设想系统能够支持各种客户端展现,比如说jsp、extjs(json)、android甚至.net客户端、delphi客户端。
这块研究了几天,经过各种实验,基本出来了,记录一下最终结果。
参考了http://www.blogjava.net/badqiu/archive/2009/12/21/306793.html
springmvc-servlet.xml添加配置如下:
<bean
class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<!-- 设置为true以忽略对Accept Header的支持 -->
<property name="ignoreAcceptHeader" value="true" />
<!-- true,开启扩展名支持,false关闭支持 ?-->
<property name="favorPathExtension" value="true" />
<!-- 用于开启 /userinfo/123?format=json的支持 ?-->
<property name="favorParameter" value="true" />
<!-- 在没有扩展名时即: "/user/1" 时的默认展现形式 -->
<property name="defaultContentType" value="text/html" />
<property name="mediaTypes">
<map>
<!-- 扩展名至mimeType的映射,即 /user.json => application/json -->
<!-- 告诉视图解析器,返回的类型为json格式 application/json -->
<entry key="json" value="application/json" />
<entry key="xml" value="application/xml" />
</map>
</property>
<property name="viewResolvers">
<list>
<bean class="org.springframework.web.servlet.view.BeanNameViewResolver" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView"></property>
<property name="prefix" value="/" />
<property name="suffix" value=".jsp" />
</bean>
</list>
</property>
<property name="defaultViews">
<list>
<!-- ModelAndView里的数据变成JSON -->
<bean
class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" />
<!-- XML -->
<bean class="org.springframework.web.servlet.view.xml.MarshallingView">
<property name="marshaller">
<bean class="org.springframework.oxm.xstream.XStreamMarshaller" />
</property>
</bean>
</list>
</property>
</bean>
UserController.java
package mybatistest.controller;
import java.util.HashMap;
import java.util.Map;
import mybatistest.entity.User;
import mybatistest.service.UserService;
import mybatistest.utils.DaoHelper;
import org.apache.log4j.Logger;
import org.aspectj.weaver.patterns.ThisOrTargetAnnotationPointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
@Controller
@RequestMapping("/user/*")
public class UserController extends BaseController {
Logger logger = Logger.getLogger(UserController.class);
@Autowired
private UserService userService;
// @RequestMapping("login")
@RequestMapping(value="/login",
method=RequestMethod.GET)
//,produces={"text/html", "application/xml", "application/json"})
// 子request请求url,拼接后等价于/user/login
public ModelAndView Login(String userName, String password) {
logger.debug("=============");
logger.debug("username:" + userName);
logger.debug("password:" + password);
ModelAndView mv = new ModelAndView();
//添加模型数据 可以是任意的POJO对象
// mv.addObject("message", userName + "! Hello World!");
// User user = userService.getByID("1");
// mv.addObject("message", user.getUserName() + "! Hello World!");
//设置逻辑视图名,视图解析器会根据该名字解析到具体的视图页面
// mv.setViewName("hello"); // 逻辑视图名
User user = null;
user = new User();
user.setUserID(DaoHelper.getUUID());
user.setUserNumber("001");
user.setUserName("李四");
user.setRemark("备注");
mv.addObject(user);
Map<String, User> mp = new HashMap<>();
mp.put("message", user);
mv.addAllObjects(mp);
mv.setViewName("hello");
return mv;
}
}
启动试一下:
1、IE中,登录页面直接“确认”,结果如下:
mybatistest.entity.User@429c874d
2、http://localhost:8088/mybatistest/user/login.xml
- <mybatistest.entity.User>
<userID>e251b2e1340f4935b3e389124291be21</userID>
<userNumber>001</userNumber>
<userName>李四</userName>
<remark>备注</remark>
</mybatistest.entity.User>
3、http://localhost:8088/mybatistest/user/login.json
是个下载,用火狐试一下:
{"message":{"userID":"0250924bdc2d4470bb07d327ea52c0f3","userNumber":"001","userName":"李四","remark":"备注"},"user":{"userID":"0250924bdc2d4470bb07d327ea52c0f3","userNumber":"001","userName":"李四","remark":"备注"}}
同样,换成http://localhost:8088/mybatistest/user/login?format=json、http://localhost:8088/mybatistest/user/login?format=xml、http://localhost:8088/mybatistest/user/login?format=XXX
都OK了。
最开始@Controller 是用的Spring4的@RestController ,但发现换回@Controller 也行,不清楚。
当然,最关键的是springmvc-servlet.xml中的配置,上面大部分都写了注释。唯一没有想明白的是,是如何根据不同的请求,返回不同的view。看日志中,是有Requested media type is 'application/json' (based on parameter 'format'='json')等信息,但如何决定用哪个view呢?先记下吧。