Flask接口签名sign原理与实例代码浅析
238
2023-07-13
spring mvc4.1.6 spring4.1.6 hibernate4.3.11 mysql5.5.25开发环境搭建图文教程
一、准备工作
开始之前,先参考上一篇:
struts2.3.24 + spring4.1.6 + hibernate4.3.11 + mysql5.5.25 开发环境搭建及OGOgA相关说明
思路都是一样的,只不过把struts2替换成了spring mvc
二、不同的地方
工程目录及jar包:
action包改成controller;
删除struts2 jar包,添加spring mvc包(已有的话,不需添加);
web.xml配置:
跟之前不同的地方是把struts2的过滤器替换成了一个servlet,主要目的是路由url,交给spring mvc处理;
applicationContext.xml配置:
不同的地方主要是配置自动扫描的时候,要排除@Controller组件,这些bean是由spring mvc 去生成的;
其它配置跟前一篇一样;
xmlns:p="http://springframework.org/schema/p" xmlns:context="http://springframework.org/schema/context" xmlns:tx="http://springframework.org/schema/tx" xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:aop="http://springframework.org/schema/aop" xmlns:jdbc="http://springframework.org/schema/jdbc" xsi:schemaLocation=" http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd http://springframework.org/schema/aop http://springframework.org/schema/aop/spring-aop.xsd http://springframework.org/schema/tx http://springframework.org/schema/tx/spring-tx-4.1.xsd http://springframework.org/schema/jdbc http://springframework.org/schema/jdbc/spring-jdbc-4.1.xsd http://springframework.org/schema/context http://springframework.org/schema/context/spring-context-4.1.xsd"> class="org.springframework.orm.hibernate4.HibernateTransactionManager">
xmlns:p="http://springframework.org/schema/p"
xmlns:context="http://springframework.org/schema/context"
xmlns:tx="http://springframework.org/schema/tx"
xmlns:xsi="http://w3.org/2001/XMLSchema-instance"
xmlns:aop="http://springframework.org/schema/aop"
xmlns:jdbc="http://springframework.org/schema/jdbc"
xsi:schemaLocation="
http://springframework.org/schema/beans
http://springframework.org/schema/beans/spring-beans.xsd
http://springframework.org/schema/aop
http://springframework.org/schema/aop/spring-aop.xsd
http://springframework.org/schema/tx
http://springframework.org/schema/tx/spring-tx-4.1.xsd
http://springframework.org/schema/jdbc
http://springframework.org/schema/jdbc/spring-jdbc-4.1.xsd
http://springframework.org/schema/context
http://springframework.org/schema/context/spring-context-4.1.xsd">
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
springmvc-servlet.xml配置:
配置自动扫描ssh.controller包下的@Controller,这里要恢复applicationContext.xml中配置的exclude-filter;
配置视图解析器,有很多解析器,这里以InternalResourceViewResolver为例;
xmlns:context="http://springframework.org/schema/context" xmlns:mvc="http://springframework.org/schema/mvc" xmlns:p="http://springframework.org/schema/p" xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:tx="http://springframework.org/schema/tx" xsi:schemaLocation=" http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans-4.1.xsd http://springframework.org/schema/context http://springframework.org/schema/context/spring-context-4.1.xsd http://springframework.org/schema/mvc http://springframework.org/schema/mvc/spring-mvc-4.1.xsd http://springframework.org/schema/tx http://springframework.org/schema/tx/spring-tx-4.1.xsd ">
xmlns:context="http://springframework.org/schema/context"
xmlns:mvc="http://springframework.org/schema/mvc"
xmlns:p="http://springframework.org/schema/p"
xmlns:xsi="http://w3.org/2001/XMLSchema-instance"
xmlns:tx="http://springframework.org/schema/tx"
xsi:schemaLocation="
http://springframework.org/schema/beans
http://springframework.org/schema/beans/spring-beans-4.1.xsd
http://springframework.org/schema/context
http://springframework.org/schema/context/spring-context-4.1.xsd
http://springframework.org/schema/mvc
http://springframework.org/schema/mvc/spring-mvc-4.1.xsd
http://springframework.org/schema/tx
http://springframework.org/schema/tx/spring-tx-4.1.xsd
">
编写controller:
由于使用spring mvc替换struts2,也就没有了action包了,删除,并新建controller包,在包下新建UserController类;
@RequestMapping:映射url;
@ResponseBody:内容直接作为body返回;
UserController.java
package ssh.controller;
import java.io.PrintWriter;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import ssh.aop.AopTest;
import ssh.model.User;
import ssh.service.UserService;
import com.google.gson.Gson;
@Controller
@RequestMapping("/user")
public class UserController {
Logger logger = Logger.getLogger(UserController.class);
@Resource
private UserService userService;
@Resource
private AopTest aopTest;
@RequestMapping(value="/addUser")
@ResponseBody
public void addUser(HttpServletRequest request, HttpServletResponse response){
PrintWriter out = null;
try{
response.setContentType("text/html;charset=UTF-8");
String account = request.getParameter("account");
String name = request.getParameter("name");
String address = request.getParameter("address");
User user = new User();
user.setAccount(account);
user.setAddress(address);
user.setName(name);
userService.add(user);
out = response.getWriter();
out.write(new Gson().toJson("success"));
}catch(Exception e){
e.printStackTrace();
logger.error(e.getMessage());
if(out != null)
out.write(new Gson().toJson("fail"));
}finally{
out.flush();
out.close();
}
}
@RequestMapping(value="/queryUser")
@ResponseBody
public void queryAllUser(HttpServletRequest request, HttpServletResponse response){
PrintWriter out = null;
aopTest.test1();
aopTest.test2();
try {
response.setContentType("text/html;charset=UTF-8");
Gson gson = new Gson();
List
String gsonStr = gson.toJson(userList);
out = response.getWriter();
out.write(gsonStr);
} catch (Exception e) {
e.printStackTrace();
logger.error(e.getMessage());
if(out != null)
out.write(new Gson().toJson("fail"));
}finally{
out.flush();
out.close();
}
}
}
三、运行程序
运行程序,添加用户、查询用户功能正常;
另外二级缓存也正常工作,第二次查询已经没有操作数据库了;
@author 风一样的码农
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~