配置为侦听端口8080的Tomcat连接器无法启动。该端口可能已在使用中,或者连接器配置错误

时间:2019-03-14 13:55:10

标签: java spring-boot controller console

  

DAO类

package com.dao;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bin.Student;
import com.repository.StudentRepository;

@Service
public class StudentDAO {
    @Autowired
    StudentRepository studentRepository;

    /*to save an employee*/

    public Student save(Student std) {
        return studentRepository.save(std);
    }


    /* search all employees*/

    public List<Student> findAll(){
        return studentRepository.findAll();
    }


    /*get an employee by id*/
    public Student findOne(Integer id){
        return studentRepository.getOne(id);

    }


    /*delete an employee*/

    public void delete(Student std) {
        studentRepository.delete(std);
    }


}
  

控制器

package com.controller;

import java.util.ArrayList;
import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.bin.Student;
import com.dao.StudentDAO;

@Controller

public class StudentController {

    @Autowired
    private StudentDAO studentDao;

    @RequestMapping(value="/enroll",method=RequestMethod.GET)
    public String newRegistration(ModelMap model) {
        Student student = new Student();
        model.addAttribute("student",student);
        return "enroll";
    }

    @RequestMapping(value="/save",method=RequestMethod.POST)
    public String saveRegistration(@Valid Student student,BindingResult result,ModelMap model,RedirectAttributes redirectAttributes) {

        if(result.hasErrors()) {
            return "enroll";
        }

        studentDao.save(student);

        return "redirect:/viewstudents";
    }


    @RequestMapping(value="/viewstudents")
    public ModelAndView getAll() {

        List<Student> list=studentDao.findAll();
        return new ModelAndView("viewstudents","list",list);
    }


    @RequestMapping(value="/editstudent/{id}")
    public String edit (@PathVariable int id,ModelMap model) {

        Student student=studentDao.findOne(id);
        model.addAttribute("student",student);
        return "editstudent";
    }

    @RequestMapping(value="/editsave",method=RequestMethod.POST)
    public ModelAndView editsave(@ModelAttribute("student") Student p) {

        Student student=studentDao.findOne(p.getId());

        student.setFirstName(p.getFirstName());
        student.setLastName(p.getLastName());
        student.setCountry(p.getCountry());
        student.setEmail(p.getEmail());
        student.setSection(p.getSection());
        student.setSex(p.getSex());

        studentDao.save(student);
        return new ModelAndView("redirect:/viewstudents");
    }

    @RequestMapping(value="/deletestudent/{id}",method=RequestMethod.GET)
    public ModelAndView delete(@PathVariable int id) {
        Student student=studentDao.findOne(id);
        studentDao.delete(student);
        return new ModelAndView("redirect:/viewstudents");
    }



    @ModelAttribute("sections")
    public List<String> intializeSections(){
        List<String> sections = new ArrayList<String>();
        sections.add("Graduate");
        sections.add("Post Graduate");
        sections.add("Reasearch");
        return sections;
    }


    /*
     * Method used to populate the country list in view. Note that here you can
     * call external systems to provide real data.
     */
    @ModelAttribute("countries")
    public List<String> initializeCountries() {

        List<String> countries = new ArrayList<String>();
        countries.add("INDIA");
        countries.add("USA");
        countries.add("CANADA");
        countries.add("FRANCE");
        countries.add("GERMANY");
        countries.add("ITALY");
        countries.add("OTHER");
        return countries;
    }

}
  

应用程序类

package com.application;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;


@SpringBootApplication
@EnableJpaAuditing
public class StudentApplication {

    public static void main(String[] args) {
        SpringApplication.run(StudentApplication.class, args);

    }

}
  

application.prprties

spring.mvc.view.prefix : /WEB-INF/views/
spring.mvc.view.suffix: .jsp

## Spring DATASOURCE (DataSourceAutoConfiguration & DataSourceProperties)
spring.datasource.url = jdbc:mysql://localhost:3306/studentdb?allowPublicKeyRetrieval=true&useSSL=false
spring.datasource.username = root
spring.datasource.password = root


## Hibernate Properties
# The SQL dialect makes Hibernate generate better SQL for the chosen database
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect

