idea 没有java,IntelliJ IDEA没有Java 10'var'的代码完成?

Recently I installed the new version (2018.1) of IntelliJ IDEA, which added support for Java 10.

But when I trying to use the var (for local variable type inference), I found there was no var in the code completion list. (See the screenshot below)

If I continue typing, it will apply VarHandle as the first suggestion in that list. Then I have to undo that completion to restore var.

I have checked project settings, I use JDK 10 and the language level is also set to 10. Code like var number = 1; compiles well. And I couldn't find anything about this in IDE settings.

What am I doing wrong? I just want to use var declaration without interruption.

8v8yC.png

解决方案

As @Vic pointed out, this appears to be a bug. Until it gets fixed, you can create a custom live template as a workaround.

Create a live template triggered by the var abbreviation, then enter var as the template text. Make it available in the Java statement context.

a0RYH.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现学生信息和导师信息查询页面设计,需要使用 Java Web 技术,并结合数据库进行数据的存储和查询。下面是一个简单的示例: 1. 创建一个 Maven 项目,并添加以下依赖: ```xml <dependencies> <!-- Servlet API --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency> <!-- JSP API --> <dependency> <groupId>javax.servlet</groupId> <artifactId>jsp-api</artifactId> <version>2.2</version> <scope>provided</scope> </dependency> <!-- JSTL --> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <!-- MySQL JDBC --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.23</version> </dependency> </dependencies> ``` 2. 创建一个名为 `Student` 的 JavaBean 类,表示学生信息,包括学号、姓名和所属导师: ```java public class Student { private String id; private String name; private String advisor; // 省略 getter 和 setter 方法 } ``` 3. 创建一个名为 `Advisor` 的 JavaBean 类,表示导师信息,包括姓名和联系方式: ```java public class Advisor { private String name; private String contact; // 省略 getter 和 setter 方法 } ``` 4. 创建一个名为 `DatabaseUtil` 的工具类,用于连接数据库和执行 SQL 语句: ```java public class DatabaseUtil { private static final String URL = "jdbc:mysql://localhost:3306/mydb"; private static final String USER = "root"; private static final String PASSWORD = "root"; public static Connection getConnection() throws SQLException { return DriverManager.getConnection(URL, USER, PASSWORD); } public static void close(Connection conn, Statement stmt, ResultSet rs) throws SQLException { if (rs != null) { rs.close(); } if (stmt != null) { stmt.close(); } if (conn != null) { conn.close(); } } public static void close(Connection conn, Statement stmt) throws SQLException { close(conn, stmt, null); } } ``` 5. 创建一个名为 `StudentDao` 的 DAO 类,用于操作学生信息的数据库表: ```java public class StudentDao { public List<Student> getAllStudents() throws SQLException { List<Student> students = new ArrayList<>(); try (Connection conn = DatabaseUtil.getConnection(); Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("SELECT * FROM students")) { while (rs.next()) { Student student = new Student(); student.setId(rs.getString("id")); student.setName(rs.getString("name")); student.setAdvisor(rs.getString("advisor")); students.add(student); } } return students; } public List<Student> getStudentsByAdvisor(String advisor) throws SQLException { List<Student> students = new ArrayList<>(); try (Connection conn = DatabaseUtil.getConnection(); PreparedStatement stmt = conn.prepareStatement("SELECT * FROM students WHERE advisor = ?")) { stmt.setString(1, advisor); try (ResultSet rs = stmt.executeQuery()) { while (rs.next()) { Student student = new Student(); student.setId(rs.getString("id")); student.setName(rs.getString("name")); student.setAdvisor(rs.getString("advisor")); students.add(student); } } } return students; } } ``` 6. 创建一个名为 `AdvisorDao` 的 DAO 类,用于操作导师信息的数据库表: ```java public class AdvisorDao { public List<Advisor> getAllAdvisors() throws SQLException { List<Advisor> advisors = new ArrayList<>(); try (Connection conn = DatabaseUtil.getConnection(); Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("SELECT * FROM advisors")) { while (rs.next()) { Advisor advisor = new Advisor(); advisor.setName(rs.getString("name")); advisor.setContact(rs.getString("contact")); advisors.add(advisor); } } return advisors; } } ``` 7. 创建一个名为 `StudentServlet` 的 Servlet 类,用于处理学生信息查询请求: ```java @WebServlet("/students") public class StudentServlet extends HttpServlet { private static final long serialVersionUID = 1L; private StudentDao studentDao; @Override public void init() throws ServletException { studentDao = new StudentDao(); } @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String advisor = request.getParameter("advisor"); try { if (advisor == null) { List<Student> students = studentDao.getAllStudents(); request.setAttribute("students", students); request.getRequestDispatcher("/WEB-INF/views/students.jsp").forward(request, response); } else { List<Student> students = studentDao.getStudentsByAdvisor(advisor); request.setAttribute("students", students); request.setAttribute("advisor", advisor); request.getRequestDispatcher("/WEB-INF/views/students_by_advisor.jsp").forward(request, response); } } catch (SQLException e) { e.printStackTrace(); response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); } } } ``` 8. 创建一个名为 `AdvisorServlet` 的 Servlet 类,用于处理导师信息查询请求: ```java @WebServlet("/advisors") public class AdvisorServlet extends HttpServlet { private static final long serialVersionUID = 1L; private AdvisorDao advisorDao; @Override public void init() throws ServletException { advisorDao = new AdvisorDao(); } @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { List<Advisor> advisors = advisorDao.getAllAdvisors(); request.setAttribute("advisors", advisors); request.getRequestDispatcher("/WEB-INF/views/advisors.jsp").forward(request, response); } catch (SQLException e) { e.printStackTrace(); response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); } } } ``` 9. 创建两个 JSP 页面,分别为 `students.jsp` 和 `students_by_advisor.jsp`,用于渲染学生信息查询结果: `students.jsp`: ```html <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>学生信息查询</title> </head> <body> <h1>学生信息查询</h1> <table border="1"> <tr> <th>学号</th> <th>姓名</th> <th>所属导师</th> </tr> <c:forEach items="${students}" var="student"> <tr> <td>${student.id}</td> <td>${student.name}</td> <td>${student.advisor}</td> </tr> </c:forEach> </table> <br> <a href="${pageContext.request.contextPath}/advisors">返回</a> </body> </html> ``` `students_by_advisor.jsp`: ```html <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>学生信息查询</title> </head> <body> <h1>学生信息查询</h1> <h2>导师:${advisor}</h2> <table border="1"> <tr> <th>学号</th> <th>姓名</th> <th>所属导师</th> </tr> <c:forEach items="${students}" var="student"> <tr> <td>${student.id}</td> <td>${student.name}</td> <td>${student.advisor}</td> </tr> </c:forEach> </table> <br> <a href="${pageContext.request.contextPath}/students">返回</a> </body> </html> ``` 10. 部署应用程序并启动 Tomcat 服务器,访问 `http://localhost:8080/项目名/advisors`,即可查看导师信息查询页面。选择一个导师,点击链接进入学生信息查询页面,即可查看该导师所带的学生信息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值