HTML单选按钮事件调用Spring MVC控制器

时间:2015-11-10 04:40:11

标签: html spring spring-mvc

我想使用单选按钮事件进行过滤。我已经搜索过将java方法调用到html中,并找到了这个link。并尝试实施它。

HTML文件

<form action="${pageContext.request.contextPath}/servlet" method="post">
      <input type="radio" value="1" name="radioFilter1"/> <br/>
      <input type="radio" value="2" name="radioFilter2"/> <br/>
      <input type="radio" value="3" name="radioFilter3"/> <br/>
</form>

SomeClass.java

public class SomeClass{
    public String value;

    public String filter1() {
        return value= "1";
    }

    public String filter2() {
        return value= "2";
    }

    public String filter3() {
        return value= "3";
    }
}

Controller.java

@RequestMapping("/servlet")
public String filterServlet(HttpServletRequest request, HttpServletResponse response) throws Exception {
    SomeClass s = new FilterSorting();
    if (request.getParameter("radioFilter1") != null) {
        s.value= s.filter1();
    } else if (request.getParameter("radioFilter2") != null) {
        s.value= s.filter2();
    } else if (request.getParameter("radioFilter3") != null) {
        s.value= s.filter3();
    }
    return "/newhtml.html";
}

@RequestMapping("/newhtml.html")
public String newhtml(Model model, HttpServletRequest request, HttpServletResponse response) throws Exception {
    FilterSorting s = new FilterSorting();
    list = Common.getList(dbSessionId, s.value);
    model.addAttribute("asnlist", asnList);
    return "/newhtml.html";
}

我使用的是Spring framework 4.0.0版。而且我已经尝试过使用servlet,但似乎没有用。 我想要的是,当我点击radioFilter2然后它将调用filter2(),其中设置参数值为2,依此类推。然后该值将用作加载内容的参数。但是从我的尝试来看,它并没有奏效。请帮助我,谢谢!

0 个答案:

没有答案