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

时间:2012-07-11 20:49:37

标签: java spring web-applications

我觉得自己是一个完全白痴,在工作的最后5个小时里,我正在试图弄清楚春天以及如何做到这一点。

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

我有checklist.jsp,它有一个名为“ChecklistController.java”的控制器。 这是我在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;
}
}

2 个答案:

答案 0 :(得分:1)

这样的内容将允许您将已发布的表单输入作为参数接收到请求处理程序:

@RequestMapping("/checklist")
public ModelAndView checklist(
    @RequestParam("FQDN") String FQDN,
    @RequestParam("rdoScript") int rdoScript) {
    // ...
}

有关详细信息,请参阅Spring Framework Reference Documentation

答案 1 :(得分:1)

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
// 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;
    }
}

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