# Hibernate ddl auto (create, create-drop, validate, update)
spring.jpa.hibernate.ddl-auto = update
  

存储库

package com.repository;

import org.springframework.data.jpa.repository.JpaRepository;

import com.bin.Student;

public interface StudentRepository extends JpaRepository<Student, Integer> {

}
  

这是我的应用程序后端。我已经做了很多尝试来解决这个问题。请尝试帮助。我需要解决此错误才能部署项目。这将是真正的帮助

body {
    background-color: lightblue;
}

h1 {
    color: navy;
    margin-left: 20px;
}

table {
    width:100%;
   
}
table, th, td {
    
    border-collapse: collapse;
    
}
th, td {
    padding: 5px;
    text-align: left;
}
table#t01 tr:nth-child(even) {
    background-color: #eee;
   
}
table#t01 tr:nth-child(odd) {
   background-color:#fff;
   
}
table#t01 th {
    background-color: blue;
    color: white;
    text-align: center;
}


table#t02 td {
    
    vertical-align: inherit;
    text-align: center;
}
table#t02 tr:nth-child(even) {
    background-color: #eee;
    
   
}
table#t02 tr:nth-child(odd) {
   background-color:#fff;
   
}
table#t02 th {
    
    background-color: lightblue;
    text-align: center;
}


h1 {
    text-align: center;
    display: block;
    font-size: 2em;
    margin-top: 0.67em;
    margin-bottom: 0.67em;
    margin-left: 0;
    margin-right: 0;
    font-weight: bold;
    color:blue;
}
h2 {
    text-align: left;
    display: block;
    font-size: 1em;
    margin-top: 0.67em;
    margin-bottom: 0.67em;
    margin-left: 0;
    margin-right: 0;
    font-weight: bold;
    color:#008040;
}
a {
   
    font-weight: bold;
    color:blue;
}

body {
    background-color: lightblue;
}

h1 {
    color: navy;
    margin-left: 20px;
}

table {
    width:100%;
   
}
table, th, td {
    
    border-collapse: collapse;
    
}
th, td {
    padding: 5px;
    text-align: left;
}
table#t01 tr:nth-child(even) {
    background-color: #eee;
   
}
table#t01 tr:nth-child(odd) {
   background-color:#fff;
   
}
table#t01 th {
    background-color: blue;
    color: white;
    text-align: center;
}


table#t02 td {
    
    vertical-align: inherit;
    text-align: center;
}
table#t02 tr:nth-child(even) {
    background-color: #eee;
    
   
}
table#t02 tr:nth-child(odd) {
   background-color:#fff;
   
}
table#t02 th {
    
    background-color: lightblue;
    text-align: center;
}


h1 {
    text-align: center;
    display: block;
    font-size: 2em;
    margin-top: 0.67em;
    margin-bottom: 0.67em;
    margin-left: 0;
    margin-right: 0;
    font-weight: bold;
    color:blue;
}
h2 {
    text-align: left;
    display: block;
    font-size: 1em;
    margin-top: 0.67em;
    margin-bottom: 0.67em;
    margin-left: 0;
    margin-right: 0;
    font-weight: bold;
    color:#008040;
}
a {
   
    font-weight: bold;
    color:blue;
}
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page isELIgnored="false" %>

<html>

<head>
	<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
	<title>Student Enrollment Form</title>
	<link href="css/bootstrap.css"      rel="stylesheet">
	<link href="css/custom.css"      rel="stylesheet">
	<link href="css/main.css"      rel="stylesheet">
</head>

<body>

 	<div class="form-container">
 	
 	<h1>Student Enrollment Form</h1>
 	
	<form:form method="POST" modelAttribute="student" commandName="student" class="form-horizontal" action="save">
	
