首先检查并验证所有资源是否保留在正确的目录中。对于spring-boot,jsp页面应该放置在目录src->main->webapp->WEB-INF->view->*.jsp
中。
使用嵌入式tomcat编译jsp时,您应该在pom文件中添加以下依赖项:
<!
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<!
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
现在我们需要告诉Spring JSP文件的位置,有两种方法:
在application.properties中添加条目
spring.mvc.view.prefix=/WEB-INF/view/
spring.mvc.view.suffix=.jsp
在这里,我们可以指定jsp文件所在的文件夹,并定义视图。
配置InternalResourceViewResolver以提供JSP页面
@Configuration
@EnableWebMvc
@ComponentScan
public class MvcConfiguration extends WebMvcConfigurerAdapter
{
@Override
public void configureViewResolvers(ViewResolverRegistry registry) {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/view/");
resolver.setSuffix(".jsp");
resolver.setViewClass(JstlView.class);
registry.viewResolver(resolver);
}
}
现在,关于
注册servlets(小程序)的问题:
ServletRegistrationBean
用于在Servlet 3.0 +容器中注册Servlets。我们需要在JavaConfig中创建ServletRegistrationBean
的一个bean。以下是一些方法,可用于配置ServletRegistrationBean。
setServlet():设置要注册的servlet
addUrlMappings():为Servlet添加URL映射
setLoadOnStartup:设置以优先级加载Servlet启动。
假设我们有两个Servlets,分别是HelloCountryServlet
和HelloStateServlet
,然后我们将使用ServletRegistrationBean
在Spring Boot中将它们注册,如下所示:
@Configuration
public class WebConfig {
@Bean
public ServletRegistrationBean<HttpServlet> countryServlet() {
ServletRegistrationBean<HttpServlet> servRegBean = new ServletRegistrationBean<>();
servRegBean.setServlet(new HelloCountryServlet());
servRegBean.addUrlMappings("/country/*");
servRegBean.setLoadOnStartup(1);
return servRegBean;
}
@Bean
public ServletRegistrationBean<HttpServlet> stateServlet() {
ServletRegistrationBean<HttpServlet> servRegBean = new ServletRegistrationBean<>();
servRegBean.setServlet(new HelloStateServlet());
servRegBean.addUrlMappings("/state/*");
servRegBean.setLoadOnStartup(1);
return servRegBean;
}
}
对于每个Servlet,我们都需要创建一个
ServletRegistrationBean bean。
而servlet类看起来应该是这样的:
public class HelloCountryServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException{
doGet(request,response);
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<h3>Hello India!</h3>");
}
}
这是一种方法。
另一种方法在spring-boot中注册servlet的方式是使用注释:@ServletComponentScan和@WebServlet。Spring Boot中的@ServletComponentScan
将扫描用@WebServlet
注释的Servlet、用@WebFilter
注释的Filters以及用@WebListener
注释的Listeners。注释@ServletComponentScan在JavaConfig的类级别上使用。@ServletComponentScan仅在嵌入式web服务器中扫描Servlets、Filters和Listeners。查找带有@WebServlet注释的Servlets。
因此,之前的HelloCountryServlet Servlet可以提供如下:
@WebServlet(urlPatterns = "/country/*", loadOnStartup = 1)
public class HelloCountryServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException{
doGet(request,response);
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<h3>Hello India!</h3>");
}
}
并提供类似于ServletComponentScan注释的注释:
@ServletComponentScan
@SpringBootApplication
public class SpringBootAppStarter {
public static void main(String[] args) {
SpringApplication.run(SpringBootAppStarter.class, args);
}
}