Java的WEB框架中,Struts2应该是最著名的,不过最近试了试Spring3 MVC,感觉好爽啊,几乎像ASP.Net MVC3一样舒服,以后就用它了。简单记录一下过程,没有技术含量。
1、准备包
下载的是spring framework 3.2.0,从中抽取以下jar到工程的WEB-INF/lib下:
spring-beans-3.2.0.RELEASE.jar
spring-context-3.2.0.RELEASE.jar
spring-core-3.2.0.RELEASE.jar
spring-expression-3.2.0.RELEASE.jar
spring-web-3.2.0.RELEASE.jar
spring-webmvc-3.2.0.RELEASE.jar
另外还需要几个第三方jar包,记录日志和处理json:
commons-logging-1.1.1.jar
jackson-core-als-1.9.11.jar
jackson-mapper-asl-1.9.11.jar
2、WEB-INF/web.xml
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_2_5.xsd"
id="WebApp_ID" version="2.5">
3、WEB-INF/spring-servlet.xml
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
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.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
4、WEB-INF/applicationContext.xml
spring的配置文件,由于我们不使用它的其它功能,暂时放个空的就好了。
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.0.xsd">
5、写Controller
package com.test.mvc.web;
import org.springframework.stereotype.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.*;
/**
* 控制器,用Controller注解
*/
@Controller
public class HomeController {
/**
* 映射到/welcome
*/
@RequestMapping(value = "/welcome")
public ModelAndView welcome(){
ModelAndView mv = new ModelAndView("welcome"); //使用welcome.jsp,如果不写,根据url默认也是welcome.jsp
mv.addObject("hello", "Hello"); //model中增加一个名为hello的字符串
Client client = new Client();
client.setName("User");
mv.addObject("client", client); //再增加一个名为client的自定义对象
return mv;
}
/**
* 如果不需要Model,直接返String更简单,对应的view为login_page.jsp
*/
@RequestMapping(value = "/login")
public String login(){
return "login_page";
}
/**
* 一个返回json的方法,用ResponseBody标识
* 可以在url中定义参数中,实现RESTful真是太简单了
* 传参很灵活,可以从url中取,也可以定义普通的
*/
@RequestMapping(value="/client/{name}", method = RequestMethod.GET)
@ResponseBody
public Client getClient(@PathVariable String name, String title){
Client client = new Client();
client.setName(title+ " " + name);
return client;
}
}
里面用到了Client,很简单的POJO:
package com.test.mvc.web;
/**
* 自定义一个POJO
*/
public class Client {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
6、写视图
根据spring-servlet.xml中的配置,视图要放到WEB-INF/jsp下,新建welcome.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
${hello}
${client.name}
一切就绪,把Tomcat跑起来吧,用
浏览器访问 localhost:8080/m
vc/welcome 就能看到页面了。