spring mvc not bind model to view

时间:2018-01-19 03:58:04

标签: java spring jsp spring-mvc

我正在探索spring mvc,但在查看绑定模型时会遇到问题。 我的控制器:

                    OpenFileDialog1.ShowDialog()
                TextBox1.text = (IO.File.ReadAllLines(OpenFileDialog1.FileName))
    if TextBox1.text contains (":") then 
    TextBox2.text = (Text After The :)
    TextBox1.text = (Text Before the :)
Do all the functions with that login
Then read the next line and use that pair login details (USERNAME2:PASSWORD2)

我在/ WEB-INF / advertiser目录中也有一个edit.jsp文件。

我的追踪器:

@Controller
@RequestMapping(value = "/advertiser")

public class AdvertiserController {
    @Autowired
    private AdvertiserService advertiserService;
    @RequestMapping(value="/edit/{id}", method = RequestMethod.GET)
    public ModelAndView edit(@PathVariable long id){
            Advertiser adv=advertiserService.get(id);
            return new ModelAndView("advertiser/edit","command",adv);
    }
}

然而,当我转到:http://localhost:8080/springmvcdemo/advertiser/edit/1

时,我刚收到404错误

错误说明:/springmvcdemo/WEB-INF/advertiser/edit/1.jsp

看起来spring正在尝试找到一个1.jsp文件来提供请求,而不是像我想要的那样使用edit.jsp。

更新

我只是向我的控制器添加了一个功能,它也不起作用:

<bean id="viewResolver"
      class="org.springframework.web.servlet.view.InternalResourceViewResolver"
      p:prefix="/WEB-INF/"
      p:suffix=".jsp" />

问题仍然存在,描述中的404错误:

@RequestMapping(value="/add2", method = RequestMethod.GET)
public ModelAndView add2(ModelAndView mav){


        mav.addObject("command", new Advertiser());
        mav.setViewName("advertiser/add");

        return mav;
}

我已将该观点指向&#34; advertiser / add&#34;但春天仍然试图找到add2.jsp而不是add.jsp。这让我觉得我在viewResolver配置中失败但仍然没有找到它是什么。

2 个答案:

答案 0 :(得分:0)

将路径更改为@RequestMapping(value="/edit/{id}", method = RequestMethod.GET) public ModelAndView edit(@PathVariable long id){ Advertiser adv=advertiserService.get(id); return new ModelAndView("advertiser/edit","command",adv); } ,如下所示。

// Module Import Statements //
import {BrowserModule} from '@angular/platform-browser';
import {NgModule} from '@angular/core';
import {NgbModule} from '@ng-bootstrap/ng-bootstrap';
import {RouterModule, Routes} from '@angular/router';

import { CalendarModule } from './scripts/calendar/calendar.module';

// Component Import Statements //
import { AppComponent } from './app.component';

// Routes Declaration Statements //

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    CalendarModule,
    BrowserModule,
    NgbModule.forRoot(),
    RouterModule.forRoot([
      {
           path: 'calendar', component: CalendarComponent
      }
    ])
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

答案 1 :(得分:0)

你需要检查点。

<强>的servelt-context.xml中

<!-- Resolves views selected for rendering by @Controllers to .jsp resources 
in the /WEB-INF/views directory -->
<beans:bean id="jspViewResolver"
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <beans:property name="viewClass"
        value="org.springframework.web.servlet.view.JstlView" />
    <beans:property name="prefix" value="/WEB-INF/views/" />
    <beans:property name="suffix" value=".jsp" />
</beans:bean>

<强>控制器

@Controller
@RequestMapping(value = "/advertiser")
public class AdvertiserController {

    @Autowired
    private AdvertiserService advertiserService;

    @RequestMapping(value="/edit/{id}", method = RequestMethod.GET)
    public ModelAndView edit(@PathVariable long id, ModelAndView mav){
         Advertiser adv = advertiserService.getAdvertiserById(id);

         mav.addObject("command", adv);
         mav.setViewName("folder/viewName");

         return mav;
    }
}

示例目录:webapp / WEB-INF / views / advertiser / 编辑 .jsp

示例案例:mav.setViewName(advertiser / edit );使用viewName。

我想改变你的代码。如果您的目录是WEB-INF / advertiser / add2 .jsp

您的更新:

mav.setViewName("advertiser/**add2**");

示例代码

您的目录中有文件(WEB-INF / advertiser / add和add2.jsp)。

显示add.jsp

    @RequestMapping(value="/one", method = RequestMethod.GET)
    public ModelAndView edit(ModelAndView mav){

         mav.setViewName("advertiser/add");

         return mav;
    }

显示add2.jsp

    @RequestMapping(value="/two", method = RequestMethod.GET)
    public ModelAndView edit(ModelAndView mav){

         mav.setViewName("advertiser/add2");

         return mav;
    }