使用Spring Boot创建第二个servlet

时间:2016-04-05 08:36:41

标签: spring spring-boot

我有一个在Spring启动时开发的应用程序,我有以下Application类,我在其中注册FacesServlet。直到现在都很好,但我需要第二个servlet。

我无法注册并设置这个新servlet的映射。

我一遍又一遍地搜索帖子,但我没有发现任何帮助。

这是我的Application类,我在其中注册了第二个servlet,Login servlet:

@Configuration
@EnableAutoConfiguration
@ComponentScan(basePackages = {"at.bdt"})
@EnableJpaRepositories(basePackages = {"at.bdt.persist.repository"})
@EntityScan(basePackages = {"at.bdt.persist.model"}) 
public class Application extends SpringBootServletInitializer implements ServletContextAware
{
    public static void main(String[] args)
    {
        SpringApplication.run(Application.class, args);
    }

    @Bean
    public ServletRegistrationBean facesServlet()
    {
        FacesServlet servlet = new FacesServlet();
        ServletRegistrationBean servletRegistrationBean = new  ServletRegistrationBean(servlet, "*.xhtml");
        servletRegistrationBean.setLoadOnStartup(1);
        return servletRegistrationBean;
    }

    @Bean
    public ServletListenerRegistrationBean<ConfigureListener> jsfConfigureListener()
    {
        return new ServletListenerRegistrationBean<ConfigureListener>(new ConfigureListener());
    }

    @Bean
    public ServletRegistrationBean servletRegistrationBean(){
        LoginServlet lg=new LoginServlet();
        ServletRegistrationBean servletRegistrationBeanLog = new ServletRegistrationBean(lg, "/LoginServlet");
    //  servletRegistrationBeanLog.addUrlMappings("/LoginServlet");
        servletRegistrationBeanLog.setLoadOnStartup(1);
        return servletRegistrationBeanLog;
    }


/*  @Bean
    public MessageSource message()
    {
        ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
        messageSource.setBasename("messages");
        return messageSource;
    }*/
    @Bean
    public CustomScopeConfigurer registerViewScope()
    {
        CustomScopeConfigurer configurer = new CustomScopeConfigurer();
        Map<String, Object> scopes = new HashMap<String, Object>();
        scopes.put("view", new ViewScope());
        configurer.setScopes(scopes);
        return configurer;
    }
    @Override
    public void setServletContext(ServletContext sc)
    {
    }
}"

这是我的servlet:

@WebServlet(
        description = "Login Servlet", 
        urlPatterns = { "/LoginServlet" })
public class LoginServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;


    public void init() throws ServletException {

    }


    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //get request parameters for userID and password
        String user = request.getParameter("user");
        String pwd = request.getParameter("pwd");


            response.sendRedirect("LoginSuccess.jsp");
       }

    }

}

servlet仅用于测试目的。我只是想看看

的login.html:

<!DOCTYPE html>
<html>
<head>
<meta charset="US-ASCII">
<title>Login Page</title>
</head>
<body>

<form action="LoginServlet" method="post">

Username: <input type="text" name="user">
<br>
Password: <input type="password" name="pwd">
<br>
<input type="submit" value="Login">
</form>
</body>
</html>

LoginSuccess.jsp:

<%@ page language="java" contentType="text/html; charset=US-ASCII"
    pageEncoding="US-ASCII"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Login Success Page</title>
</head>
<body>
<h3>Hi Doris, Login successful.</h3>
<a href="login.html">Login Page</a>
</body>
</html>

我没有使用web.xml文件,我使用spring boot来注册servlet。 当我访问http://localhost:8080/web/views/LoginServlet/login.html

我收到以下错误:

This application has no explicit mapping for /error, so you are seeing this as a fallback.

Tue Apr 05 09:09:23 CEST 2016
There was an unexpected error (type=Not Found, status=404).
No message available

我已尝试将其作为:http://localhost:8080/web/views/LoginServlet访问 没有成功。

我只想注册第二个servlet并使其正常运行,但我无法通过spring boot来管理它。 我无法弄清楚如何设置网址映射。 任何帮助将不胜感激。

0 个答案:

没有答案