简体   繁体   English

Spring MVC,tomcat url 404错误

[英]Spring MVC, tomcat url 404 error

I have problem with spring-mvc/tomcat, and more specifically with Url When I'm trying to execute: http://localhost:8080/HelloWeb/index.html 我遇到spring-mvc / tomcat的问题,更具体地说是Url当我试图执行时: http:// localhost:8080 / HelloWeb / index.html

WARNING: No mapping found for HTTP request with URI [/HelloWeb/index.html] in DispatcherServlet with name 'HelloWeb' 警告:找不到名为“HelloWeb”的DispatcherServlet中带有URI [/HelloWeb/index.html]的HTTP请求的映射

HelloWeb-servlet.xml: 的HelloWeb-servlet.xml中:

<context:component-scan base-package="pl.solsoft.web"/>

<bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
    <property name="templateLoaderPath" value="/WEB-INF/pages"/>
        </bean>
<bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
    <property name="cache" value="true"/>
    <property name="prefix" value=""/>
    <property name="suffix" value=".ftl"/>
</bean>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/pages/"/>
    <property name="suffix" value=".jsp"/>
</bean>

StudentController: StudentController:

@Controller
public class StudentController{
private static List<User> userList=new ArrayList<User>();
static {
    userList.add(new User("Bill", "Gates"));
    userList.add(new User("Kasia", "K"));
}
@RequestMapping(value = "/index", method = RequestMethod.GET)
public String index (@ModelAttribute("model") ModelMap model){
    model.addAttribute("userList", userList);
    return "index";
}
@RequestMapping(value = "/add", method = RequestMethod.POST)
public String add(@ModelAttribute("user") User user){
    if (null != user && null != user.getName()
            && null != user.getLastName() && !user.getName().isEmpty()
            && !user.getLastName().isEmpty()) {
        synchronized (userList) {
            userList.add(user);
        }
    }
    return "redirect:index.html";
}
}

web.xml: web.xml中:

<display-name>HW</display-name>
<welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.jsp</welcome-file>
</welcome-file-list>

<servlet>
    <servlet-name>HelloWeb</servlet-name>
    <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>HelloWeb</servlet-name>
    <url-pattern>*.html</url-pattern>
</servlet-mapping>

Thank U for all tips 感谢U的所有提示

如果您将.html添加到RequestMappings,例如@RequestMapping(value =“/ index.html”),该怎么办?

In your web.xml, you've mapped the DispatcherServlet to handle requests matching *.html. 在您的web.xml中,您已映射DispatcherServlet以处理与* .html匹配的请求。 But your StudentController is not mapped to handle such requests. 但是您的StudentController未映射以处理此类请求。

Modify the value in @RequestMapping to include .html extension. 修改@RequestMapping中的值以包含.html扩展名。

@RequestMapping(value = "/index.html", method = RequestMethod.GET)
public String index (@ModelAttribute("model") ModelMap model){
   ......
   return "index";
}

@RequestMapping(value = "/add.html", method = RequestMethod.POST)
public String add(@ModelAttribute("user") User user){
   .....
   return "redirect:index.html";
}

Now try to access your page by going to http://localhost:8080/HelloWeb/index.html 现在尝试访问http:// localhost:8080 / HelloWeb / index.html来访问您的页面

-----------Edit----------------------------- - - - - - -编辑 - - - - - - - - - - - - - - -

Verify the Controller is getting initialized by Spring. 验证Controller是否已被Spring初始化。 To do that, create a no-arg constructor and try to print something to the console. 为此,创建一个无参数构造函数并尝试将某些内容打印到控制台。

@Controller
public class StudentController{

       public StudentController(){
              System.out.println("Hey, I'm in StudentController");
       }

}

I do not see <mvc:annotation-driven/> in your servlet config. 我在servlet配置中没有看到<mvc:annotation-driven/> Can you add and check. 你可以添加和检查。 Thanks. 谢谢。

If you are trying to deploy your HTML files as simple static files, you should configure them as such. 如果您尝试将HTML文件部署为简单的静态文件,则应将其配置为这样。 Add this to your dispatcher servlet, making sure you have the proper namespace declared: 将此添加到您的调度程序servlet,确保您声明了正确的名称空间:

<mvc:resources mapping="*.html" location="/" />

You don't need a controller method to serve this file. 您不需要控制器方法来提供此文件。 If you want additional logic on the back-end, you can have your request to /index do its thing and then redirect to your file, or just convert index.html to a .jsp file. 如果你想在后端使用额外的逻辑,你可以让你的请求/index做它的事情,然后重定向到你的文件,或者只是将index.html转换为.jsp文件。 Right now, your controller is most likely trying to find a JSP view named index.html , which does not exist. 现在,您的控制器很可能正在尝试查找名为index.html的JSP视图,该视图不存在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM