基于tomcat的ssm项目结构(备份)

首先添加tomcat服务器:window-preferences-server-runtime environments-add-你的服务器地址

基于tomcat的ssm项目结构(备份)_第1张图片

window-showview-servers在你的工具栏中会出现servers

基于tomcat的ssm项目结构(备份)_第2张图片

Click将tomcat7添加finish即可,注意先finish,然后双击出现的服务器选中图中的选项,这样项目之后就会自动配置到tomcat中一个你可以查看文件夹wtpwebapps中,但真正的运行文件并不在这里,可以方便你查看项目的状况

基于tomcat的ssm项目结构(备份)_第3张图片

新建一个maven项目,在项目栏中右击new-project-maven-mavenproject

基于tomcat的ssm项目结构(备份)_第4张图片在图中选中部位右击-Generate D D S

maven的配置可以看一些其他的文章

这个时候我们需要将项目加入到服务器中

右击项目-properties-targeted runtimes

基于tomcat的ssm项目结构(备份)_第5张图片

在之前的servers服务器上右击-add and remove

基于tomcat的ssm项目结构(备份)_第6张图片

将项目加入finish

publish是部署,start是开始,stop,restart...

我们会在项目列表中还看到servers项目

基于tomcat的ssm项目结构(备份)_第7张图片这就是服务器的一些配置文件,打开下面的server.xml文件我们可以更改字符集,端口号,协议,包括可以添加线程池大小等等,加入的项目也会在最后配置

基于tomcat的ssm项目结构(备份)_第8张图片先关注的两个文件pom.xml和web.xml文件,前者是配置需要引入的包,我会列出一些我用过的,对于不同的环境可能不同的版本才能使用,可以自己调节一下version

pom.xml


	4.0.0
	cn.ck
	MusicWebsite
	0.0.1-SNAPSHOT
	war
	
		
		
			dom4j
			dom4j
			1.6.1
		
		
		
			mysql
			mysql-connector-java
			5.1.40
		
		
		
			jstl
			jstl
			1.2
		
		
		
			commons-dbcp
			commons-dbcp
			1.4
		
		
		
			junit
			junit
			4.12
			test
		
		
		
			org.springframework
			spring-webmvc
			3.2.7.RELEASE
		
		
		
			org.mybatis
			mybatis
			3.2.8
		
		
			org.mybatis
			mybatis-spring
			1.3.1
		
		
			org.springframework
			spring-jdbc
			3.2.8.RELEASE
		
		
		
			com.fasterxml.jackson.core
			jackson-core
			2.8.8
		
		
			com.fasterxml.jackson.core
			jackson-annotations
			2.8.5
		
		
			com.fasterxml.jackson.core
			jackson-databind
			2.8.8.1
		
		
		
			org.springframework
			spring-aop
			3.2.7.RELEASE
		
		
			org.aspectj
			aspectjweaver
			1.8.0
		
		
			org.aspectj
			aspectjtools
			1.8.0
		
		
		
			commons-fileupload
			commons-fileupload
			1.3.3
		
		
		
			org.apache.poi
			poi-ooxml
			3.16
		
		
		
			commons-codec
			commons-codec
			1.9
		
		
		
			org.apache.tomcat
			tomcat-jsp-api
			9.0.2
		
	

