如何将Servlet代码转换为Spring Boot代码

时间:2017-05-17 10:16:41

标签: spring servlets spring-boot

下面是我的普通java servlet代码,它有Httpservlet请求和响应。现在如果我在spring boot中使用相同的代码,我会遇到一些错误。我不知道在春季启动时应该用这些请求和响应替换什么。

Servlet.java -

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

    variables.scrollflag = 1;
    if (request.getParameter("ins") != null) {
        variables.cruiseflag=0;
        variables.flag6 = 0;
        variables.flag9 = 0;
        variables.labelflag = 0;
        variables.displayflag=0;

        String n = request.getParameter("inserttextbox1");
        StringBuilder sb = new StringBuilder(n);
        for (int index = 0; index < sb.length(); index++) {
            char c = sb.charAt(index);
            if (Character.isLowerCase(c)) {
                sb.setCharAt(index, Character.toUpperCase(c));
            }
        }
        PrintWriter o = response.getWriter();
        o.print(sb.toString());
}

这是我的Spring Boot代码 - 控制器 -

@Controller
public class Scontroller {
 @Autowired  
    JdbcTemplate jdbc; 

@RequestMapping("/")
public String home(Map<String, Object> model) {
    //model.put("message", "HowToDoInJava Reader !!");
    return "searchpc";
}
@RequestMapping(value = "/bridgpc", method = RequestMethod.GET, params = 
{"insertpc"})
public String Controller(@RequestParam(value="insertpc", required = true, 
defaultValue = "klm") String argName) {
   jdbc.update("INSERT INTO PIUSER VALUES ((SELECT max(id) + 1 FROM 
PIUSER),'09koo','kl99i','kmko')");

return "insertpc";

}

Mainapplication -

 @SpringBootApplication
    public class kn extends SpringBootServletInitializer {
        @Bean
        public DataSource datasource(DataSource dataSource) {
        return datasource();
        }

        @Bean
        public JdbcTemplate jdbcTemplate(DataSource dataSource) {
        return new JdbcTemplate(dataSource);
        }
        @Override
        protected SpringApplicationBuilder 
configure(SpringApplicationBuilder application) {
            return application.sources(kn.class);
        }

        public static void main(String[] args) throws Exception {
            SpringApplication.run(kn.class, args);
        }



     }

1 个答案:

答案 0 :(得分:-2)

sample project带有弹簧靴和百里香视图covered in this guide可能会让你走上正轨。

编辑:正如评论链接中所指出的,只有答案不是很好,所以我总结了上述指南中的片段以使其正常工作。

控制器:

@Controller
public class GreetingController {

    @GetMapping("/greeting")
    public String greetingForm(Model model) {
        model.addAttribute("greeting", new Greeting());
        return "greeting";
    }

    @PostMapping("/greeting")
    public String greetingSubmit(@ModelAttribute Greeting greeting) {
        return "result";
    }

}

模特:

public class Greeting {

    private long id;
    private String content;

    //getters and setters omitted...

}

问候语视图:

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Getting Started: Handling Form Submission</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
    <h1>Form</h1>
    <form action="#" th:action="@{/greeting}" th:object="${greeting}" method="post">
        <p>Id: <input type="text" th:field="*{id}" /></p>
        <p>Message: <input type="text" th:field="*{content}" /></p>
        <p><input type="submit" value="Submit" /> <input type="reset" value="Reset" /></p>
    </form>
</body>
</html>

结果视图:

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Getting Started: Handling Form Submission</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
    <h1>Result</h1>
    <p th:text="'id: ' + ${greeting.id}" />
    <p th:text="'content: ' + ${greeting.content}" />
    <a href="/greeting">Submit another message</a>
</body>
</html>

所需的启动启动器是:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>