简单搭建springmvc,没有用到maven
1、勾选
2、
3、生成项目了
此时无法运行,需要一些配置
4、
5、
就算配置tomcat,也可能运行不了,因为缺了东西,按照这一步进行
6、开始配置tomcat
7、选择服务器
8、看图片
9、有报错的,因为没有加载的war包,tomcat不知道运行啥项目,看图
10,IDEA很智能,进来就主动生成了war包。application context名字难看,还是不要写了,如果写了,路径就变为:localhost:8080/springmvc_war_exploded/
11、项目启动成功了
12、进入index.jsp页面
13、一些需要注意的
如果在1添加的新的内容,在2也要加上,否则运行的时候会显示找不到类
看这里,建立需要的目录,配置jdbc
太麻烦了,我直接贴代码了,HelloControl与TestControl是一样的,只粘贴一个好了
package test.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;
import java.util.Map;
@Controller
@RequestMapping("/mvc")
public class HelloControl {
@Autowired
JdbcTemplate jdbcTemplate;
//跳页面
@RequestMapping("/hello")
public String helle(){
return "index";
}
//返回json格式数据
@RequestMapping("/mysql")
@ResponseBody
public List<Map<String, Object>> hello(){
List<Map<String, Object>> list = jdbcTemplate.queryForList("SELECT SCENE_ID as sceneId,PARAM_FIELD as paramField,PARAM_NAME as paramName,( CASE WHEN PARAM_MAP_FIELD IS NULL THEN PARAM_FIELD WHEN PARAM_MAP_FIELD = '' THEN PARAM_FIELD ELSE PARAM_MAP_FIELD END ) paramMapField,PARAM_TYPE as paramType,PARAM_DESC as paramDesc,IS_MUST as isMust FROM param_config WHERE IS_MUST = 'Y'");
for (Map<String,Object> map:list){
System.out.println(map.toString());
}
return list;
}
}
index.jsp
<%--
Created by IntelliJ IDEA.
User: *******
Date: 2019/11/22
Time: 14:37
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<h2>Hello Word!!!</h2>
</body>
</html>
applicationContext.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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config/>
<context:property-placeholder location="/WEB-INF/jdbc.properties"/>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName"><value>${jdbc.driverClassName}</value></property>
<property name="url"><value>${jdbc.url}</value></property>
<property name="username"><value>${jdbc.username}</value></property>
<property name="password"><value>${jdbc.password}</value></property>
</bean>
<bean id="jdbcTemplate" name="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
</beans>
dispatcher-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: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.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.xsd">
<context:component-scan base-package="test.controller"/><!--注入控制层,如果有其他层,可以将范围写大一点-->
<mvc:default-servlet-handler/>
<mvc:annotation-driven/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
id="internalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
jdbc.properties,如果本机用localhost不行,可能与IDEA有关,那就写具体的IP地址
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test?useSSL=false&allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8
jdbc.username=root
jdbc.password=root
到此为止,还缺了一些包(jdbc、加载外部文件jdbc.properties也需要包),其实jdbc包是已经有了,但是tomcat并不加载进来,这个要自己加载进去,反正我的是不能自动加载进去的,另外还有一些jar包也是.
去下载包,放在lib上,至于放在其他磁盘路径,应该也行,没有试过
导入包,spring-jdbc,spring-tx,mysql-connector-java,jackson-annotations,jackson-core,jackson-databind
到了这一步,还是差了一点,因为你已经改了一些东西,但是tomcat的war包没有改变,所以要更新一下
好了,启动成功,如果还是不行的,看看xml的头部是不是缺了内容,如果不是,就看看是不是少了jar包。