Spring注释MVC返回404

时间:2015-06-02 11:14:54

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

我已经构建了一个返回404的Spring MVC骨架Helloworld应用程序。这里是代码:

控制器:

<!DOCTYPE html>
<html>
<head>
<style>
table, td {
    border: 1px solid black;
}
</style>
</head>
<body>

<p>Click the button to add a new row at the first position of the table and then add cells and content.</p>
<div id="tableContainer">

</div>
<br>

<button onclick="myFunction()">Try it</button>
<button onclick="readTableData()"> Read it </button>
<script>
function myFunction() {
    var tab = '<table id="MarksTable">';
    var counter = 0;
    for(i = 0; i< 4; i++){
        tab = tab + '<tr><td rowspan = "4"> Dept1 </td><td> <input type="text" id="inputcell'+counter+'"  value="'+i+'"/> </td></tr>';
        counter++;
        tab = tab+'<tr><td> <input type="text" id="inputcell'+counter+'"  value="'+i+'"/> </td></tr>';
        counter++;
        tab = tab+'<tr><td> <input type="text" id="inputcell'+counter+'"  value="'+i+'"/> </td></tr>';
        counter++;
        tab = tab+'<tr><td> <input type="text" id="inputcell'+counter+'"  value="'+i+'"/> </td></tr>';
        counter++;
    }
    tab = tab + '</table>';
    document.getElementById("tableContainer").innerHTML = tab;
}

function readTableData(){
    var val;
    var table = document.getElementById("MarksTable");
    var column_count = table.rows[1].cells.length;
    var rowcount = table.rows.length;
    alert(rowcount);
    if(column_count>0){
        for(var index = 0; index < rowcount;index++){
            var row = table.rows[index];
            val = document.getElementById("inputcell"+index);
            alert(val.value);

            //marks = row.cells[0].getElementsByName('inputcell').value;
    //Or marks[index] = document.getElementsByName('inputcell' + index)[0].value;
        }
    }
   alert(val);
}
</script>

</body>
</html>

配置:

@Controller
@RequestMapping("/")
public class HelloWorldController {
  @RequestMapping(value = {"/helloworld**"}, method = RequestMethod.GET)
  public ModelAndView welcomePage() {
    ModelAndView model = new ModelAndView();
    model.setViewName("helloworld");
    return model;
  }
}

WEB-INF / views / helloworld.jsp中没有XML配置和helloworld.jsp

请求http://localhost:8080/helloworld.jsp时,我会收到404.

启动时来自Tomcat的日志:

@Configuration
public class MvcWebApplicationInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

    @Override
    protected Class<?>[] getRootConfigClasses() {
      return new Class[]{RootConfig.class};
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
      return new Class[]{ServletConfig.class};
    }

    @Override
    protected String[] getServletMappings() { return new String[]{"/"}; }
}

@EnableWebMvc
@Configuration
@ComponentScan(basePackages =  "com.tmlink.springmvc.controller.*")
public class RootConfig extends WebMvcConfigurerAdapter {
  @Bean
  public InternalResourceViewResolver viewResolver() {
    InternalResourceViewResolver viewResolver
            = new InternalResourceViewResolver();
    viewResolver.setViewClass(JstlView.class);
    viewResolver.setPrefix("/WEB-INF/views/");
    viewResolver.setSuffix(".jsp");
    return viewResolver;
  }
}

@Configuration
public class ServletConfig extends WebMvcConfigurerAdapter {}

知道这里有什么问题吗?

2 个答案:

答案 0 :(得分:0)

更改为

  

@RequestMapping( “/ *”)

答案 1 :(得分:0)

发现错误:

@ComponentScan(basePackages =&#34; com.tmlink.springmvc.controller。*&#34;)

应该是:

@ComponentScan(basePackages =&#34; com.tmlink.springmvc.controller&#34;)

在RootConfig类中