将前端与后端连接

时间:2017-12-18 03:24:07

标签: java spring jsp

我的控制器是

@RestController公共类MainRestController {

    return employeeDAO.getAllEmployees();
}

@RequestMapping(value = "/employee/{empName}", method = RequestMethod.GET, produces = { MediaType.APPLICATION_JSON_VALUE })

public JSONObject getEmployee(@PathVariable("empName") String empName) {
    return employeeDAO.getEmployee(empName);
}

@RequestMapping(value = "/employee", method = RequestMethod.POST, produces = { MediaType.APPLICATION_JSON_VALUE})

public Employee addEmployee(@RequestBody Employee emp) {

    return employeeDAO.addEmployee(emp);

}

@RequestMapping(value = "/employee", method = RequestMethod.PUT, produces = { MediaType.APPLICATION_JSON_VALUE})
public Employee updateEmployee(@RequestBody Employee emp) {

    return employeeDAO.updateEmployee(emp);
}

@RequestMapping(value = "/employees/{empName}", method = RequestMethod.DELETE, produces = { MediaType.APPLICATION_JSON_VALUE })
public void deleteEmployee(@PathVariable("empName") String empName) {
    employeeDAO.deleteEmployee(empName);
}

@RequestMapping(value = "/newHome", method = RequestMethod.GET)
public Employee newEmployee(){
    Employee employee = new Employee();
    employee.setEmpName("newHome");
    return employee;
} }

* /     @RequestMapping(value =" / employee",method = RequestMethod.GET,produce = {MediaType.APPLICATION_JSON_VALUE})     public JSONObject getEmployees(){

public List getTheFileAsObject(String filePath){

    List <Employee> employeeList = new ArrayList<>();

    try {

        FileInputStream file = new FileInputStream(new File(filePath));

        // Get the workbook instance for XLS file
        HSSFWorkbook workbook = new HSSFWorkbook(file);

        int numberOfSheets = workbook.getNumberOfSheets();
        //loop through each of the sheets
        for (int i = 0; i < numberOfSheets; i++) {

            // Get first sheet from the workbook
            HSSFSheet sheet = workbook.getSheetAt(i);

            // Iterate through each rows from first sheet
            Iterator<Row> rowIterator = sheet.iterator();
            while (rowIterator.hasNext()) {
                // Get Each Row
                Row row = rowIterator.next();

                //Leaving the first row alone as it is header

                if (row.getRowNum() == 0)
                    continue;

                // For each row, iterate through each columns
                Iterator<Cell> cellIterator = row.cellIterator();

                Employee employee = new Employee();

                while (cellIterator.hasNext()) {

                    Cell cell = cellIterator.next();

                    int columnIndex = cell.getColumnIndex();

                    switch (columnIndex + 1) {

                        case 1:
                            employee.setEmpName(cell.getStringCellValue());
                            break;
                        case 2:
                            employee.setExtCode((int) cell.getNumericCellValue());
                            break;
                    }
                }
                employeeList.add(employee);
            }
        }
        file.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return employeeList;
} }

我的java文件是:

公共类ReadExcelFileAndStore {

kableExtra

我跟踪了一些关于在控制器中提供静态内容的Web,并添加了JSP文件中包含的JSP和标记所需的必需依赖项。但即使我有链接的JSP文件,我仍然不会将我的前端连接到我的后端。我需要添加更多内容以便将我的前端与后端连接起来。请有人知道分享你的答案。谢谢

2 个答案:

答案 0 :(得分:1)

首先你的问题不明确。你还没有说明到底出了什么问题。 其次,请提供一个完全可运行的代码,以便我们可以从我们这边运行它。

最后,我猜你所面临的问题是你的JSP没有链接到你的java类。

为此,您需要使用@Controller将您的类注释为控制器,以便spring知道谁在处理请求。 此外,我们使用@ModelAttribute将前端链接到底层的Java代码。

下面给出了一个非常详细的教程示例 http://www.mkyong.com/spring-mvc/spring-mvc-form-handling-example/

答案 1 :(得分:0)

您的src / main文件夹中可能有一个名为resources的文件夹。创建一个名为static或public的新文件夹,并将html页面放在static或public文件夹中。如果从索引页开始,则无需创建单独的映射。希望这是您正在寻找的答案。有关更多信息,请参阅他的链接 https://spring.io/blog/2013/12/19/serving-static-web-content-with-spring-boot