当先锋百科网

首页 1 2 3 4 5 6 7

springboot

异常:No Java compiler available for configuration options compilerClassName: [null] and compiler: [null]

缺乏一个eclipse依赖

org.eclipse.jdt.core.compiler ecj 4.6.1 provided

创建springboot项目:

向pom.xml中添加jar包

  	<!-- spring-boot web启动器 -->
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-web</artifactId>
		<version>2.2.2.RELEASE</version>
	</dependency>
	
	<!-- 配置springboot内置tomcat环境 -->
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-tomcat</artifactId>
		<version>2.2.2.RELEASE</version>
		<scope>provided</scope>
	</dependency>
	
	<!-- 热部署 -->
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-devtools</artifactId>
		<version>2.2.2.RELEASE</version>
		<scope>provided</scope>
	</dependency>
	
	<!-- 配置spring视图解析器 -->
	<dependency>
		<groupId>org.apache.tomcat.embed</groupId>
		<artifactId>tomcat-embed-jasper</artifactId>
		<version>9.0.22</version>
	</dependency>	
	
	<!-- eclipse依赖 -->
	<dependency>
		<groupId>org.eclipse.jdt.core.compiler</groupId>
		<artifactId>ecj</artifactId>
		<version>4.6.1</version>
		<scope>provided</scope>
	</dependency>
	
	<!-- jdbc -->
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-jdbc</artifactId>
		<version>2.2.2.RELEASE</version>	
	</dependency>

	<!-- 连接数据库 -->
	<dependency>
	    <groupId>mysql</groupId>
	    <artifactId>mysql-connector-java</artifactId>
	    <version>5.1.38</version>
	</dependency>

	<!-- 阿里数据源 -->
	<dependency>
	  <groupId>com.alibaba</groupId>
	  <artifactId>druid</artifactId>
	  <version>1.1.10</version>
	</dependency>

	<!-- 配置springboot集成mybatis环境 -->
 	<dependency>
		<groupId>org.mybatis.spring.boot</groupId>
		<artifactId>mybatis-spring-boot-starter</artifactId>
	    <version>2.1.1</version>
	</dependency>

	<!-- 文件下载 -->
    <dependency>
        <groupId>commons-fileupload</groupId>
        <artifactId>commons-fileupload</artifactId>
        <version>1.3.3</version>
    </dependency>

创建Controller

package com.wenhua.spring.project.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.wenhua.spring.project.bean.User;
import com.wenhua.spring.project.service.UserService;

@Controller
@RequestMapping(path="/login")
public class LoginController {

	@Autowired
	UserService userService;
	
	@ResponseBody
	@RequestMapping(path="/tohello")
	public String toHello(){
		System.out.println("tohello");
		return "hello world";
	}
	
	@RequestMapping(path="/tosuccess")
	public String toSuccess(HttpServletRequest request,HttpSession session){
		try {
			String account =  request.getParameter("account");
			String password = request.getParameter("password");
			User user = userService.findUserByAcPs(account,password);
			if (user != null) {
				session.setAttribute("user", user);
				return "success";
			}else{
				return "redirect:/index.jsp";
			}
		} catch (Exception e) {
			// 日志输出
			e.printStackTrace();
			return "redirect:/index.jsp";
		}
		
	}
}

dao层

package com.wenhua.spring.project.dao;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.wenhua.spring.project.bean.User;

@Repository
public class UserDao {

	@Autowired
	JdbcTemplate jdbcTemplate;

	public User checkLogin(String account, String password) {
		String sql = "select id,account,password,sex "
				+ " from t_user where account = ? and password = ?";
		User users = jdbcTemplate.queryForObject(sql,new RowMapper<User>() {
            // 注意如果查询结果为空,封装映射会弹出异常
			@Override
			public User mapRow(ResultSet resultSet, int rowNum) throws SQLException {
					// 注意此处应该用原型模式,也就是多例模式
					User user = new User();
					user.setId(resultSet.getInt("id"));
					user.setAccount(resultSet.getString("account"));
					user.setPassword(resultSet.getString("password"));
					user.setSex(resultSet.getString("sex"));
					return user;
				}
			},account,password);
		return users;
	}	
}

service层

package com.wenhua.spring.project.service;

import java.util.HashMap;
import java.util.Map;

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

