怕忘记搭建步骤如下;
1.新建
项目新建完成
我们看到jsp页面在报错,我们只要配置jdk和tomcat 就好了
2.点击项目build path配置jdk1.8,tomcat 8.5
3.新增文件夹src/test/resorces,因为框架一般有4个,所以我们需要手动添加一个
tes
4.删除web.xml因为版本太低了,然后重新配置一个,如下
把勾去掉,点击apply,然后勾上选择2.5,然后配置web.xml
5.pom.xml配置引用
ssm_demo
junit
spring-context
spring-beans
spring-core
spring-expression
spring-aop
spring-aspects
spring-tx
spring-test
spring-jdbc
spring-web
spring-webmvc
spring-context-support
spring-web
mybatis
mybatis-spring
mysql-connector-java
jstl
slf4j-api
slf4j-log4j12
commons-fileupload
commons-io
commons-codec
jackson-databind
jackson-core
jackson-annotations
pagehelper
maven-compiler-plugin
6.配置文件
applicationContext.xml代码如下
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd ">
db.properties
jdbc.jdbcUrl=jdbc:mysql://localhost:3306/study
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.user=root
jdbc.password=123
log4j.properties
# Global logging configuration
log4j.rootLogger=DEBUG, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
---------------------
u4F5Cu8005uFF1A
u6765u6E90uFF1A
u539Fu6587uFF1A
u7248u6743u58F0u660EuFF1Au672Cu6587u4E3Au535Au4E3Bu539Fu521Bu6587u7AE0uFF0Cu8F6Cu8F7Du8BF7u9644u4E0Au535Au6587u94FEu63A5uFF01
mybatis.xml
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
spring-mvc.xml
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 ">
id="internalResourceViewResolver">
spring-mybatis.xml
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd ">
model.addAttribute("msg", "");
return "login";
}
@RequestMapping(value="/login")
public String login(Model model, // 向前台页面传的值放入model中
HttpServletRequest request // 从前台页面取得的值
){
String username = request.getParameter("username");
String password = request.getParameter("password");
boolean flag = LoginCheck(username, password);
if(flag){
return "userList";
}else{
model.addAttribute("msg", "用户名或密码不正确,请重新登录");
return "login";
}
}
private boolean LoginCheck(String username,String password){
User user = userService.selectByUserName(username);
if(user == null || "".equals(user)){
return false;
}
if(user.getPassword().equals(password)){
return true;
}else{
return false;
}
}
}
UserController.java
package com.demo.controller;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
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 org.springframework.web.bind.annotation.ResponseBody;
import com.demo.entity.Msg;
import com.demo.entity.User;
import com.demo.service.IUserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
@Controller
public class UserController {
@Autowired
IUserService userService;
@RequestMapping(value = "/user", method = RequestMethod.GET)
@ResponseBody
public Msg getUser(@RequestParam(value = "pn", defaultValue = "1") Integer pn) {// 分页参数
PageHelper.startPage(pn, 7);
// startPage后紧跟的查询即为分页查询
List
// 使用pageInfo包装查询后的结果集,封装详细的分页信息,5是连续显示5页
PageInfo pageInfo = new PageInfo(list, 5);
return Msg.success().add("pageInfo", pageInfo);
}
@RequestMapping(value = "/user", method = RequestMethod.POST)
@ResponseBody
public Msg addUser(User user) {
userService.addUser(user);
return Msg.success();
}
}
login.jsp
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd ">



