ssm搭建 (2)

新建springmvc-config.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/context http://www.springframework.org/schema/mvc "> <!--springmvc 只管扫描 controller包--> <context:component-scan base-package="com.selton.hellossm.controller"></context:component-scan> <!-- 让spring-mvc支持注解 --> <mvc:annotation-driven> </mvc:annotation-driven> </beans> 使用

这时完成了后台的配置,让我们实现一个简单地登录系统

用基本maven项目搭建出来的工程骨架里没有webapp

我们需要在src/main下新建文件夹webapp
当然不需要手动创建
idea有自动化的功能 ---链接

在webapp下新建loginfailed.html

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> login failed </body> </html>

新建loginsuccess.html

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> login success </body> </html>

新建index.html

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <form action="" method="get"> name: <input type="text"> <br> password: <input type="password"> <br> <input type="submit" value="登录"> </form> </body> </html>

在web.xml中写入

<?xml version="1.0" encoding="UTF-8"?> <web-app 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 " version="3.1"> <!-- 这个全局参数的作用是:加载Spring-config的配置文件--> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-config.xml</param-value> </context-param> <!--Spring的一个核心监听器,对Spring容器进行初始化动作 即我们以前的ApplicationContext context=new ClassPathXml..... --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!--SpringMVC的核心Servlet--> <servlet> <servlet-name>spring-mvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc-config.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>spring-mvc</servlet-name> <url-pattern>*.php</url-pattern> </servlet-mapping> <!-- 处理乱码--> <filter> <filter-name>encoding</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>encoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>

完成所有的配置工作后
开始写后台代码

在数据库中新建

/* Navicat Premium Data Transfer Source Server : link1 Source Server Type : MySQL Source Server Version : 50622 Source Host : localhost:3306 Source Schema : db_test1 Target Server Type : MySQL Target Server Version : 50622 File Encoding : 65001 Date: 10/07/2018 11:22:41 */ SET NAMES utf8mb4; SET FOREIGN_KEY_CHECKS = 0; -- ---------------------------- -- Table structure for user -- ---------------------------- DROP TABLE IF EXISTS `user`; CREATE TABLE `user` ( `id` int(11) NOT NULL, `name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, `password` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, `age` int(11) NULL DEFAULT NULL, PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact; SET FOREIGN_KEY_CHECKS = 1;

加入lombok依赖----链接
不使用lombok的话,去掉@Data
然后自己添上无参构造,等各种get,set
在entities中新建实体类User

@Data public class User { private int id; private String name; private String password; private int age; }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zyyzyy.html