import com.wenhua.spring.project.bean.User;
import com.wenhua.spring.project.dao.LoginDao;
import com.wenhua.spring.project.dao.UserDao;

@Service
public class UserService {

	/*集成jdbc
	 @Autowired
	UserDao userDao;
	
	**
	 * 登录验证账号
	 * @param account
	 * @param password
	 * @return
	 *
	public User findUserByAcPs(String account, String password) {
		User user = userDao.checkLogin(account,password);
		return user;
	}*/
	
	/*集成mybatis*/
	@Autowired
	LoginDao loginDao;
	
	public User findUserByAcPs(String account, String password) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("account", account);
		map.put("password", password);
		User user = loginDao.checkLogin(map);
		return user;
	}
}

启动类

package com.wenhua.spring.project;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * @SpringBootApplication标注是一个主程序类,说明这是一个spring Boot应用
 * @author ASUS
 *
 */
@SpringBootApplication
public class SpringApp {

	public static void main(String[] args) {
		
		// springboot启动起来
		SpringApplication.run(SpringApp.class, args);
		
	}
}

springboot项目打包

springboot项目打包
   1.创建一个类,使用@SpringBootApplication注解
   2.继承SpringBootServletInitializer类
   3.重写configure方法,并加载当前类的class
   4.将配置文件.yml中的服务器端口号进行注释,或删除 # server.port: 1909
   5.在pom.xml配置文件中将热部署和内部的tomcat不进行打包设置<scope>provided</scope>
   6.eclipse:右键项目名-->Run As-->Maven Install  BUILD SUCCESS 
   6.idea:①Build-->Build Artifacts-->All Artifacts-->Clean ②Build--> ReBuild Porject ③Build-->Build Artifacts-->All Artifacts-->ReBuild
   7.部署成功之后,在target目录下找到项目对应对的.war包
   8.将war放置在服务器(tomcat)的webapps文件夹下即可
   9.启动服务器(tomcat),记得查看服务器端口号
   10.通过http://127.0.0.1:端口号/项目名(建议去复制war包的名称或复制work目录下的文件名)/访问路径
  • 注解@SpringBootApplication

  • 启动类继承extends SpringBootServletInitializer

重写configure方法

package com.wenhua.spring.project;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

/**
 * 打包测试启动类
 */
@SpringBootApplication
public class SpringBootServletApp extends SpringBootServletInitializer {

	@Override
	protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
		
		return builder.sources(SpringBootServletApp.class);
		
	}	
}

去掉配置文件中内部服务器端口server.port: 9999

去掉pom.xml文件中的热部署和内置tomcat

打包Run as —> Maven Install如果打包不成功,进入该文件的目录,选中目录在目录中输入cmd直接进入该目录的命令窗口,输入

mvn clean(清理)

mvn install(打包)

过滤器

  • 注解@Configuration或@WebListener
  • 实现接口ServletContextListener
package com.wenhua.spring.project.util;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.springframework.context.annotation.Configuration;

/**
 * 监听Context上下文创建时,添加path属性
 * @author ASUS
 *
 */
@Configuration
public class ContextPathListener implements ServletContextListener {

	@Override
	public void contextInitialized(ServletContextEvent sce) {
		
		ServletContext context = sce.getServletContext();
		context.setAttribute("path", context.getContextPath());
	}

	@Override
	public void contextDestroyed(ServletContextEvent sce) {

	}
}

拦截器

  • 放行登录页面以及session失效跳转对其他的跳转添加的拦截器
  • 实现 HandlerInterceptor
package com.wenhua.spring.project.util;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.wenhua.spring.project.bean.User;


/**
 * 请求拦截器
 * @author ASUS
 *
 */
public class isLoginInterceptor implements HandlerInterceptor {

	/**
	 * 预处理
	 * 在Controller未处理之前拦截请求
	 */
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		// 获得session并取得user对象
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("user");
		if(user == null){
			// 注意这里使用的是请求转发,可以直接访问到非WEB-INF文件下的文件
			response.sendRedirect("index.jsp");
			return false;
		}else{
			/*当请求到达控制器之前被执行
			   true--继续向下执行,到达下一个拦截器,或控制器
			   false--不会继续向下执行*/
			return true;
		}
	}

	/**
	 * 
	 * 在Controller处理之后返回时拦截
	 */
	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
	}

	/**
	 * 在渲染View之后向用户发送View时拦截
	 */
	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
	}
}