【问题标题】:Request parameter is null calling methods on MultiActionController请求参数为空调用 MultiActionController 上的方法
【发布时间】:2011-04-07 22:35:04
【问题描述】:

在 Spring 3.0 中

如果我有一个带有两个不同链接的 jsp 页面,每个链接在 MultiActionController 上调用不同的方法

<form:form method="POST">
    <a href="user.htm?action=add" style="color: blue;">Add</a>
    <a href="user.htm?action=delete" style="color: blue;">Delete</a>
</form:form>

在 MultiActionController 上,我将请求参数设为 null,因此无法验证值

String username=request.getParameter("username");
String password=request.getParameter("password");

我也尝试使用一个按钮并将其更改为看起来像一个链接,但在按钮单击的情况下,添加方法被调用两次,请求参数为空,第二次使用正确的值,但这两次输入在代码也使这项工作我正在使用表单操作,这在两个不同的方法调用的情况下将不起作用

 <form:form action="user.htm?action=add method="POST">
     <input type="submit" value="I have info"/>"> ---> should call delete method
     <input type="submit" value="Click to send info"/> ---> should call add method 

    </form:form>

想在没有 javascript 的情况下实现这一目标

我还在 xml 文件中设置了参数 reslover,默认调用方法

让我再次解释一下我的问题,忘记上面我只是举个例子的代码

我有一个 jsp 页面,它有两个输入文本字段和两个链接,每个链接都应该调用控制器的不同方法,该方法都将验证输入并将重定向到另一个简单的页面!

我使用 MultiActionController 的原因....... 不幸的是,我必须继续使用扩展 MultiActionController 的控制器,因为同一个 jsp 页面也有分页,它工作得很好

所以我要做的只是在单击任何一个链接后实现服务器和客户端验证并相应地重定向。

请给我一些例子,让我在这方面继续前进......

我想在没有 javascript 的情况下实现这个我在这里得到了这个例子 但我的问题是为什么 requestParameter 为 Null

http://www.goospoos.com/2009/11/spri...oller-example/

这是我的代码

<bean id="myExampleController" class="com.ui.controller.MyExampleController">
            <property name="methodNameResolver">
            <ref bean="paramResolver" />
        </property> 
        <property name="validators" ref="myExampleValidator"/>       
    </bean>

    <bean id="paramResolver"
        class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver">
<property name="defaultMethodName" value="loadPage"></property>  
<property name="paramName">
<value>action</value>            
</property>     
</bean>

<bean id="myExampleValidator" class="com.validator.MyExampleValidator" />

控制器

public ModelAndView validateValues(HttpServletRequest request, HttpServletResponse response) throws Exception{
ModelAndView mav=null;
----> this is null ??? 
String value1=request.getParameter("textvalue1");
String value2=request.getParameter("textvalue2");


mav = new ModelAndView("myexample");
mav=getPageData(request, false); 
return mav;

}

JSP 页面

<form action="myexample.htm" method="post">

input type="text" name="textvalue1" size="20" />
input type="text" name="textvalue2" size="20" />
</form>

<a href="myexample.htm?action=validateValues">click to validate</a>

--------->如果上面提到的站点可以调用方法并且工作正常,这是什么问题,为什么我无法获取请求参数

【问题讨论】:

    标签: java html spring spring-mvc


    【解决方案1】:

    您应该考虑重新设计您的控制器。您所需的操作应在上下文中属于语义标识符。对于用户实体的 CRUD(创建、读取、更新、删除)操作,我将 RequestMappings 设置为:

    • /user(根用户索引页面,列出所有用户)
    • /user/add(创建新用户)
    • /user/delete/{id}(按id删除用户)
    • /user/{id}(通过id查找用户)

    在控制器级别,您可以通过以下方式完成此操作:

    @RequestMapping(value="/user")
    public class UserController
    {
        @RequestMapping(method=RequestMethod.GET)
        public String getListUsers() // default page that is resolved by /user
        {
            return "user/list"; // used by view resolver
        }
    
        @RequestMapping(value="/add.html" method=RequestMethod.GET)
        public String getAddUser(Model model)
        {
            //add person form backing entity to model
            return "user/add"; // used by view resolver to locate view
        }
    
        @RequestMapping(value="/add.html" method=RequestMethod.POST)
        public String postAddUser(@ModelAttribute person) // other objects for errors, etc.
        {
            // add person, validate person, etc.
            return "redirect:/user"; // redirects to index
        }
    }
    

    等等...对于其他 URI,您将遵循这些模式

    【讨论】:

    • 不能使用注解。框架被设计为使用 xml 配置,你能给我举个使用 MultiActionController 的例子吗?
    • 设计为使用 XML 配置是什么意思?注释也是通过 spring config 配置的。上面的例子是一个多动作控制器。
    【解决方案2】:
    Then below works for me
    

    用户界面

      <form:form action="user.htm?action=add method="POST">  
    <input type="submit" name="add"/>
    

    服务器端

    (WebUtils.hasSubmitParameter(request, "add")
    

    【讨论】:

      猜你喜欢
      • 2020-12-14
      • 1970-01-01
      • 1970-01-01
      • 2017-05-08
      • 2016-09-06
      • 1970-01-01
      • 2011-06-24
      • 1970-01-01
      • 2018-10-11
      相关资源
      最近更新 更多