spring MVC控制器没有重定向到任何视图

时间:2017-05-29 08:29:34

标签: java spring jsp spring-mvc model-view-controller

我是spring和stackoverflow的新手。我的情况如下: 我在索引页面上单击了一个映射到控制器类的按钮,我到达那里但是由于控制器没有进一步重定向到所需的视图或jsp文件,因此进一步的映射不起作用。

这是我的控制器类

import java.util.List;
import model.Emp;
import model.EmpDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class EmpController {

@Autowired
EmpDao dao;//will inject dao from xml file  

/*It displays a form to input data, here "command" is a reserved request 
attribute 
 *which is used to display object data into form 
 */
@RequestMapping("/empform")
public ModelAndView showform() {
    return new ModelAndView("redirect:/empform", "command", new Emp());
}

/*It saves object into database. The @ModelAttribute puts request data 
 *  into model object. You need to mention RequestMethod.POST method  
 *  because default request is GET*/
@RequestMapping(value = "/save", method = RequestMethod.POST)
public ModelAndView save(@ModelAttribute("emp") Emp emp) {
    dao.save(emp);
    return new ModelAndView("redirect:/viewemp");//will redirect to viewemp request mapping  
}

/* It provides list of employees in model object */
@RequestMapping("/viewemp")
public ModelAndView viewemp() {
    List<Emp> list = dao.getEmployees();

    return new ModelAndView("redirect:/viewemp", "list", list);
}

/* It displays object data into form for the given id.  
 * The @PathVariable puts URL data into variable.*/
@RequestMapping(value = "/editemp/{id}")
public ModelAndView edit(@PathVariable int id) {
    Emp emp = dao.getEmpById(id);
    return new ModelAndView("empeditform", "command", emp);
}

/* It updates model object. */
@RequestMapping(value = "/editsave", method = RequestMethod.POST)
public ModelAndView editsave(@ModelAttribute("emp") Emp emp) {
    dao.update(emp);
    return new ModelAndView("redirect:/viewemp");
}

/* It deletes record for the given id in URL and redirects to /viewemp */
@RequestMapping(value = "/deleteemp/{id}", method = RequestMethod.GET)
public ModelAndView delete(@PathVariable int id) {
    dao.delete(id);
    return new ModelAndView("redirect:/viewemp");
}}

我检查过很多与此问题相关的问题,但遗憾的是没有任何效果。请帮忙。

调度-servlet.xml中

    <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMap
 ping"/>

    <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <props>
                <prop key="index.htm">indexController</prop>
            </props>
        </property>
    </bean>

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

    <bean name="indexController" class="org.springframework.web.servlet.mvc.ParameterizableViewController"
          p:viewName="index" />

的applicationContext.xml

<bean id="ds" class="org.springframework.jdbc.datasource.DriverManagerDataSource">  
    <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>  
    <property name="url" value="jdbc:mysql://localhost:3306/demoDB"></property>  
    <property name="username" value="root"></property>  
    <property name="password" value="password"></property>  
</bean>  

<bean id="jt" class="org.springframework.jdbc.core.JdbcTemplate">  
    <property name="dataSource" ref="ds"></property>  
</bean>  

<bean id="dao" class="model.EmpDao">  
    <property name="template" ref="jt"></property>  
</bean>

项目树:

Project Tree

1 个答案:

答案 0 :(得分:1)

我认为您正试图重定向到viewemp中的@Controller页面。它会一次又一次地重定向到同一个URL。不要执行redirect,而是提供View名称

/* It provides list of employees in model object */
@RequestMapping("/viewemp")
public ModelAndView viewemp() {
      List<Emp> list = dao.getEmployees();

      return new ModelAndView("viewemp", "list", list);
}