gpt4 book ai didi

java - 无法弄清楚如何从 Java EE Spring Web 项目中的表单中获取数据

转载 作者:行者123 更新时间:2023-11-30 04:44:19 25 4
gpt4 key购买 nike

我感觉自己像个十足的白痴,在工作的最后 5 个小时里,我试图弄清楚 Spring 以及如何做到这一点。

我在“checklist.jsp”中有一个表单,我想获取用户放入表单中的数据。我稍后会使用该数据,但我现在只想捕获它......

我有 checklist.jsp,它有一个名为“ChecklistController.java”的 Controller 。这是我在 checklist.jsp 中的表单

<form method="POST" action="checklist.jsp">
<table>
<tr>
<td>FQDN:</td>
<td colspan="2"><input type='text' id="FQDN" /></td>
<td><input type="radio" id="rdoScript" /></td>
<td>Script 1 </td>
<td><input type="radio" id="rdoScript" /></td>
<td>Script 2</td>
</tr>
</table>
<input type="submit" value="Run" id="selectionSubmit" />
</form>

请注意:我使用了标签,但我更改了它,以便我可以实际看到该页面。我一直在反复玩弄它。

这是我的 ChecklistController.java

@Controller
// handling methods are relative to this controller
public class ChecklistController {
private ChecklistService service;
private static final Log LOG = LogFactory.getLog(ChecklistController.class);

@RequestMapping("/checklist")
public ModelAndView checklist() throws Exception{
ModelAndView mavChecklist = new ModelAndView("checklist");
mavChecklist.addObject("test",service.simpleTest());
mavChecklist.addObject("date",service.getDateTime());
return mavChecklist;
}


@Autowired
public void setService(ChecklistService service){
this.service = service;
}
}

我的想法是将数据放入一个bean中,所以我创建了这个类。 UserSel.java

public class UserSel implements java.io.Serializable {

/**
*
*/
private static final long serialVersionUID = 1L;
private String script;
private String FQDN;

public UserSel(){
}

public String getFQDN() {
return FQDN;
}

public void setFQDN(String fqdn) {
FQDN = fqdn;
}

public String getScript() {
return script;
}

public void setScript(String script) {
this.script = script;
}
}

我希望我提供了足够的信息。我想要获取 FQDN 和脚本选择,并能够使用用户的这些选择(以运行远程脚本)。

如果需要更多信息,请告诉我 -- 我现在对 Spring 感到沮丧:(

编辑:添加当前的ChecklistController.java

@Controller
// handling methods are relative to this controller
public class ChecklistController {
private ChecklistService service;
private static final Log LOG = LogFactory.getLog(ChecklistController.class);


@RequestMapping("/checklist")
public ModelAndView checklist(@ModelAttribute(value="UserSel")UserSel userSel) throws Exception{
ModelAndView mav = viewRender();
String FQDN = userSel.getFQDN();
String getScript = userSel.getScript();
mav.addObject("FQDN", FQDN);
mav.addObject("script",getScript);
return mav;
}

@RequestMapping("/newchecklist")
public ModelAndView viewRender() {
ModelAndView mav=new ModelAndView();
mav.addObject("UserSel ", new UserSel ());
mav.setViewName("checklist");
return mav;
}

@Autowired
public void setService(ChecklistService service){
this.service = service;
}
}

最佳答案

1) 添加spring的表单tld。并像这样重写你的表单

<form:form commandName="UserInfo" method="POST" action="/web/checklist">
<tr>
<td>FQDN:</td>
<td colspan="2"><input path="FQDN" /></td>
<td><form:radiobutton path="script" value="1"/></td>
<td><form:radiobutton path="script" value="2"/></td>
</tr>
<input type="submit" value="Save Changes" />
</form:form>

2.修改 Controller 以获取提交的值。

@Controller
// handling methods are relative to this controller
public class ChecklistController {
private ChecklistService service;
private static final Log LOG = LogFactory.getLog(ChecklistController.class);

//Called when you submit data and access it like this.
@RequestMapping("/checklist")
public ModelAndView checklist(@ModelAttribute(value="UserSel")UserSel userSel) throws Exception{

//userSel.get..... you will get submited values like this here. Use your service class here. And return appropriate ModelAndView

return mavChecklist;
}

//Call this method first to get blank jsp. This will bind your dataclass to jsp, in which you will get data once you submit.
@RequestMapping("/newcheklist")
public ModelAndView viewRender() {
ModelAndView mav=new ModelAndView();
mav.addObject("UserSel ", new UserSel ());
mav.setViewName("checklist");
return mav;
}
@Autowired
public void setService(ChecklistService service){
this.service = service;
}
}

由于您没有提供更多代码(例如上下文文件),我需要采取某些假设

关于java - 无法弄清楚如何从 Java EE Spring Web 项目中的表单中获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11441252/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com