Java Spring URL变更并添加模型

huangapple go评论116阅读模式
英文:

Java spring url changes and adds model

问题

我在我的Web应用程序中有一个页面:

http://localhost:8080/user/form/prod/create/0

该页面具有以下表单属性:

<c:url var="addAction" value="/user/form/prod/create/${formProd.formHeader.headerId}"></c:url>
<form:form id="formSubmit" action="${addAction}" modelAttribute="formProd" class="form-horizontal" method="get">

-- 输入和提交按钮的代码 --

</form:form>

一旦单击按钮,它将经过以下控制器:

@RequestMapping(value = {"/user/form/prod/{type}/{headerId}"}, method = RequestMethod.GET)
public ModelAndView formCreateView(@Validated @ModelAttribute("formProd") FormProd formProd, BindingResult result
    , ModelMap model, Principal principal, @PathVariable(value="headerId") Long headerId, @PathVariable(value="type") String type
    , HttpServletRequest request) {

-- 做一些操作,并返回到相同的URL页面http://localhost:8080/user/form/prod/create/0

return new ModelAndView(AuthenticationBase.JSP_USER_FORM_PROD, model);
}

问题是URL会更改为:

http://localhost:8080/user/form/prod/create/0?formHeader.status=DRAFT&amp;formHeader.prodDate=&amp;formHeader.startDateTime=&amp;formHeader.endDateTime=&amp;lookupName=KMM1++++++++++++++++++++++++++&amp;formHeader.line=&amp;formHeader.machine=&amp;formHeader.prodCategoryId=10&amp;formHeader.product=&amp;formHeader.outputMeasureUnitId=13&amp;formHeader.unitPerCarton=&amp;formHeader.standardMachineSpeed=0.0&amp;formHeader.intervalMins=&amp;formHeader.sessionId=

其中还包括我的模型。由于URL不同,如果我再次点击或提交表单,它现在会显示"whitelabel error page":

Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.

Thu Sep 03 11:41:58 SGT 2020
There was an unexpected error (type=Not Found, status=404).

是否有关于URL更改的任何想法?

英文:

I have page in my web application:

http://localhost:8080/user/form/prod/create/0

which has a form attributes of

&lt;c:url var=&quot;addAction&quot; value=&quot;/user/form/prod/create/${formProd.formHeader.headerId}&quot; &gt;&lt;/c:url&gt;
&lt;form:form id=&quot;formSubmit&quot; action=&quot;${addAction}&quot; modelAttribute=&quot;formProd&quot; class=&quot;form-horizontal&quot; method=&quot;get&quot;&gt;

-- Codes for inputs and submit button --

&lt;/form:form&gt;

Once the button is clicked it will go through this controller:

@RequestMapping(value = {&quot;/user/form/prod/{type}/{headerId}&quot;}, method = RequestMethod.GET)
public ModelAndView formCreateView(@Validated @ModelAttribute(&quot;formProd&quot;) FormProd formProd, BindingResult result
		, ModelMap model, Principal principal, @PathVariable(value=&quot;headerId&quot;) Long headerId, @PathVariable(value=&quot;type&quot;) String type
		, HttpServletRequest request) {

-- Do some stuff AND return to the same url page http://localhost:8080/user/form/prod/create/0

return new ModelAndView(AuthenticationBase.JSP_USER_FORM_PROD, model);
}

The issue is the url changes to:

http://localhost:8080/user/form/prod/create/0?formHeader.status=DRAFT&amp;formHeader.prodDate=&amp;formHeader.startDateTime=&amp;formHeader.endDateTime=&amp;lookupName=KMM1++++++++++++++++++++++++++&amp;formHeader.line=&amp;formHeader.machine=&amp;formHeader.prodCategoryId=10&amp;formHeader.product=&amp;formHeader.outputMeasureUnitId=13&amp;formHeader.unitPerCarton=&amp;formHeader.standardMachineSpeed=0.0&amp;formHeader.intervalMins=&amp;formHeader.sessionId=

