我是Spring + MVC的新手。 我找到了一个脚本,我可以运行这个脚本的一部分。 这个脚本配置了没有xml的spring mvc,在java端。 我将所有的罐子放入WEB-INF / lib。
ControllerConfiguration .java
package org.java.springmvc.bootstrap;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "org.java.springmvc.controller")
public class ControllerConfiguration {
@Bean
public InternalResourceViewResolver configureInternalResourceViewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".jsp");
return resolver;
}
}
WebAppInitializer.java
package org.java.springmvc.bootstrap;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration.Dynamic;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
public class WebAppInitializer implements WebApplicationInitializer {
@Override
public void onStartup(final ServletContext servletContext) throws ServletException {
final AnnotationConfigWebApplicationContext root = new AnnotationConfigWebApplicationContext();
root.setServletContext(servletContext);
root.scan("org.java.springmvc.bootstrap");
root.refresh();
final Dynamic servlet = servletContext.addServlet("spring", new DispatcherServlet(root));
servlet.setLoadOnStartup(1);
servlet.addMapping("/*");
}
}
HomeController.java
package org.java.springmvc.controller;
import java.io.IOException;
import java.io.Writer;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HomeController {
@RequestMapping(value = "/")
public void home(final Writer writer)
throws IOException {
writer.append("<h2>Welcome, XML Free Spring MVC!</h2>");
}
@RequestMapping(value = "/giris")
public void giris(final Writer writer)
throws IOException {
writer.append("Giris");
}
}
FilmController.java
package org.java.springmvc.controller;
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.java.springmvc.model.Film;
import org.java.springmvc.model.Film.FilmTurleri;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping("/film")
public class FilmController {
@RequestMapping(value = "filmler")
public void filmler(final Writer writer)
throws IOException {
writer.append("<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=ISO-8859-9\"><title>...Filmler...</title>");
writer.append("<script type=\"text/javascript\" src=\"/js/touch/sencha-touch-all.js\"></script>");
writer.append("<script type=\"text/javascript\" src=\"/js/film/filmler.js\"></script>");
writer.append("</head><body></body></html>");
}
@RequestMapping (value = "/filmleriGetir", method = RequestMethod.GET)
public @ResponseBody Map<String, List<Film>> FilmleriGetir() {
List<Film> movies = new ArrayList<Film>();
// For testing...
movies.add(new Film(0, "Birinci Film", "Birinci Yönetmen", 2015, FilmTurleri.Aksiyon));
movies.add(new Film(0, "İkinci Film", "İkinci Yönetmen", 2015, FilmTurleri.Komedi));
movies.add(new Film(0, "Üçüncü Film", "Üçüncü Yönetmen", 2015, FilmTurleri.Aile));
Map<String, List<Film>> resp = new HashMap<String, List<Film>>();
resp.put("filmListesi", movies);
return resp;
}
}
Film.java
package org.java.springmvc.model;
public class Film {
public int Id;
public String FilmAdi, Yonetmen;
public int CikisTarihi;
public FilmTurleri Turu;
public enum FilmTurleri {
Aksiyon, Komedi, Aile, Korku, Savas;
}
public Film(){
}
public Film(int id, String title, String director, int yearOfRelease, FilmTurleri tur)
{
super();
this.Id = id;
this.FilmAdi = title;
this.Yonetmen = director;
this.CikisTarihi = yearOfRelease;
this.Turu = tur;
}
//getter, settings method
}
我有两个问题:
“警告:在名为'spring'的DispatcherServlet中找不到带有URI [/SpringMVC/WEB-INF/views/index.jsp]的HTTP请求的映射”
警告:在DispatcherServlet中找不到名为“spring”的带有URI [/SpringMVC/Giris.jsp]的HTTP请求的映射
编辑: 我改变了一点。 我的项目结构如下:
我收到此错误:
无法加载资源:
http://localhost:8080/js/film/filmler.js
http://localhost:8080/js/touch/sencha-touch-all.js
我认为这样的逻辑:- there will be a jsp file including "*.js" files. (filmler.jsp)
- there are some methods returning json object in those *.js files. (FilmleriGetir method)
对此逻辑的任何建议?
问候。
答案 0 :(得分:0)
@RequestMapping("/movies")
servlet.addMapping("/*");
,这意味着您的org.springframework.web.servlet.DispatcherServlet
即Spring将拦截到您的应用程序的每个请求。现在,在任何控制器中都没有RequestMapping
Giris.jsp
,因此Spring会抛出错误:找不到带URI的HTTP请求的映射[/SpringMVC/Giris.jsp] 为了显示Giris.jsp页面,您需要:
A]使用RequestMapping
为'Giris.jsp'添加/生成新控制器,并将视图设置为'Giris'
例如:
@Controller
public class MyController {
@RequestMapping(value = "/Giris.jsp")
public void home(final Writer writer)
throws IOException {
return 'Giris';
}
}
您最好将RequestMapping用作/giris
而不是/Giris.jsp
,因为它暴露了底层技术是JSP。
B]将Giris.jsp文件放在/WEB-INF/views/
文件夹下。
了解InternalResourceViewResolver
的工作原理。引用您的ControllerConfiguration
,当控制器的视图名称返回为'Giris'时,InternalResourceViewResolver会添加您定义的前缀和后缀,并显示该页面。因此,如果视图名称为“Giris”,则会显示页面'/WEB-INF/views/'+ 'Giris' + '.jsp'
。
giris.jsp
代替Giris.jsp
编辑(修改后的问题):
了解,当DispatcherServlet
映射到/*
时,来自您的网络应用的每个请求都由DispatcherServlet
处理,即Spring
。
每当您的应用程序遇到网址http://localhost:8080/js/film/filmler.js时,它就知道DispatcherServlet
将处理该网址。 DispatcherServlet
检查网址是否有RequestMapping
(在控制器中)。
现在,添加网址
没有RequestMapping可以处理这种类型的URL,所以你得到一个网址。
要加载js文件或图像文件等资源,请使用mvc:resources
。
例如:
对于js文件:
将所有js文件放在目录/WEB-INF/js/
中。
在配置中为js文件添加mvc:resource mapping:
<mvc:resources mapping="/js/**" location="/WEB-INF/js/" />
现在您将能够访问您的js文件。如果Spring遇到了诸如/js/film/filmler.js
之类的URL,它将从mvc:resource mapping知道知道在哪里查找该文件。
Goof mvc:教程资源。