項目框架搭建
阿新 • • 發佈:2017-09-15
技術 trap mysq sql spring c-s jcp 3.1 blog
本文根據個人喜好記錄“騰訊課堂”的《Java項目之Maven+SpringMVC+Spring+Mybatis+MySql消費查詢系統》視頻教程關鍵步驟信息,視頻地址:https://ke.qq.com/course/193395
1. 前端框架jquery+bootstrap導入
2. 集成springmvc+spring
2.1 修改web.xml的域名空間如下
<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"> </web-app>
2.2 pom文件中添加servlet、jstl、spring相關依賴
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api--> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency> <!-- https://mvnrepository.com/artifact/javax.servlet/jstl--> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency>
其中,spring選擇的版本號如下
<spring.version>4.3.11.RELEASE</spring.version>
2.3 web.xml添加springmvc配置
<filter> <filter-name>encoding-filter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> </filter> <filter-mapping> <filter-name>encoding-filter</filter-name> <servlet-name>spring-mvc-servlet</servlet-name> </filter-mapping> <servlet> <servlet-name>spring-mvc-servlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-mvc.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>spring-mvc-servlet</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping>
2.4 添加spring-mvc.xml配置
<mvc:annotation-driven/> <context:component-scan base-package="com.pawn.vip.web.controller"/>
2.5 編寫測試類驗證
@Controller
public class HelloController {
@ResponseBody
@RequestMapping("/hello.do")
public String testHello(){
return "hello spring mvc" ;
}
}
結果如下:
2.6 web.xml添加spring配置
<listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-context.xml</param-value> </context-param>
2.7 添加spring-context.xml配置文件
<context:component-scan base-package="com.pawn.vip.service"/>
2.8 編寫測試類
@Service
public class HelloService {
public String getHelloString(){
return "hello spring mvc from service" ;
}
}
@Controller
public class HelloController {
@Autowired
HelloService helloService ;
@ResponseBody
@RequestMapping("/hello.do")
public String testHello(){
return helloService.getHelloString() ;
}
}
結果如下:
項目框架搭建