<div class="row">
			<div class="form-group col-md-12">
				<label class="col-md-3 control-lable" for="firstName">First Name</label>
				<div class="col-md-7">
					<form:input type="text" path="firstName" id="firstName" class="form-control input-sm"/>
					<div class="has-error">
						<form:errors path="firstName" class="help-inline"/>
					</div>
				</div>
			</div>
		</div>

		<div class="row">
			<div class="form-group col-md-12">
				<label class="col-md-3 control-lable" for="lastName">Last Name</label>
				<div class="col-md-7">
					<form:input type="text" path="lastName" id="lastName" class="form-control input-sm"/>
					<div class="has-error">
						<form:errors path="lastName" class="help-inline"/>
					</div>
				</div>
			</div>
		</div>
		

		

		<div class="row">
			<div class="form-group col-md-12">
				<label class="col-md-3 control-lable" for="email">Email</label>
				<div class="col-md-7">
					<form:input type="text" path="email" id="email" class="form-control input-sm"/>
					<div class="has-error">
						<form:errors path="email" class="help-inline"/>
					</div>
				</div>
			</div>
		</div>


		

		<div class="row">
			<div class="form-group col-md-12">
				<label class="col-md-3 control-lable" for="country">Country</label>
				<div class="col-md-7">
					<form:select path="country" id="country" class="form-control input-sm">
				        <form:option value="">Select Country</form:option>
			    	    <form:options items="${countries}" />
				    </form:select>
					<div class="has-error">
						<form:errors path="country" class="help-inline"/>
					</div>
				</div>
			</div>
		</div>
		
<div class="row">
			<div class="form-group col-md-12">
				<label class="col-md-3 control-lable" for="sex">Sex</label>
				<div class="col-md-7" class="form-control input-sm">
					<form:radiobutton path="sex" value="M" />Male 
	    			<form:radiobutton path="sex" value="F" />Female
					<div class="has-error">
						<form:errors path="sex" class="help-inline"/>
					</div>
				</div>
			</div>
		</div>
		
		<div class="row">
			<div class="form-group col-md-12">
				<label class="col-md-3 control-lable" for="section">Section</label>
				<div class="col-md-7" class="form-control input-sm">
					<form:radiobuttons path="section" items="${sections}" />
					<div class="has-error">
						<form:errors path="section" class="help-inline"/>
					</div>
				</div>
			</div>
		</div>
		
		

		<div class="row">
			<div class="form-actions floatRight">
				<input type="submit" value="Register" class="btn btn-primary btn-sm">
			</div>
		</div>
	</form:form>
	</div>
</body>
</html>

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page isELIgnored="false" %>

<html>

<head>
	<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
	<link href="/css/bootstrap.css"      rel="stylesheet">
	<link href="/css/custom.css"      rel="stylesheet">
	<link href="/css/main.css"      rel="stylesheet">
</head>

<body>

 	<div class="form-container">
 	
 	<h1>Edit Student Details</h1>
 	
	<form:form method="POST" modelAttribute="student" commandName="student" class="form-horizontal" action="/editsave">

