上接Maven多模块工程搭建完之后,开始搭建web工程。
一、pom.xml的配置
1、首先是pom.xml中要添加各种Sring依赖。
2、其次pom.xml中添加打包要包含的资源文件,在
3、在pom.xml中添加各种jetty插件和maven插件,其中jetty.xml以及jetty如何启动见前面的jetty启动文章。
二、web.xml
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
三、springMVC-servlet.xml的配置
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation=
"http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">
四、测试:
1、启动jetty首先进行index.jsp首页测试
http://localhost:9080/ps_service/
会显示jsp中的内容。
2、写一个测试类
package com.suning.service;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.suning.viewsolver.Account;
@Controller
public class PsServiceApp {
private Logger log = LoggerFactory.getLogger(PsServiceApp.class);
@RequestMapping(value = "/change_version.do", produces = "application/json; charset=UTF-8")
public void changeVersion(@RequestParam("version") String version, HttpServletResponse response)
throws Exception{
JSONObject json = new JSONObject();
response.setContentType("application/json;charset=UTF-8");
String result = version;
json.put("result", result);
json.writeJSONString(response.getWriter());
return;
}
}
http://localhost:9080/ps_service/change_version.do?version=v1.1
会显示
{"result":"v1.1"}说明SpringMVC的框架搭建完成
下接 SpringMVC通过视图配置器返回Json和跨域Jsonp格式数据