【发布时间】:2018-03-05 22:44:42
【问题描述】:
这种方式不是最好的。我正在寻找更好的方法。没有Lists可以吗?
我的页面控制器:
public class CreateUserPageController {
@Autowired
private UserServiceImpl userServiceImpl;
@RequestMapping(value = "/create-user", method = RequestMethod.GET)
public ModelAndView showForm() {
ModelAndView model = new ModelAndView("admin/create-user");
model.addObject("user", new User());
UserRoleDTO[] roles = UserRoleDTO.values();
List<String> roleNames = new ArrayList<>();
for (UserRoleDTO role : roles) {
roleNames.add(role.getName());
}
model.addObject("roleNames", roleNames);
return model;
}
@RequestMapping(value = "/create-user", method = RequestMethod.POST)
public ModelAndView submitForm(@ModelAttribute("user") User user) {
userServiceImpl.create(user);
return showForm();
}
}
我的 JSP:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<html>
<head>
<title>Create test</title>
</head>
<body>
<h1>Create Test</h1>
<form:form method="POST" action="/create-test" modelAttribute="topicTestDTO">
<form:input path="topic.name" type="text" list="topic_list" placeholder="choose or create topic"/>
<datalist id="topic_list">
<c:forEach items="${topicList}" var="topic">
<option>${topic}</option>
</c:forEach>
</datalist>
<br>
<form:input path="test.name" type="text" placeholder="create test name"/>
<input type="submit">
</form:form>
</body>
</html>
感谢您的帮助。 如果您需要更多信息,请告诉我哪一个,我将完成代码。
【问题讨论】:
-
请问您想要什么样的改进,您这样做的目的是什么?