Witam mam oto taki kod:
@Controller
@RequestMapping(value = "/BlogCreatorSetup")
public class SetupController {
@RequestMapping(value = "/Setup", method = RequestMethod.GET)
public ModelAndView setupForm()
{
return new ModelAndView("setup/setup","command", new SetupForm());
}
@RequestMapping(value = "/RedirectSetup", method = RequestMethod.POST)
public String setupFinish(@ModelAttribute("SpringWeb")SetupForm setupForm,ModelMap model)
{
ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
SetupFile file = (SetupFile) context.getBean("setupFile");
file.saveToFile(setupForm);
((ConfigurableApplicationContext)context).close();
return "redirect:../AdminPanel";
}
}
@Controller
@RequestMapping(value = "/AdminPanel")
public class HomeController {
@RequestMapping(method = RequestMethod.GET)
public String getInformation(ModelMap model)
{
ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
SetupFile file = (SetupFile) context.getBean("setupFile");
SetupForm form = file.getForm();
model.addAttribute("admin",form.getAdmin());
model.addAttribute("password", form.getPassword());
model.addAttribute("dataBaseName", form.getDataBaseName());
model.addAttribute("dataBaseLogin", form.getDataBaseLogin());
model.addAttribute("dataBasePassword", form.getDataBasePassword());
((ConfigurableApplicationContext)context).close();
return "admin/adminPanel";
}
}
public class SetupFile {
private SetupForm form = null;
Resource resource = new ClassPathResource("configuration.bc");
public SetupForm getForm()
{
form = new SetupForm();
try {
Scanner sc = new Scanner(resource.getFile());
form.setAdmin(sc.nextLine().toString());
form.setPassword(sc.nextLine().toString());
form.setDataBaseLogin(sc.nextLine().toString());
form.setDataBasePassword(sc.nextLine().toString());
form.setDataBaseName(sc.nextLine().toString());
sc.close();
return form;
} catch (FileNotFoundException e) {
System.out.println("ERROR ! code 0x00000003");
} catch (NoSuchElementException e){
System.out.println("ERROR ! code 0x00000004");
} catch (IOException e) {
System.out.println("ERROR ! code 0x00000005");
}
return null;
}
public void saveToFile(SetupForm form)
{
this.form = form;
PrintWriter writer;
try {
System.out.println(resource.getFile().getAbsolutePath());
writer = new PrintWriter(resource.getFile());
writer.println(form.getAdmin());
writer.println(form.getPassword());
writer.println(form.getDataBaseLogin());
writer.println(form.getDataBasePassword());
writer.println(form.getDataBaseName());
writer.close();
} catch (FileNotFoundException e) {
System.out.println("ERROR ! code 0x00000001");
} catch (IOException e) {
System.out.println("ERROR ! code 0x00000002");
}
}
}
Ja mam w zasadzie tylko małe pytanie, bo męczyłem się nad tym 2h i dziwiłem się czemu mi program nie działa. Otóż wcześniej w klasie SetupFile, nie miałem wcześniej linijki
form = new SetupForm()
. Jeżeli moja aplikacja w saveToFile zapisuje sobie do this.form = form; to mam jakąś instancje w formi'e klasy SetupForm więc dlaczego musze później tworzyć form = new SetupForm()
Czyżby, gdy odwołuje się do
```java
SetupFile file = (SetupFile) context.getBean("setupFile");
Dostaję inną instancję tej klasy ? W beans.xml jest domyślnie singleton... Mam nadzieję, że ktoś zrozumie to pytanie :P
Shalomniezdecydowany