如何将一个@controller的输出拆分为两个jsp文件

时间:2016-11-23 14:04:16

标签: java spring jsp

我正在学习Java并尝试构建一个将数据存储到mysql数据库并在表格中显示数据的Web应用程序。

首先,我已经完成了一些在线教程并编写了一个小应用程序,它允许动态地向mysql表添加数据并在同一页面上显示数据库表的所有字段。为了达到这个目的,我使用了@Controller。下面是控制器代码和jsp页面

package com.webappdemo01.controller;            

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.webappdemo01.model.Client;
import com.webappdemo01.service.ClientService;

@Controller
public class ClientController {
@Autowired
private ClientService clientService;

@RequestMapping("/index")
public String setupForm(Map<String, Object> map){
    Client client = new Client();
    map.put("client", client);
    map.put("clientList", clientService.getAllClient());
    return "client";
}
@RequestMapping(value="/client.do", method=RequestMethod.POST)
public String doActions(@ModelAttribute Client client, BindingResult result, @RequestParam String action, Map<String, Object> map){
    Client clientResult = new Client();
    switch(action.toLowerCase()){
    case "add":
        clientService.add(client);
        clientResult = client;
        break;
    case "edit":
        clientService.edit(client);
        clientResult = client;
        break;
    case "delete":
        clientService.delete(client.getClientname());
        clientResult = new Client();
        break;
    case "search":
        Client searchedClient = clientService.getClient(client.getClientname());
        clientResult = searchedClient!=null ? searchedClient : new Client();
        break;
    }
    map.put("client", clientResult);
    map.put("clientList", clientService.getAllClient());
    return "client";
  }

}

client.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ include file="/WEB-INF/jsp/includes.jsp" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Client Management</title>
</head>
<body>
<h1>Client Data</h1>
<form:form action="client.do" method="POST" commandName="client">
    <table>
        <tr>
            <td>Application Name</td>
            <td><form:input path="applicationname" /></td>
        </tr>
        <tr>
            <td>Client Type</td>
            <td><form:input path="clienttype" /></td>
        </tr>
        <tr>
            <td>Client Name</td>
            <td><form:input path="clientname" /></td>
        </tr>
        <tr>
            <td>Hostname</td>
            <td><form:input path="hostname" /></td>
        </tr>
        <tr>
            <td>Environment</td>
            <td><form:input path="envtype" /></td>
        </tr>
        <tr>
            <td colspan="2">
                <input type="submit" name="action" value="Add" />
                <input type="submit" name="action" value="Edit" />
                <input type="submit" name="action" value="Delete" />
                <input type="submit" name="action" value="Search" />
            </td>
        </tr>
    </table>
</form:form>
<br>
<table border="1">
    <th>Application Name</th>
    <th>Client Type</th>
    <th>Client Name</th>
    <th>Hostname</th>
    <th>Environment</th>
    <c:forEach items="${clientList}" var="client">
        <tr>
            <td>${client.applicationname}</td>
            <td>${client.clienttype}</td>
            <td>${client.clientname}</td>
            <td>${client.hostname}</td>
            <td>${client.envtype}</td>
        </tr>
    </c:forEach>
</table>
</body>
</html>

输出在这里

enter image description here

我想搞定输出,以便我可以在页面中添加客户端并显示不同页面中的所有客户端

2 个答案:

答案 0 :(得分:0)

您可以在控制器中添加一个单独的方法及其自己的映射。

@RequestMapping("/clientForm")
public String clientForm(Map<String, Object> map){
    //do something
    //return data
}

您可以使用/index来显示客户列表。然后从/clientFormclient.jsp发送请求。同样,将'clientForm.jsp文件放在jsp文件夹中。

我希望这是有道理的。

答案 1 :(得分:0)

您的代码几乎没有基本问题:

(1)您正尝试将所有HTTP操作映射到POST,这是不正确的,而是使用GET(查询),POST(添加),DELETE (删除),PUT(更新)单独。

(2)您正在尝试使用单一控制器方法。

而是需要拆分具有不同操作的控制器方法,如下所示:

@Controller
public class ClientController {

    @Autowired
    private ClientService clientService;

    @RequestMapping("/index", , method=RequestMethod.GET)
    public String setupForm(Map<String, Object> map){
        Client client = new Client();
        map.put("client", client);
        map.put("clientList", clientService.getAllClient());
        return "client";
    }

    @RequestMapping(value="/client.do", method=RequestMethod.POST)
    public String doActions(@ModelAttribute Client client, BindingResult result, Map<String, Object> map){
        //code for clientService.add()
        return "clientResult";
    }

     @RequestMapping(value="/client.do", method=RequestMethod.GET)
    public String doDelete(@ModelAttribute Client client, BindingResult result, Map<String, Object> map) {
        //code for clientService.delete()
        return "clientQueryResults";
    }

    @RequestMapping(value="/client.do", method=RequestMethod.PUT)
    public String doActions(@ModelAttribute Client client, BindingResult result, Map<String, Object> map){
        //code for clientService.update()
        return "clientResult";
   }


    @RequestMapping(value="/client.do", method=RequestMethod.DELETE)
    public String doDelete(@ModelAttribute Client client, BindingResult result, Map<String, Object> map) {
        //code for clientService.delete()
        return "clientResult";
    }
}

另外,请确保从JSP页面传递正确的<form method="GET" ..><form method="DELETE" ..>等。