SpringMVC pit filling record

Recording the problems I encountered in the development process of using springMVC as a rookie.


[1 ModelAndView object setViewName() cannot get the rendered page]
Description: The code can be executed and no error is reported, but the access is that the jsp page specified by setViewName() cannot be rendered. The page reports HTTP Status 404 -an error.

@RequestMapping(value="/index.jsp")
    public ModelAndView handleRequest(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception {
        System.out.println("handlerequest 被调用");    
        ModelAndView mv=new ModelAndView();
        mv.addObject("message", "hello world!");
        mv.setViewName("/WEB-INF/pages/index.jsp");
        return mv;
    }

Reason: Use logging log4j to output the running status from the console (for development, log output is a powerful tool to make the running process of the program "visible"). You can see the following information

17:34:40,786  WARN PageNotFound:1172
 - No mapping found for HTTP request with URI [/Suhe/WEB-INF/pages/index.jsp] 
 - in DispatcherServlet with name 'springmvc'

The general meaning is /Suhe/WEB-INF/pages/index.jspthat this url request DispatcherServlet(前端请求控制分发器)cannot be found mapping. In fact, the server treats this mv.setViewName("/WEB-INF/pages/index.jsp")operation as an http request and DispatcherServletprocesses it. In web.xml, servlet-mappingthe matching rules are set, as follows, .jspthe HTTP request at the end will be successfully matched, intercepted and handed over to DispatcherServletdistribution processing.

<servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>*.jsp</url-pattern>
</servlet-mapping>

Solution: Change the url-patternmatching interception setting to other forms, for example *.do或则.html 等等, the corresponding @RequestMapping() can also be modified accordingly


[2 Solve the problem that the automatically generated parameters in eclipse are arg0, arg1]
see Solving the problem that the automatically generated parameters in eclipse are arg0, arg1

[3 To develop a web project in Eclipse for javaee, the jar package must be placed in the /WEB-INF/lib folder]

  1. The tomcat application server, which has its own class loader, goes to the path of %web-project%/WEB-INF/lib to find the jar file under the corresponding lib according to the J2EE specification
  2. When using eclipse javaee IDE, all libs in WEB-INF/lib will be automatically added to library
  3. The library under the eclipse project is used to compile the java files in the src. When it is actually published to tomcat, only the jar package in WEB-INF/lib is copied, so it appears that eclipse can be compiled normally but tomcat cannot be found. kind
  4. If you are using TOMCAT as a server now, then the JAR packages that need to be depended on should be tested in WEB-INF/lib, TOMCAT will automatically compile it and put it in the WEB-INF/classes directory (of course, your src code will also be put in the WEB-INF/classes directory after compilation). here), it should be done, this is the rule.

Of course, under Eclipse, deploy the jar package to other folders instead of the WEB-INF/lib folder. In this way, when deployed to the server, these packages can be associated and uploaded to the server. Specifically, the project needs to be configured. Under the .classpath, just add the path of the jar that needs to be associated under this file.
For example (I put the jar under the lib file in the root directory, note that it is not the folder of WEB-INF/lib):

<classpathentry kind="lib" path="lib/acegi-security-1.0.6.jar"/>
<classpathentry kind="lib" path="lib/activation-1.1.jar"/>
<classpathentry kind="lib" path="lib/antlr-2.7.6.jar"/>
<classpathentry kind="lib" path="lib/aopalliance-1.0.jar"/>
<classpathentry kind="lib" path="lib/asm-1.5.3.jar"/>

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325389932&siteId=291194637