SSM框架搭建简单的前后台分离项目(前台篇)
来源:互联网 发布:免费行业报告下载知乎 编辑:程序博客网 时间:2024/05/16 14:20
前台搭建
接上一篇,本篇介绍前台项目的搭建。因为和后台项目有很多相似的地方,因此本篇只给出关键代码,完整项目可以在文章末尾下载。
目录结构
关键代码
- Controller
package com.xiaojian.stage.controller;import com.xiaojian.stage.entity.Person;import com.xiaojian.stage.service.IPersonService;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RequestParam;import javax.annotation.Resource;/** * Created by xiaojian on 2017/9/5. */@Controllerpublic class PersonController { @Resource private IPersonService personService; @RequestMapping(value = "/me/person",produces = "text/html;charset=UTF-8",method = RequestMethod.GET) public String getPerson(@RequestParam("value") String data, Model model){ Person person = personService.getPerson(data); if (person!=null){ model.addAttribute("p",person); return "index"; }else{ return "error"; } } @RequestMapping("/index") public String index(){ return "index"; }}
- URL配置文件
因为是前后台,所以需要两台服务器同时运行。其中一台服务器需要修改端口号。
url.properties
#getpersonUrl=http://127.0.0.1:8080/back4/base/person
- 核心工具类HttpUtil
这里用到了HttpClient,关于HttpClient的更多用法可以自行搜索。
package com.xiaojian.stage.util;import org.apache.commons.configuration.Configuration;import org.apache.commons.configuration.ConfigurationException;import org.apache.commons.configuration.PropertiesConfiguration;import org.apache.http.HttpResponse;import org.apache.http.client.methods.HttpGet;import org.apache.http.client.methods.HttpPost;import org.apache.http.client.utils.URIBuilder;import org.apache.http.entity.StringEntity;import org.apache.http.impl.client.CloseableHttpClient;import org.apache.http.impl.client.HttpClients;import org.apache.http.util.EntityUtils;import org.apache.log4j.Logger;import java.io.IOException;import java.net.URI;import java.net.URISyntaxException;/** * Created by xiaojian on 2017/9/1. */public class HttpUtil { private static Logger logger = Logger.getLogger(HttpUtil.class); private static final String READ_FAILED = "{\"result\":\"read failed!\"}"; public static String getData(String data,String urlName,String type){ String url = getUrl(urlName); if (url==null){ return READ_FAILED; } String responseData = getRequest(data,url,type); logger.info("response---------------->"+responseData); return responseData; } //发送请求 public static String getRequest( String data,String url,String type){ CloseableHttpClient httpClient = HttpClients.createDefault(); HttpResponse httpResponse=null; try { if (type.equals("GET")) { HttpGet httpGet = new HttpGet(getURI(url, data)); logger.info("访问地址---------------->" + httpGet.getURI()); httpResponse = httpClient.execute(httpGet); } else if (type.equals("POST")) { HttpPost httpPost = new HttpPost(url); httpPost.setEntity(new StringEntity(data, "UTF-8")); logger.info("访问地址---------------->" + httpPost.getURI()); } }catch (IOException e){ e.printStackTrace(); } return getResponse(httpResponse); } //获得返回数据 public static String getResponse(HttpResponse response){ String responseData="{\"result\":\"访问失败!\"}"; if (response!=null){ int statusCode = response.getStatusLine().getStatusCode(); switch (statusCode){ case 200: try { responseData=EntityUtils.toString(response.getEntity(),"UTF-8"); } catch (IOException e) { e.printStackTrace(); } break; case 404: responseData="{\"result\":\"404!请求的网页不存在!\"}"; break; case 500: responseData="{\"result\":\"500!服务不可用!\"}"; break; } } return responseData; } //生成URI public static URI getURI(String url,String data){ URI uri = null; try { uri = new URIBuilder(url) .setParameter("json",data) .build(); }catch (URISyntaxException e){ e.printStackTrace(); } return uri; }//读取url public static String getUrl(String urlName){ Configuration configuration = null; try { configuration = new PropertiesConfiguration("url.properties"); } catch (ConfigurationException e) { e.printStackTrace(); } return configuration.getString(urlName); }}
配置文件
- spring-mvc.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: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-4.0.xsd"> <!--扫描@Controller--> <context:component-scan base-package="com.xiaojian.stage.controller"/> <context:component-scan base-package="com.xiaojian.stage.serviceImpl"/> <!--开启注解驱动--> <mvc:annotation-driven /> <!--视图模式--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"/> <property name="suffix" value=".jsp"/> </bean></beans>
- web.xml
<?xml version="1.0" encoding="UTF-8"?><web-app id="WebApp_ID" version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3.0.xsd"> <display-name>back</display-name> <welcome-file-list> <welcome-file>/index</welcome-file> </welcome-file-list> <!--log4j监听器 要放在spring的linstener之前--> <context-param> <param-name>log4jConfigLocation</param-name> <param-value> /WEB-INF/classes/log4j.properties </param-value> </context-param> <!-- Spring刷新Log4j配置文件变动的间隔,单位为毫秒 --> <context-param> <param-name>log4jRefreshInterval</param-name> <param-value>10000</param-value> </context-param> <listener> <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> </listener> <servlet> <servlet-name>SpringMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:/spring/spring-mvc.xml</param-value> </init-param> <!--启动时加载--> <load-on-startup>1</load-on-startup> <async-supported>true</async-supported> </servlet> <servlet-mapping> <servlet-name>SpringMVC</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!--拦截默认首页--> <servlet-mapping> <servlet-name>SpringMVC</servlet-name> <url-pattern>/index</url-pattern> </servlet-mapping> </web-app>
运行
到此前后台就搭建完成了,启动后台,运行前台后弹出界面:
点击查询:
完整项目下载
点此下载完整项目。。。。
阅读全文
1 0
- SSM框架搭建简单的前后台分离项目(前台篇)
- SSM框架搭建简单的前后台分离项目(后台篇)
- 利用tp框架做前后台分离
- ssm框架的简单搭建
- CAS不使用CAS服务器登录界面(适用于前后台完全分离的项目)
- CI项目架构(1)---前后台分离
- ssm项目的简单搭建
- 使用idea搭建一个简单的SSM框架:(1)使用idea创建maven项目
- 前后台数据如何交互(SSM)
- symfony前后台分离
- 前后台分离思想
- 简单的SSM框架的搭建
- SSM框架中前后台json数据交互的相关知识
- SSM简单项目搭建
- IDEA搭建简单的SSM(spring+springMVC+mybatis)框架
- 搭建简单的ssm maven框架
- 【IDEA】简易的前后台分离项目如何划分?如何引入其他module?
- SSM框架搭建(Maven项目)
- java 对象new的理解
- Android进程间(IPC机制)通信(Bundler,Messenger,AIDL,ContentProvider)
- 单例模式
- 输入一个整数,输出该数二进制表示中1的个数。其中负数用补码表示。
- Mybatis传参与返回值
- SSM框架搭建简单的前后台分离项目(前台篇)
- Mybaits根据数据库表自动生成entity,dao,mapper相关配置文件
- 报数—循环链表
- Java知识:初识集合类
- PHP 正则表达式匹配 preg_match 与 preg_match_all 函数
- Chrome学习笔记(二):UI组件,皮肤引擎 —— 基础设施篇
- Jsp使用<c:forEach>遍历List集合
- ios 安装yasm(转)
- GYM 100488 Yet Another Goat in the Garden