我正在学习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>
输出这里
我想slipt输出,这样我可以添加客户端在页面中显示所有客户端的不同页面
非常感谢你的回复。很抱歉,迟到的回复需要一段时间才能理解。我完全陌生的java编码每一个我在java中遇到的词对我来说都是新的 –
@GopiD不用担心!我很高兴,我可以帮忙。只要坚持下去,你就会很棒! –