<div class="row">
			<div class="form-group col-md-12">
				<label class="col-md-3 control-lable" for="id"></label>
				<div class="col-md-7">
					<form:hidden path="id"  class="form-control input-sm"/>
					<div class="has-error">
						
					</div>
				</div>
			</div>
		</div>
		<div class="row">
			<div class="form-group col-md-12">
				<label class="col-md-3 control-lable" for="firstName">First Name</label>
				<div class="col-md-7">
					<form:input type="text" path="firstName" id="firstName" class="form-control input-sm"/>
					<div class="has-error">
						<form:errors path="firstName" class="help-inline"/>
					</div>
				</div>
			</div>
		</div>

		<div class="row">
			<div class="form-group col-md-12">
				<label class="col-md-3 control-lable" for="lastName">Last Name</label>
				<div class="col-md-7">
					<form:input type="text" path="lastName" id="lastName" class="form-control input-sm"/>
					<div class="has-error">
						<form:errors path="lastName" class="help-inline"/>
					</div>
				</div>
			</div>
		</div>

		<div class="row">
			<div class="form-group col-md-12">
				<label class="col-md-3 control-lable" for="sex">Sex</label>
				<div class="col-md-7" class="form-control input-sm">
					<form:radiobutton path="sex" value="M" />Male 
	    			<form:radiobutton path="sex" value="F" />Female
					<div class="has-error">
						<form:errors path="sex" class="help-inline"/>
					</div>
				</div>
			</div>
		</div>

		

		<div class="row">
			<div class="form-group col-md-12">
				<label class="col-md-3 control-lable" for="email">Email</label>
				<div class="col-md-7">
					<form:input type="text" path="email" id="email" class="form-control input-sm"/>
					<div class="has-error">
						<form:errors path="email" class="help-inline"/>
					</div>
				</div>
			</div>
		</div>


		<div class="row">
			<div class="form-group col-md-12">
				<label class="col-md-3 control-lable" for="section">Section</label>
				<div class="col-md-7" class="form-control input-sm">
					<form:radiobuttons path="section" items="${sections}" />
					<div class="has-error">
						<form:errors path="section" class="help-inline"/>
					</div>
				</div>
			</div>
		</div>


		<div class="row">
			<div class="form-group col-md-12">
				<label class="col-md-3 control-lable" for="country">Country</label>
				<div class="col-md-7">
					<form:select path="country" id="country" class="form-control input-sm">
				        <form:option value="">Select Country</form:option>
			    	    <form:options items="${countries}" />
				    </form:select>
					<div class="has-error">
						<form:errors path="country" class="help-inline"/>
					</div>
				</div>
			</div>
		</div>

	
		<div class="row">
			<div class="form-actions floatRight">
				<input type="submit" value="Edit" class="btn btn-primary btn-sm">
			</div>
		</div>
	</form:form>
	</div>
</body>
</html>

<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>    
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>  
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
	<link href="/css/bootstrap.css"      rel="stylesheet">
	<link href="/css/custom.css"      rel="stylesheet">
	<link href="/css/main.css"      rel="stylesheet">
</head>
<body>
<h1 align="center">Students List</h1>
<table id="t02"  cellpadding="2">
<tr>
<th>
<a  href="/enroll"><h2>Home Page:Enroll New Student</h2></a>

</th>

<th>

<a  align ="right" href="/delete"><h2>Delete All Students</h2></a>
</th>
</tr>
</table>
<table >


</table>
   
<form:form  class="form-horizontal" >
<table id="t01" border="2" width="70%" cellpadding="2">
<tr><th>Id</th><th>First Name</th><th>Last Name</th>
<th>Sex</th><th>Date Modified</th>
<th>Email</th><th>Section</th><th>Country</th>
<th>Edit</th><th>Delete</th></tr>  

   <c:forEach var="student" items="${list}"> 
   <tr>  
   <td>${student.id}</td>  
   <td>${student.firstName}</td> 
   <td>${student.lastName}</td>  
   <td>${student.sex}</td> 
   <td>${student.createdAt}</td>  
   <td>${student.email}</td> 
   <td>${student.section}</td> 
   <td>${student.country}</td>  
   
   
   <td><a href="/editstudent/${student.id}">Edit</a></td>  
   <td><a href="/deletestudent/${student.id}">Delete</a></td>  
   </tr>  
   </c:forEach> 
   
   
   </table>  
   <br/>
   
  
   </form:form>
</body>
</html>

  

这是我的整个项目。当我尝试在Web上部署它时,它显示404找不到,在控制台中它显示

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2019-03-14 19:16:13.739 ERROR 4368 --- [  restartedMain] o.s.boot.SpringApplication               : Application run failed

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'studentController': Unsatisfied dependency expressed through field 'studentDao'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.dao.StudentDAO' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

2 个答案:

答案 0 :(得分:1)

尝试在@Repository接口上添加StudentRepository注释,在@EnableJpaRepositories("com.repository")类上添加StudentApplication,在@ComponentScan("com")类上也添加StudentApplication

由于StudentRepository类和其他类位于不同的程序包中,因此Spring无法自动连接StudentDAOStudentApplication

答案 1 :(得分:0)

SpringBoot将自动扫描软件包中的存储库,这些软件包是@SpringBootApplication带注释的类所在的软件包的子软件包。

因此,在您的情况下,因为它位于com.application中,所以应该将存储库放在该位置之下。也许com.application.repository。否则,您需要弄乱其他注释,以按照其他人的建议在其他软件包中启用组件扫描。