java.io.FileNotFoundException: OSGi resource[/WEB-INF/views/解决方式

本文探讨了Spring MVC中DispatcherServlet的配置问题,解释了如何正确设置URL映射以避免文件找不到的错误,并通过示例展示了如何使用FreeMarker作为视图层模板引擎。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

java.io.FileNotFoundException:OSGiresource[/WEB-INF/views/|bnd.id=235|bnd.sym=com.zhanyun.cloud.web.clientUI]cannot be resolved to absolute file path

         atorg.springframework.osgi.io.OsgiBundleResource.getFile(OsgiBundleResource.java:345)[100:org.springframework.osgi.io:1.2.1]


http://stackoverflow.com/questions/1266303/no-mapping-found-for-http-request-with-uri-web-inf-pages-apiform-jsp/2923511#2923511

 


Looks like DispatcherServlet is trying to process the request for apiForm.jsp, which suggests to me that your web.xml servlet-mapping is directing requests for that space to DispatcherServlet.

You might have something like this?

<servlet-mapping>

  <servlet>dispatcher</servlet>

 <url-pattern>/*</url-pattern>

</servlet-mapping>

Try calling your controllers with a different extension (.do for example) and update the servlet-mapping to suit

 <servlet-mapping>

  <servlet>dispatcher</servlet>

  <url-pattern>*.do</url-pattern>

</servlet-mapping>

share|improve this answer

answered Aug 12 '09 at 14:54

 

ptomli
4,74111429

yes, u are right. can u explain more, why i cannot set dispatcher as /* ? – cometta Aug 12 '09 at 15:01

3

 

When you set the url-pattern to /* then all requests will be sent to that DispatcherServlet, which includes the request for JSP rendering. Though it's not true, it's sometimes useful to think of the InternalResourceView (and derived like JstlView) as another HTTP request, since that way you'll see why a request for the JSP is getting picked up by the DispatcherServlet. – ptomli Aug 12 '09 at 15:16

when i set to / , i the request is render fine. only when i set /* . what is the different / and /* ? – comettaAug 12 '09 at 15:27

12

 

<url-pattern>/</url-pattern> only matches the URL host/servlet <url-pattern>/*</url-pattern> matches everything under host/servlet, such as /index.html, /foo.jpg and, most importantly in this case, /WEB-INF/pages/apiForm.jsp the * is the wildcard, which says "anything" In the earlier suggestion, *.do matches anything that ends in .do, for example, /foo.do, /foo/bar.do. It doesn't match anything ending in jsp, so a request for /WEB-INF/pages/apiFrom.jsp is not matched, and is not routed to the DispatcherServlet – ptomliAug 12 '09 at 15:42

 

 

thanks for your answer, helped a lot – arturh Sep 9 '09 at 15:21

up vote45down vote

Yes, I know I'm late to this party but it might help others.

The servlet container chooses the mapping based on the longest path that matches. So you can put this mapping in for your JSPs and it will be chosen over the /* mapping.

<servlet-mapping>

  <servlet-name>jsp</servlet-name>

  <url-pattern>/WEB-INF/pages/*</url-pattern>

 </servlet-mapping>

Actually for Tomcat that's all you'll need since jsp is a servlet that exists out of the box. For other containers you either need to find out the name of the JSP servlet or add a servlet definition like:

<servlet>

  <servlet-name>jsp</servlet-name>

  <servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class>

</servlet>

share|improve this answer

answered May 27 '10 at 17:49

 

sourcedelica
8,66921424

1

 

Thanks very much for writing this, it was exactly what I was looking for! – MikeeMike Feb 24 '11 at 16:00

1

 

This is perfect, allows for no extensions! Thanks, – Bob Fincheimer Mar 23 '11 at 13:44

2

 

Jetty also uses the Jasper servlet by default under the servlet name "jsp", so the same mapping would work with Jetty. – yincrash Aug 21 '11 at 23:27

 

up vote3down vote

With Spring 3.1 and Tomcat 7 I got next error:

org.springframework.web.servlet.DispatcherServlet noHandlerFound No mapping found for HTTP request with URI [/baremvc/] in DispatcherServlet with name 'appServlet'

And I needed to add to web.xml next configuration:

<welcome-file-list>

    <welcome-file></welcome-file>

</welcome-file-list>

And the application worked!

http://viralpatel.net/blogs/freemarker-servlet-tutorial-example/



ile: /WebContent/WEB-INF/web.xml

<? xml version = "1.0" encoding = "UTF-8" ?>
< web-app xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
     xmlns = "http://java.sun.com/xml/ns/javaee"
         xmlns:web = "http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
 
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
 
     id = "WebApp_ID" version = "2.5" >
 
     < display-name >FreeMarker_Hello_World</ display-name >
     < welcome-file-list >
         < welcome-file >index.html</ welcome-file >
     </ welcome-file-list >
 
     < servlet >
         < servlet-name >freemarker</ servlet-name >
         < servlet-class >freemarker.ext.servlet.FreemarkerServlet</ servlet-class >
 
         <!-- FreemarkerServlet settings: -->
         < init-param >
             < param-name >TemplatePath</ param-name >
             < param-value >/</ param-value >
         </ init-param >
         < init-param >
             < param-name >NoCache</ param-name >
             < param-value >true</ param-value >
         </ init-param >
         < init-param >
             < param-name >ContentType</ param-name >
             < param-value >text/html; charset=UTF-8</ param-value >
             <!-- Forces UTF-8 output encoding! -->
         </ init-param >
 
         <!-- FreeMarker settings: -->
         < init-param >
             < param-name >template_update_delay</ param-name >
             < param-value >0</ param-value >
             <!-- 0 is for development only! Use higher value otherwise. -->
         </ init-param >
         < init-param >
             < param-name >default_encoding</ param-name >
             < param-value >ISO-8859-1</ param-value >
             <!-- The encoding of the template files. -->
         </ init-param >
         < init-param >
             < param-name >number_format</ param-name >
             < param-value >0.##########</ param-value >
         </ init-param >
 
         < load-on-startup >1</ load-on-startup >
     </ servlet >
 
     < servlet-mapping >
         < servlet-name >freemarker</ servlet-name >
         < url-pattern >*.ftl</ url-pattern >
     </ servlet-mapping >
 
 
     < servlet >
         < servlet-name >hello_servlet</ servlet-name >
         < servlet-class >net.viralpatel.freemarker.HelloServlet</ servlet-class >
     </ servlet >
 
     < servlet-mapping >
         < servlet-name >hello_servlet</ servlet-name >
         < url-pattern >/hello</ url-pattern >
     </ servlet-mapping >
 
     <!--
         Prevent the visiting of MVC Views from outside the servlet container.
         RequestDispatcher.forward/include should and will still work. Removing
         this may open security holes!
     -->
     < security-constraint >
         < web-resource-collection >
             < web-resource-name >FreeMarker MVC Views</ web-resource-name >
             < url-pattern >*.ftl</ url-pattern >
         </ web-resource-collection >
         < auth-constraint >
             <!-- Nobody is allowed to visit these -->
         </ auth-constraint >
     </ security-constraint >
 
</ web-app >

To start with first we make an entry for freemarker.ext.servlet.FreemarkerServlet servlet inweb.xml. Note how we mapped this servlet with url-pattern *.ftl. Thus all the request that ends with.ftl will get processed by FreemarkerServlet servlet.

In addition to this, we also provided few parameters to FreemarkerServlet. These parameters are more or less self explanatory. Have a look at comment.

For our servlet app, we defined a new servlet net.viralpatel.freemarker.HelloServlet which we mapped to url /hello. This serves as an entry point to our app. We start with /hello page.

The FTL templates will be saved in .ftl files in our WebApp folder. To avoid any unwanted access to these files we defined security-constraint in web.xml.

Once we have setup web.xml, we add Java source code.

Step 3: Hello World FreeMarker

Create a class User.java under /src/net/viralpatel/freemarker folder. This class serves as bean class which we uses to set User data. Copy following code in User.java.

File: /src/net/viralpatel/freemarker/User.java

package net.viralpatel.freemarker;
 
public class User {
     private String firstname;
     private String lastname;
 
     public User() {
     }
 
     public User(String firstname, String lastname) {
         this .firstname = firstname;
         this .lastname = lastname;
 
     }
 
     //Getter and Setter methods
 
}

Once User bean is created, we add HelloServlet. Create HelloServlet.java under/src/viralpatel/freemarker and copy following code.

File: /src/net/viralpatel/freemarker/HelloServlet.java

package net.viralpatel.freemarker;
 
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
 
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
public class HelloServlet extends HttpServlet {
     
     private static final long serialVersionUID = 1L;
     
     private static List<User> userList = new ArrayList<User>();
     
     //Just prepare static data to display on screen
     static {
         userList.add( new User( "Bill" , "Gates" ));
         userList.add( new User( "Steve" , "Jobs" ));
         userList.add( new User( "Larry" , "Page" ));
         userList.add( new User( "Sergey" , "Brin" ));
         userList.add( new User( "Larry" , "Ellison" ));
     }
}

We haven’t defined any doGet or doPost methods in servlet. We will add them shortly. Note how we defined a static instance of List<User>. We use it to store user list. Ideally instead of doing this, you should store user information in database. But for sake of simplicity of this example we store users in this static list.

Add following doGet() method in HelloServlet. In this method we just set User list in request viarequest.setAttribute("users", userList); and forwards the request to /index.ftl. We do HTTP Forward here. The request gets forwarded to index.ftl. As this URL ends with .ftl, the servlet container triggers FreemarkerServet. This servlets load the index.ftl template file and renders the output.

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
         throws ServletException, IOException {
     //Put the user list in request and
     //let freemarker paint it.
     request.setAttribute( "users" , userList);
     
     request.getRequestDispatcher( "/index.ftl" ).forward(request, response);
 
}

In our example, we also have functionality of adding a user. We have a form with two fields firstname and lastname. User can add new users through this form. We define a doPost() method which gets called when add user form is submitted. In this method we get firstname, lastname values through request and add it in our user list. Also note how we called doGet() method inside doPost(). This is because we want to render same output, the user list on adding new user.

Add doPost() method to HelloServlet.

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
         throws ServletException, IOException {
     
     String firstname = request.getParameter( "firstname" );
     String lastname = request.getParameter( "lastname" );
     
     if ( null != firstname && null != lastname
             && !firstname.isEmpty() && !lastname.isEmpty()) {
         
         synchronized (userList) {
             userList.add( new User(firstname, lastname));
         }
         
     }
     
     doGet(request, response);
}

We are almost done. All we need is our freemarker template file. Create a file index.ftl under /WebContent/ folder. Copy following content into it.

File: /WebContent/index.ftl

< html >
< head >< title >ViralPatel.net - FreeMarker Hello World</ title >
 
< body >
   < form name = "user" action = "hello" method = "post" >
     Firstname: < input type = "text" name = "firstname" /> < br />
     Lastname: < input type = "text" name = "lastname" />       < br />
     < input type = "submit" value = "Save" />
   </ form >
 
   < table class = "datatable" >
     < tr >
         < th >Firstname</ th >  < th >Lastname</ th >
     </ tr >
     <#list users as user>
     < tr >
         < td >${user.firstname}</ td > < td >${user.lastname}</ td >
     </ tr >
     </#list>
   </ table >
</ body >
</ html >

For sake of simplicity, I have removed CSS styles from the above FTL file. You can download the full source code at the end of this tutorial.

Just compare your project structure with project structure image above. See if there are any differences.

That’s All Folks

You may want to run the application see the result. I assume you have already configured Tomcat in eclipse. All you need to do:
Open Server view from Windows > Show View > Server. Right click in this view and select New > Server and add your server details.

To run the project, right click on Project name from Project Explorer and select Run as > Run on Server (Shortcut: Alt+Shift+X, R)

freemarker-servlet-example-demo

Use the Add User form to add new user into below list.


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值