ssh框架搭建思路: ssh和ssm整合(核心思路),所涉及到的配置文件内容和存放位置以本实例为主。
搭建用的框架:struts2 + Spring +Spring data jpa(这是Spring框架对hibernate二次包装)
配置文件:
web.xml
Struts2框架 src/struts.xml,空文件即可。Hibernate框架 置。------domain实体类注解配置映射
src/hibernate.cfg.xml 在domain有 Xxx.hbm.xml ,不需要这两个文件了。前者是配置连接数据库之类的信息,已经交给spring去管理,在applicationContext.xml中配置;后者是描述domain实体类和数据库表对应关系,这个直接在domain实体类中运用注解配置。
Spring框架 src/applicationContext.xml
关于日志 log4j.properties
关于数据库连接 db.properties
项目包结构:
用maven项目管理,导入依赖jar包
pom.xml
4.0.0
com.herbert
sshdemo
1.0-SNAPSHOT
war
sshdemo Maven Webapp
http://www.example.com
UTF-8
1.7
1.7
4.2.8.RELEASE
2.3.24
5.0.1.Final
1.6.6
1.10.4.RELEASE
2.5
4.11
1.0.9
5.1.32
1.2
2.5
2.0
2.5
org.springframework
spring-context
${spring.version}
org.springframework
spring-context-support
${spring.version}
org.springframework
spring-web
${spring.version}
org.springframework
spring-orm
${spring.version}
org.springframework
spring-test
${spring.version}
org.springframework
spring-aspects
${spring.version}
org.springframework.data
spring-data-jpa
${springdatajpa.version}
junit
junit
${junit.version}
test
org.apache.struts
struts2-core
${struts2.version}
org.apache.struts
struts2-spring-plugin
${struts2.version}
org.apache.struts
struts2-convention-plugin
${struts2.version}
org.apache.struts
struts2-json-plugin
${struts2.version}
org.hibernate
hibernate-core
${hibernate.version}
org.hibernate
hibernate-entitymanager
${hibernate.version}
com.alibaba
druid
${druid.version}
mysql
mysql-connector-java
${mysql.version}
org.slf4j
slf4j-log4j12
${slf4j.version}
jstl
jstl
${jstl.version}
javax.servlet
servlet-api
${servlet-api.version}
provided
javax.servlet
jsp-api
${jsp-api.version}
provided
sshdemo
maven-clean-plugin
3.1.0
maven-resources-plugin
3.0.2
maven-compiler-plugin
3.8.0
maven-surefire-plugin
2.22.1
maven-war-plugin
3.2.2
maven-install-plugin
2.5.2
maven-deploy-plugin
2.8.2
org.apache.tomcat.maven
tomcat7-maven-plugin
2.2
UTF-8
8080
web.xml
ssh-annotation
index.html
index.htm
index.jsp
default.html
default.htm
default.jsp
contextConfigLocation
classpath*:applicationContext.xml
org.springframework.web.context.ContextLoaderListener
CharacterEncodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
CharacterEncodingFilter
/*
struts2
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
struts2
/*
struts.xml
applicationContext.xml
db.properties
jdbc.driverClassName= com.mysql.jdbc.Driver
jdbc.url =jdbc:mysql://172.16.90.128:3306/organizationframework?characterEncoding=utf-8
jdbc.username = root
jdbc.password =123
log4j.properties
### direct log messages to stdout ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
### direct messages to file mylog.log ###
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=c:/mylog.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
### set log levels - for more verbose logging change 'info' to 'debug' ###
log4j.rootLogger=info, stdout
index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
首页
接下来展示代码部分:
Student.java
package com.herbert.domain;
import javax.persistence.*;
import java.util.Date;
/**
* @author Herbert
* @create 2019-06-09 15:12
*/
@Entity
@Table(name = "t_student", catalog = "organizationframework")
public class Student {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String username;
private String password;
private String Sno;
@Temporal(TemporalType.DATE)
private Date birthday;
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; }
public String getSno() {
return Sno;
}
public void setSno(String sno) {
Sno = sno;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
", Sno='" + Sno + '\'' +
", birthday=" + birthday +
'}';
}
}
controller层
package com.herbert.controller.action;
import com.herbert.domain.Student;
import com.herbert.service.StudentService;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
/**
* @author Herbert
* @create 2019-06-09 15:23
*/
@Namespace("/")
@ParentPackage("json-default")
@Controller
@Scope("prototype")
public class StudentAction extends ActionSupport implements ModelDriven {
private Student student = new Student();
@Override
public Student getModel() {
return student;
}
@Autowired
private StudentService studentService;
@Action(value = "addStudent", results = {@Result(name = "success", location = "./success.jsp", type = "redirect"), @Result(name = ERROR, location = "./error.jsp", type = "redirect")})
public String addStudent() {
studentService.addStudent(student);
return SUCCESS;
}
}
service层
package com.herbert.service;
import com.herbert.domain.Student;
/**
* @author Herbert
* @create 2019-06-09 15:55
*/
public interface StudentService {
void addStudent(Student student);
}
package com.herbert.service;
import com.herbert.dao.StudentRepository;
import com.herbert.domain.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
/**
* @author Herbert
* @create 2019-06-09 15:56
*/
@Service
@Transactional
public class StudentServiceImpl implements StudentService {
@Autowired
private StudentRepository studentRepository;
@Override
public void addStudent(Student student) {
studentRepository.save(student);
}
}
dao层
package com.herbert.dao;
import com.herbert.domain.Student;
import org.springframework.data.jpa.repository.JpaRepository;
/**
* @author Herbert
* @create 2019-06-09 15:58
*/
public interface StudentRepository extends JpaRepository {
}
访问路径: ip + 端口号 + 项目名 + namespace 的value值+ @action注解的value(访问路径可以不用带.action),页面里边写请求路径从项目名开始。
下图为直接访问项目静态资源路径:
注意:controller层包结构一定要有一级带action结构,貌似struts2的源码哪块有规定这类格式
不然当你提交数据的时候,会报错
最后附上实例代码:https://github.com/herbert0601/ssh ,需要的自行下载。