Spring 4.2.4.RELEASE MVC 学习笔记 - 6.2
本小结没有什么重点内容,只是把后面涉及到一些文件和代码复制出来,方便大家参考。
PS:因有些文件可能我有些改动,但是忘记该那些了,之前写过的东西我也不想回头修改了,索性这里重新粘帖出来大家看一下就好了。
/framework_spring/pom.xml
<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<!-- 因为是 _total项目的子项目 -->
<groupId>cn.vfire.frameword</groupId>
<artifactId>total</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>framework_spring</artifactId>
<packaging>war</packaging>
<name>framework_spring Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<org_springframework_version>4.2.4.RELEASE</org_springframework_version>
</properties>
<dependencies>
<!-- 导入junit jar包 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- 导入@Getter @Setter自动编码工具jar -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.6</version>
</dependency>
<!-- 导入jsp servlet规范jar包 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
</dependency>
<!-- 导入spring框架依赖jar包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${org_springframework_version}</version>
</dependency>
<dependency>
<!-- 该jar用于spring 支持 junit测试 -->
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${org_springframework_version}</version>
</dependency>
<!-- spring-context-support,该jar引入能使spring mvc非常好的对freemark的支持 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${org_springframework_version}</version>
</dependency>
<!-- 导入freemarker视图解析框架jar与sprig集成 -->
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.22</version>
</dependency>
<!-- 导入log4j的jar包 -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!-- 导入Gson json工具包 -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.6</version>
</dependency>
<!-- _total的引用子项目 -->
<dependency>
<groupId>cn.vfire.frameword</groupId>
<artifactId>framework_common</artifactId>
<version>${project.parent.version}</version>
</dependency>
</dependencies>
<build>
<finalName>framework_spring</finalName>
<!-- 修改maven编译输出目录 -->
<outputDirectory>src/main/webapp/WEB-INF/classes</outputDirectory>
<testOutputDirectory>src/main/webapp/WEB-INF/classes</testOutputDirectory>
<plugins>
<!-- 添加一个mavne的插件,作用是在我通过maven发布的时候,能将依赖的jar复制一份到我指定的目录下。(个人比较懒) -->
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>copy-lib-src-webapps</id>
<phase>package</phase>
<configuration>
<tasks>
<delete dir="src/main/webapp/WEB-INF/lib" />
<copy todir="src/main/webapp/WEB-INF/lib">
<fileset dir="${project.build.directory}\${project.build.finalName}\WEB-INF\lib">
<include name="*" />
</fileset>
</copy>
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
cn.vfire.framework.spring.mvc.view.XModelAndView
package cn.vfire.framework.spring.mvc.view;
import java.util.Map;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.View;
public class XModelAndView extends ModelAndView {
public XModelAndView() {
super();
}
public XModelAndView(String viewName) {
super(viewName);
}
public XModelAndView(String viewName, Map<String, ?> model) {
super(viewName, model);
}
public XModelAndView(String viewName, String modelName, Object modelObject) {
super(viewName, modelName, modelObject);
}
public XModelAndView(View view) {
super(view);
}
public XModelAndView(View view, Map<String, ?> model) {
super(view, model);
}
public XModelAndView(View view, String modelName, Object modelObject) {
super(view, modelName, modelObject);
}
/**
* 返回JSON报文。
*
* @return
*/
public String toJson() {
Result rs = new Result();
Map<String, Object> mode = super.getModel();
if (mode != null && mode.isEmpty() == false) {
if (mode.size() == 1) {
String k = mode.keySet().iterator().next();
rs.setData(mode.get(k));
}
if (mode.size() > 1) {
rs.setData(mode);
}
}
return rs.toJson();
}
}
cn.vfire.framework.spring.mvc.view.Result
package cn.vfire.framework.spring.mvc.view;
import lombok.Getter;
import lombok.Setter;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
public class Result {
@Getter
@Setter
private int code = 0;
@Getter
@Setter
private String error = "SUCCESS";
@Getter
@Setter
private String msgcode = "0000";
@Getter
@Setter
private String msg = "";
@Getter
@Setter
private Object data;
public Result() {
this.code = 0;
this.error = "";
}
public Result(Object data) {
this.code = 0;
this.error = "";
this.data = data;
}
public Result(String error) {
this.code = 1;
this.error = error;
}
public String toJson() {
Gson gson = new GsonBuilder().create();
return gson.toJson(this);
}
}
/framework_spring/src/main/webapp/user.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>测试页面</title>
<style type="text/css">
.vform{
width: 24%;
border: 1px #CCCCCC solid;
font-size: 12px;
float: left;
margin: 5px;
padding: 5px;
}
.vform .h1{
font-weight: bold;
padding: 4px;
}
.vform input{
width: 120px;
}
</style>
</head>
<body>
<div class="vform">
<div class="h1">HTTP POST 添加用户 /api/userAdd.api</div>
<form action="/api/userAdd.api" method="post">
<div>用户名:<input name="username" value="xiaohong" /></div>
<div>姓名:<input name="name" value="小红" /></div>
<div>年龄:<input name="age" value="22" /></div>
<div>性别:<select name="sex"><option value="0" selected="selected">女</option></select></div>
<div><button type="submit">添加新用户</button></div>
</form>
<hr />
</div>
<div class="vform">
<div class="h1">HTTP GET 查询用户 /api/userQuery.api</div>
<form action="/api/userQuery.api" method="get">
<div>用户名:<input name="username" value="xiaohong" /></div>
<div><button type="submit">查询</button></div>
</form>
<hr />
</div>
<div class="vform">
<div class="h1">HTTP POST 查询用户 /api/userSave.api</div>
<form action="/api/userSave.api" method="post">
<div>用户名:<input name="username" value="xiaohong" /></div>
<div>姓名:<input name="name" value="小红" /></div>
<div>年龄:<input name="age" value="23" /></div>
<div>性别:<select name="sex"><option value="0" selected="selected">女</option></select></div>
<div><button type="submit">添加新用户</button></div>
</form>
<hr />
</div>
<div class="vform">
<div class="h1">HTTP GET 查询用户 /api/userDel.api</div>
<form action="/api/userDel.api" method="get">
<div>用户名:<input name="username" value="xiaohong" /></div>
<div><button type="submit">添加新用户</button></div>
</form>
<hr />
</div>
</body>
</html>
/framework_spring/src/main/resources/config/spring/spring-mvc-servlet.xml
追加了解决Spring mvc直接返回字符串中文乱码的问题。因为Spring默认用的IOS8859-1字符集编码。
/framework_spring/src/main/resources/config/spring/spring-mvc-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p" 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-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd">
<!-- 开启spring 上下文注解支持 -->
<context:annotation-config />
<!-- 把标记了@Controller注解的类转换为bean -->
<context:component-scan base-package="cn.vfire.framework" />
<!-- 开启spring mvc注解支持 -->
<mvc:annotation-driven>
<mvc:message-converters register-defaults="true">
<!-- 解决response字符串中文乱码问题 -->
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
<!-- 如果当前请求为“/”时,则转发到“/helloworld/index” -->
<mvc:view-controller path="/" view-name="forward:/index.jsp" />
<!-- 设置默认的Servlet来响应静态文件 -->
<mvc:resources mapping="/resource" location="/resource" />
<!-- 当上面要访问的静态资源不包括在上面的配置中时,则根据此配置来访问 -->
<mvc:default-servlet-handler />
<!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
</beans>
cn.vfire.framework.spring.mvc.controller.UserController
package cn.vfire.framework.spring.mvc.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import cn.vfire.framework.spring.mvc.mode.UserMode;
import cn.vfire.framework.spring.mvc.view.XModelAndView;
@RequestMapping("/api")
@RestController
public class UserController {
@RequestMapping(path = "/userAdd.api", method = RequestMethod.POST)
public String userAdd(UserMode user) {
XModelAndView modeView = new XModelAndView();
modeView.addObject("user", user);
System.out.println(String.format("添加用户%s完成", user.getName()));
return modeView.toJson();
}
@RequestMapping(path = "/userQuery.api", method = RequestMethod.GET)
public String userQuery(String username) {
XModelAndView modeView = new XModelAndView();
UserMode user = new UserMode();
{
user.setUsername(username);
user.setName("小红");
user.setAge(22);
user.setSex(0);
}
modeView.addObject("user", user);
System.out.println(String.format("查询用户名为%s用户 信息成功", user.getUsername()));
return modeView.toJson();
}
@RequestMapping(path = "/userSave.api", method = RequestMethod.POST)
public String userSave(UserMode user) {
XModelAndView modeView = new XModelAndView();
user.setAge(23);
modeView.addObject("user", user);
System.out.println(String.format("更新用户名为%s用户信息成功", user.getUsername()));
return modeView.toJson();
}
@RequestMapping(path = "/userDel.api", method = RequestMethod.GET)
public String userDel(String username) {
XModelAndView modeView = new XModelAndView();
System.out.println(String.format("删除用户名为%s用户信息成功", username));
return modeView.toJson();
}
}