【发布时间】:2020-02-26 20:40:19
【问题描述】:
我目前正在开发旅行服务,我们必须使用 JAXB Marshalling 将用户信息写入 XML 文件。在 Tomcat 服务器上运行应用程序时,我在文本框中输入信息并单击“注册”,XML 格式是在控制台中写入的,但实际上并没有写入 XML 文件。截至目前,我在“src/main/resources”目录中有我的“accounts.xml”。到目前为止,这是我的控制器信息。我不确定为什么它不会写入 XML 文件:
@Controller
public class AccountController {
@RequestMapping(value = "/register", method = RequestMethod.GET)
public String registerPage(Locale locale, Model model) {
return "register";
}
@RequestMapping(value = "/home", method = RequestMethod.POST)
public String register(@RequestParam("name") String name, @Validated User user, Model model) throws JAXBException, IOException{
Account account = new Account();
account.setName(name);
account.setEmail("email");
account.setPassword("password");
try {
File file = ResourceUtils.getFile("classpath:accounts.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(Account.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
jaxbMarshaller.marshal(account, file);
jaxbMarshaller.marshal(account, System.out);
} catch (JAXBException e) {
e.printStackTrace();
}
model.addAttribute("name", user.getName());
return "user";
}
}
当我提交信息时,我的控制台输出如下所示:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<account>
<email>email</email>
<name>testName</name>
<password>password</password>
</account>
任何帮助将不胜感激!
【问题讨论】:
-
您好,如果任何答案解决了您的问题,请考虑通过单击复选标记接受它。这向更广泛的社区表明您已经找到了解决方案,并为回答者和您自己提供了一些声誉。没有义务这样做。
标签: java xml spring-mvc jaxb marshalling