对于后者web.xml这是配置访问相关文件的,我们了解该服务器的访问流程:浏览器请求http://localhost:8080/项目名/自定义路径-服务器会先从web.xml中查找是否有符合的路径url-pattern,从而访问对应的servlet和filter,如果没有则去找静态的网页资源,所以我们可以在这里配置ssm需要的已存在的类,当然你可以手写或者自动配置



	DemoProject
	
		web/index.html
		web/index.htm
		web/index.jsp
		web/default.html
		web/default.htm
		web/default.jsp
	
	
		
		DispatcherServlet
		DispatcherServlet
		org.springframework.web.servlet.DispatcherServlet
		
			
			contextConfigLocation
			classpath:spring/*.xml
		
	
	
		DispatcherServlet
		*.do
	
	
		CharacterEncodingFilter
		CharacterEncodingFilter
		org.springframework.web.filter.CharacterEncodingFilter
		
			
			encoding
			utf-8
		
	
	
		CharacterEncodingFilter
		*.do
	

Filter过滤器会先遭到访问,DispatcherServlet是总的处理类

在init-param中配置字符集和spring框架的配置文件路径,这个配置文件主要是配置spring框架控制的类的信息,具体的位置如图

基于tomcat的ssm项目结构(备份)_第9张图片会遵循一定的schema格式的xml文件,我们注意到通配符*,所以对于该文件夹下的所有都会被扫描到,这里为了方便只写在一个文件中

spring-mvc.xml



	自动扫描注解
	spring注解的驱动
	
		
		
	显示的jsp文件的路径,相对webapp文件夹
	数据库的信息提取
	配置连接池
		
		
		
		
		
		
	
	sqlsessionmybatis的重要组建连接池,映射文件
		
		
	
	sqlsession和映射类的包
		
		
	
	数据库事物处理
		
	
	
	
		
			
			
			
		
	
	
	  
           
           
           
           
           
           
      
  	自动扫描注解
	spring注解的驱动
	
		
		
	显示的jsp文件的路径,相对webapp文件夹
	数据库的信息提取
	配置连接池
		
		
		
		
		
		
	
	sqlsessionmybatis的重要组建连接池,映射文件
		
		
	
	sqlsession和映射类的包
		
		
	
	数据库事物处理
		
	
	
	
		
			
			
			
		
	
	
	  
           
           
           
           
           
           
      
  

基于tomcat的ssm项目结构(备份)_第10张图片

db.properties的内容

driverclassname:com.mysql.jdbc.Driver
url:jdbc:mysql://localhost:3306/musicwebsite
username:root
password:11111
initialsize:30
maxactive:50

musiclistmapper.xml的内容

  


	
	
	
	
	
	
		update song set love=#{love} where guid=#{guid}
	
	
	
  

接下来我们从项目包来走一下流程看一下如何使用

基于tomcat的ssm项目结构(备份)_第11张图片

请求-web.xml-dispatcherservlet-spring配置文件-interceptor-请求路径对应的controller-调用service方法-调用mapper方法-对应类的mybatis动态代理技术访问mybatis配置文件-controller返回内容到浏览器或者jsp文件

entity包中是面向对象的实体类,例如user,song等等

基于tomcat的ssm项目结构(备份)_第12张图片

这里只是一些配置流程

例如访问http://localhost:8080/MusicWebSite/musiclist/gohomepage.do就会根据返回值访问web下的musicwebsite.jsp文件

package cn.ck.demoproject.controller;

import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.ck.demoproject.entity.ResponseResult;
import cn.ck.demoproject.entity.Song;
import cn.ck.demoproject.entity.Type;
import cn.ck.demoproject.service.MusicListService;

@Controller
@RequestMapping("/musiclist")
public class MusicListController {
	@Resource(name = "musicListService")
	MusicListService service;

	@RequestMapping("/namelist.do")
	public String getListByName(HttpServletRequest request, String keyword, String page) {
		List singernamesongs = service.getSongsBySingerName(keyword);
		List songnamesongs = service.getSongsBySongName(keyword);
		Set songs = new HashSet();
		songs.addAll(singernamesongs);
		songs.addAll(songnamesongs);
		int pages = songs.size() / 5;
		if (songs.size() % 5 > 0) {
			pages++;
		}
		System.out.println(pages);
		for (Song song : songs) {
			String str = song.getSongSinger();
			song.setSongSinger(service.getSingerName(str));
		}
		List songssend = new ArrayList();
		Iterator it = songs.iterator();
		if (page == null) {
			page = "1";
		}
		int i = Integer.parseInt(page);
		int j = i * 5;
		int k = 0;
		while (it.hasNext() && k < j) {
			Song song1 = it.next();
			System.out.println("....." + song1);
			if (k >= ((i - 1) * 5)) {
				songssend.add(song1);
			}
			k++;
		}
		request.setAttribute("songs", songssend);
		request.setAttribute("searchkeyword", keyword);
		request.setAttribute("pages", pages);
		return "musiclist";
	}

	@RequestMapping("/gohomepage.do")
	public String goHomePage(HttpServletRequest request) {
		List types = service.getFiveTypes();
		List hotsongs = service.getTenHotSongs();
		for (Song song : hotsongs) {
			song.setSongSinger(service.getSingerName(song.getSongSinger()));
		}
		List newsongs = service.getTenNewSongs();
		for (Song song : newsongs) {
			song.setSongSinger(service.getSingerName(song.getSongSinger()));
		}
		request.setAttribute("types", types);
		request.setAttribute("hotsongs", hotsongs);
		request.setAttribute("newsongs", newsongs);
		return "musicwebsite";
	}

	@RequestMapping("/love.do")
	@ResponseBody
	public ResponseResult love(String guid) {
		ResponseResult rr = new ResponseResult();
		service.addLove(guid);
		rr.setState(ResponseResult.OK);
		rr.setMessage("OK");
		return rr;
	}

	@RequestMapping("/download.do")
	@ResponseBody
	public byte[] dowload(HttpServletResponse response, HttpServletRequest request, String guid, String size,
			String path) throws Exception {
		// 手工设置 下载头 Content-Disposition
		System.out.println(path.split("/")[1]);
		response.setHeader("Content-Disposition", "attachment; filename=\"" + path.split("/")[1] + "\"");
		System.out.println(request.getRealPath(path));
		FileInputStream fis = new FileInputStream(request.getRealPath(path));
		byte[] bs = new byte[Integer.parseInt(size)];
		int len = 0;
		while ((len = fis.read(bs)) != -1) {

		}
		return bs;
	}

}
package cn.ck.demoproject.entity;

import java.io.Serializable;
import java.util.Date;

public class Song implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = -6647441379708061230L;
	private String guid;
	private String songName;
	private String songPath;
	private String songWord;
	private String songSize;
	private String songSinger;
	private String songCollection;
	private String songType;
	private String remark;
	private String image;
	private String love;
	private Date songTime;

	public String getSongName() {
		return songName;
	}

	public void setSongName(String songName) {
		this.songName = songName;
	}

	public String getLove() {
		return love;
	}

	public void setLove(String love) {
		this.love = love;
	}

	public String getGuid() {
		return guid;
	}

	public void setGuid(String guid) {
		this.guid = guid;
	}

	public String getSongPath() {
		return songPath;
	}

	public void setSongPath(String songPath) {
		this.songPath = songPath;
	}

	public String getSongWord() {
		return songWord;
	}

	public void setSongWord(String songWord) {
		this.songWord = songWord;
	}

	public String getSongSize() {
		return songSize;
	}

	public void setSongSize(String songSize) {
		this.songSize = songSize;
	}

	public String getSongSinger() {
		return songSinger;
	}

	public void setSongSinger(String songSinger) {
		this.songSinger = songSinger;
	}

	public String getSongCollection() {
		return songCollection;
	}

	public void setSongCollection(String songCollection) {
		this.songCollection = songCollection;
	}

	public String getSongType() {
		return songType;
	}

	public void setSongType(String songType) {
		this.songType = songType;
	}

	public String getRemark() {
		return remark;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}

	public String getImage() {
		return image;
	}

	public void setImage(String image) {
		this.image = image;
	}

	public Date getSongTime() {
		return songTime;
	}

	public void setSongTime(Date songTime) {
		this.songTime = songTime;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((guid == null) ? 0 : guid.hashCode());
		result = prime * result + ((image == null) ? 0 : image.hashCode());
		result = prime * result + ((love == null) ? 0 : love.hashCode());
		result = prime * result + ((remark == null) ? 0 : remark.hashCode());
		result = prime * result + ((songCollection == null) ? 0 : songCollection.hashCode());
		result = prime * result + ((songName == null) ? 0 : songName.hashCode());
		result = prime * result + ((songPath == null) ? 0 : songPath.hashCode());
		result = prime * result + ((songSinger == null) ? 0 : songSinger.hashCode());
		result = prime * result + ((songSize == null) ? 0 : songSize.hashCode());
		result = prime * result + ((songTime == null) ? 0 : songTime.hashCode());
		result = prime * result + ((songType == null) ? 0 : songType.hashCode());
		result = prime * result + ((songWord == null) ? 0 : songWord.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Song other = (Song) obj;
		if (guid == null) {
			if (other.guid != null)
				return false;
		} else if (!guid.equals(other.guid))
			return false;
		if (image == null) {
			if (other.image != null)
				return false;
		} else if (!image.equals(other.image))
			return false;
		if (love == null) {
			if (other.love != null)
				return false;
		} else if (!love.equals(other.love))
			return false;
		if (remark == null) {
			if (other.remark != null)
				return false;
		} else if (!remark.equals(other.remark))
			return false;
		if (songCollection == null) {
			if (other.songCollection != null)
				return false;
		} else if (!songCollection.equals(other.songCollection))
			return false;
		if (songName == null) {
			if (other.songName != null)
				return false;
		} else if (!songName.equals(other.songName))
			return false;
		if (songPath == null) {
			if (other.songPath != null)
				return false;
		} else if (!songPath.equals(other.songPath))
			return false;
		if (songSinger == null) {
			if (other.songSinger != null)
				return false;
		} else if (!songSinger.equals(other.songSinger))
			return false;
		if (songSize == null) {
			if (other.songSize != null)
				return false;
		} else if (!songSize.equals(other.songSize))
			return false;
		if (songTime == null) {
			if (other.songTime != null)
				return false;
		} else if (!songTime.equals(other.songTime))
			return false;
		if (songType == null) {
			if (other.songType != null)
				return false;
		} else if (!songType.equals(other.songType))
			return false;
		if (songWord == null) {
			if (other.songWord != null)
				return false;
		} else if (!songWord.equals(other.songWord))
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "Song [guid=" + guid + ", songName=" + songName + ", songPath=" + songPath + ", songWord=" + songWord
				+ ", songSize=" + songSize + ", songSinger=" + songSinger + ", songCollection=" + songCollection
				+ ", songType=" + songType + ", remark=" + remark + ", image=" + image + ", love=" + love
				+ ", songTime=" + songTime + "]";
	}

}

 

package cn.ck.demoproject.service;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import cn.ck.demoproject.entity.Song;
import cn.ck.demoproject.entity.Type;
import cn.ck.demoproject.mapper.MusicListMapper;

@Service("musicListService")
public class MusicListService implements IMusicListService {
	@Resource(name = "musicListMapper")
	MusicListMapper dao;

	public List getFiveTypes() {
		// TODO Auto-generated method stub
		return dao.getFiveTypes();
	}

	public List getTenHotSongs() {
		// TODO Auto-generated method stub
		return dao.getTenHotSongs();
	}

	public String getSingerName(String guid) {
		// TODO Auto-generated method stub
		return dao.getSingerName(guid);
	}

	public List getTenNewSongs() {
		// TODO Auto-generated method stub
		return dao.getTenNewSongs();
	}

	public void addLove(String guid) {
		// TODO Auto-generated method stub
		Song song = dao.getSongById(guid);
		song.setLove(Integer.parseInt(song.getLove()) + 1 + "");
		String love = song.getLove();
		dao.addLove(guid, love);
	}

	public List getSongsBySingerName(String keyword) {
		// TODO Auto-generated method stub
		keyword = "%" + keyword + "%";
		return dao.getSongsBySingerName(keyword);
	}

	public List getSongsBySongName(String keyword) {
		// TODO Auto-generated method stub
		keyword = "%" + keyword + "%";
		System.out.println(dao.getSongsBySongName("%周%"));
		return dao.getSongsBySongName(keyword);
	}

}
package cn.ck.demoproject.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Param;

import cn.ck.demoproject.entity.Song;
import cn.ck.demoproject.entity.Type;

public interface MusicListMapper {
	List getFiveTypes();

	List getTenHotSongs();

	List getTenNewSongs();

	Song getSongById(String guid);

	void addLove(@Param("guid") String guid, @Param("love") String love);

	String getSingerName(String guid);

	List getSongsBySingerName(String keyword);

	List getSongsBySongName(String keyword);
}

这里可能大部分的处理写在controller里,这是不合理的,最好写到service里

over

 

 

 

 

 

 

你可能感兴趣的:(框架)