0
votes

I have problem with spring-mvc/tomcat, and more specifically with Url When I'm trying to execute: http://localhost:8080/HelloWeb/index.html

WARNING: No mapping found for HTTP request with URI [/HelloWeb/index.html] in DispatcherServlet with name 'HelloWeb'

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:

@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:

<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

4

4 Answers

0
votes

What if you add .html to your RequestMappings e.g. @RequestMapping(value="/index.html")

0
votes

In your web.xml, you've mapped the DispatcherServlet to handle requests matching *.html. But your StudentController is not mapped to handle such requests.

Modify the value in @RequestMapping to include .html extension.

@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

-----------Edit-----------------------------

Verify the Controller is getting initialized by 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");
       }

}
0
votes

I do not see <mvc:annotation-driven/> in your servlet config. Can you add and check. Thanks.

0
votes

If you are trying to deploy your HTML files as simple static files, you should configure them as such. Add this to your dispatcher servlet, making sure you have the proper namespace declared:

<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. Right now, your controller is most likely trying to find a JSP view named index.html, which does not exist.