2016-11-23 92 views
0

我正在学习Java,并尝试构建一个将数据存储到mysql数据库并在表中显示数据的web应用程序。如何将一个@controller的输出分成两个jsp文件

对于开始,我已经通过一些在线教程并编写了一个小应用程序,它允许动态地向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

我想slipt输出,这样我可以添加客户端在页面中显示所有客户端的不同页面

回答

0

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

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

您可以使用您的/index来显示客户端列表。然后从client.jsp发送请求到/clientForm。同样,在您的jsp文件夹中放置一个'clientForm.jsp文件。

我希望这是有道理的。

+0

非常感谢你的回复。很抱歉,迟到的回复需要一段时间才能理解。我完全陌生的java编码每一个我在java中遇到的词对我来说都是新的 –

+0

@GopiD不用担心!我很高兴,我可以帮忙。只要坚持下去,你就会很棒! –

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"; 
    } 
} 

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

相关问题