Which also includes my model. Since the url is different, If I click or submit the form again it now shows "whitelabel error page":

Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.

Thu Sep 03 11:41:58 SGT 2020
There was an unexpected error (type=Not Found, status=404).

Any idea why the url changes?

答案1

得分: 1

这是默认情况下用于GET类型映射的功能。

GET方法
在GET方法中,数据被发送作为URL参数,通常是由安培号(&)分隔的名称和值成对的字符串。一般来说,带有GET数据的URL将如下所示:
示例:
http://www.example.com/action?name=Sam&weight=55

将方法类型更改为POST,并且将表单方法也更改为POST,它就会起作用。

method = RequestMethod.GET

更改为

method = RequestMethod.POST

并且在视图页面中将表单类型更改为POST。

method="POST"
英文:

This is the by default functionality for GET type mappings.

The GET Method
In GET method the data is sent as URL parameters that are usually strings of name and value pairs separated by ampersands (&). In general, a URL with GET data will look like this:
Example :
http://www.example.com/action?name=Sam&amp;weight=55

Change method type it post and also form method to post and it works.

Change

method = RequestMethod.GET

To

method = RequestMethod.POST

And in view page change form type to POST.

method=&quot;POST&quot;

答案2

得分: 1

这里你正在尝试在按钮点击时添加数据,你使用了GET方法,这就是为什么每次点击按钮时URL都会发生变化。

修改代码如下,应该可以正常工作

<c:url var="addAction" value="/user/form/prod/create/${formProd.formHeader.headerId}"></c:url>
<form:form id="formSubmit" action="${addAction}" modelAttribute="formProd" class="form-horizontal" method="POST">

    -- 输入字段和提交按钮的代码 --

</form:form>

控制器代码更改

@RequestMapping(value = {"/user/form/prod/{type}/{headerId}"}, method = RequestMethod.POST)
public ModelAndView formCreateView(@Validated @ModelAttribute("formProd") FormProd formProd, BindingResult result
        , ModelMap model, Principal principal, @PathVariable(value="headerId") Long headerId, @PathVariable(value="type") String type
        , HttpServletRequest request) {

    -- 做一些处理并返回到相同的URL页面 http://localhost:8080/user/form/prod/create/0 --

    return new ModelAndView(AuthenticationBase.JSP_USER_FORM_PROD, model);
}

注意:由于你要求只返回翻译好的部分,我已经移除了其他内容。如果还有需要进一步解释的地方,请随时提问。

英文:

Here you are trying to add the data on click of a button and here you are using the GET method that's why your URL gets change every time when you click on the button.

modify code to below it should work

   &lt;c:url var=&quot;addAction&quot; value=&quot;/user/form/prod/create/${formProd.formHeader.headerId}&quot; &gt;&lt;/c:url&gt;
    &lt;form:form id=&quot;formSubmit&quot; action=&quot;${addAction}&quot; modelAttribute=&quot;formProd&quot; class=&quot;form-horizontal&quot; method=&quot;POST&quot;&gt;

-- Codes for inputs and submit button --

&lt;/form:form&gt;

Controller code change

@RequestMapping(value = {&quot;/user/form/prod/{type}/{headerId}&quot;}, method = RequestMethod.POST)
public ModelAndView formCreateView(@Validated @ModelAttribute(&quot;formProd&quot;) FormProd formProd, BindingResult result
        , ModelMap model, Principal principal, @PathVariable(value=&quot;headerId&quot;) Long headerId, @PathVariable(value=&quot;type&quot;) String type
        , HttpServletRequest request) {

-- Do some stuff AND return to the same URL page http://localhost:8080/user/form/prod/create/0

return new ModelAndView(AuthenticationBase.JSP_USER_FORM_PROD, model);
}

huangapple
  • 本文由 发表于 2020年9月3日 11:46:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/63716519.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定