80106

How to get a trivial case of Spring MVC view (JSP) resolving to work?

My app uses Spring MVC (latest; 3.2.2) to create a RESTful API returning JSON, and so far I haven't needed a view layer at all. But now, besides the API, I need a simple utility page (plain dynamic HTML) and wanted to use JSP for that.

I want requests to http://localhost:8080/foo/<id> to go through a controller (Java) and end up in a JSP. Should be simple, right? But I'm getting 404; something is not right in resolving the view.

HTTP ERROR 404 Problem accessing /jsp/foo.jsp. Reason: Not Found

<strong>Controller</strong>:

@RequestMapping(value = "/foo/{id}") public String testing(@PathVariable String id, ModelMap model) { model.addAttribute("id", id); return "foo"; }

Defining controllers and mapping requests works; this method gets called just fine.

<strong>Spring config</strong>:

<mvc:annotation-driven/> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/jsp/" p:suffix=".jsp"/>

The problem is probably here. I've experimented with slightly different prefixes and putting the JSPs under WEB-INF, as well as stuff like <mvc:view-controller path="/*" /> but no luck yet.

(Do I even need to specify InternalResourceViewResolver, or should default view resolvers take care of this?)

<strong>JSP files</strong>. Under src/main/webapp/jsp (the project uses Maven conventions) I obviously have the JSPs.

<img src="https://i.stack.imgur.com/YN3K2.png" width="250">

Is there something wrong with this location?

<strong>web.xml</strong>:

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

I have browsed through Spring MVC documentation, but my problem is probably too trivial and obvious to easily find help there. :-P

Can anyone enlighten me on what I'm doing wrong?

Answer1:

I think what you need to do is changing

<servlet-mapping> <servlet-name>mvc-dispatcher</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping>

to

<servlet-mapping> <servlet-name>mvc-dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>

/* won't match if there is another folder in the path, like /jsp/foo.jsp. On the other hand / will match everything.

Recommend

  • How can I send a form's contents over e-mail with JavaScript?
  • how to control the user authentication using spring mvc
  • Spring mvc: css does not work when adding slash at the end of URL
  • Neither BindingResult nor plain target object for bean name 'userProfile' available as req
  • Spring Security: @PreAuthorize works only together with @RequestMapping
  • How to provide hyperlink in email pointing to a specific method inside gwt app (but not main page)
  • C# Application Relative Paths
  • How to prevent cross domain issues by proxying in IIS?
  • Type mismatch: cannot convert from Connection to Connection
  • Performance difference between accessing local and class member variables
  • Spring MVC @RequestParam a list of objects
  • Access PCF DEV from external machine on same network as host
  • Conditional render in tagfile depending on whether the attribute is specified or not
  • Spring MVC redirect with custom http headers
  • Is it mandatory to have a doGet or doPost method?
  • Spring boot mapping static html
  • Clear fused location provider's location for testing
  • Trying to get the char code of ENTER key
  • Using Sax parsing to edit and write XML in VB6
  • Debug.DrawLine not showing in the GameView
  • Jetty 9 HashLoginService
  • Problem deserializing objects from cache on MyBatis 3/Java
  • MongoDb aggregation
  • azure media services - The request body is too large and exceeds the maximum permissible limit
  • Yii2: Config params vs. const/define
  • Unity3D & Android: Difference between “UnityMain” and “main” threads?
  • How to create a file in java without a extension
  • OpenGL ES texture problem, 4 duplicate columns and horizontal lines (Android)
  • Apache 2.4 and php-fpm does not trigger apache http basic auth for php pages
  • Cross-Platform Protobuf Serialization
  • ORA-29908: missing primary invocation for ancillary operator
  • How to format a variable of double type
  • Cannot Parse HTML Data Using Android / JSOUP
  • JTable with a ScrollPane misbehaving
  • coudnt use logback because of log4j
  • unknown Exception android
  • JaxB to read class hierarchy
  • failed to connect to specific WiFi in android programmatically
  • Converting MP3 duration time
  • How can I use threading to 'tick' a timer to be accessed by other threads?