作者对javaweb开发了解并不深,此项目为练手学习之用,大牛请绕道或添加过滤器。
首先看用到的jar包,通过几个钟头的排错,jar包终于搞齐了。
源码结构图,此图也是作者通过网上搜寻查找总结的结果。
webContent目录:
web.xml源码:
homeBank
index.jsp
org.springframework.web.context.ContextLoaderListener
contextConfigLocation
classpath:spring/applicationContext-*.xml
dispatcher
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:spring/applicationContext-mvc.xml
1
dispatcher
*.do
characterEncodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
forceEncoding
true
characterEncodingFilter
/*
text/html;charset=UTF-8
classpath:system/homebank/entity/*.xml
上面的MapperScannerConfigurer配置会扫描BaseMapper下的所有接口,这样就不用单独为每一个接口配置一个bean。数据源的配置没有通过jdbc.properties文件,因为用户名总是读取计算机名而不是文件中配置的用户名,我也不知道为啥。
applicationContext-bean.xml配置如下:
下面看mybatis的配置,config.xml:
以上是几个要命的配置文件,下面的内容就简单了。
首先是实体类User:
package system.homebank.entity;
import java.io.Serializable;
public class User implements Serializable
{
private static final long serialVersionUID = 1085062678388838031L;
private Integer id;
private String username;
private String password;
public Integer getId()
{
return id;
}
public void setId(Integer id)
{
this.id = id;
}
public String getUsername()
{
return username;
}
public void setUsername(String username)
{
this.username = username;
}
public String getPassword()
{
return password;
}
public void setPassword(String password)
{
this.password = password;
}
}
package system.homebank.mapper;
import java.util.List;
import system.homebank.entity.User;
public interface UserMapper extends BaseMapper
{
public List getAllUser();
}
package system.homebank.mapper;
public interface BaseMapper
{
}
以上是所有关于mybaits的配置,下面就是结构清晰的MVC三层了。
dao层如下:
UserDao.java
package system.homebank.dao;
import java.util.List;
import system.homebank.entity.User;
public interface UserDao
{
public List getAllUser();
}
package system.homebank.dao;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Repository;
import system.homebank.entity.User;
import system.homebank.mapper.UserMapper;
@Repository
public class UserDaoImpl implements UserDao
{
@Resource
private UserMapper mapper;
@Override
public List getAllUser()
{
return this.mapper.getAllUser();
}
}
service层如下:
UserService.java
package system.homebank.service;
import java.util.List;
import system.homebank.entity.User;
public interface UserService
{
public List getAllUser();
}
package system.homebank.service;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import system.homebank.dao.UserDao;
import system.homebank.entity.User;
@Service
public class UserServiceImpl implements UserService
{
@Resource
private UserDao dao;
@Override
public List getAllUser()
{
return this.dao.getAllUser();
}
}
cotroller层如下:
UserController.java
package system.homebank.controller;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import system.homebank.entity.User;
import system.homebank.service.UserService;
@Controller
@RequestMapping("/userController")
public class UserController
{
@Resource
private UserService service;
@RequestMapping("/list.do")
@ResponseBody
public Object list(Model model)
{
List list = service.getAllUser();
return list;
}
}
这样,list方法的访问路径就是/userController/list.do,@ResponseBody标签表示直接返回结果到页面。
后台代码到此为止。
以下为jsp页面和前台代码:
index.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path ;
%>
Insert title here
点击
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path ;
%>
Insert title here