2017-05-17 57 views
-3

下面是我的普通java servlet代碼,它具有Httpservlet請求和響應。現在,如果我在spring引導中使用相同的代碼,我會遇到一些錯誤。我不知道在春季啓動時應該如何替換這些請求和響應。如何將Servlet代碼轉換爲Spring引導代碼

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()); 
} 

這是我的春節,引導代碼 - 控制器 -

@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); 
     } 



    } 
+0

請張貼代碼你如何試圖在servlet轉換爲春季啓動。 –

回答

-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> 
+0

這就是對,但@RestController不能用於顯示視圖。即使我在我的網頁中有很多文本框,所以我認爲RequestParam不可行使用 –

+0

當問題太不清楚時,很難回答有用的問題。你說你有很多文本框,你看過百里香的表單綁定嗎? –

+1

請[僅限鏈接回答](http://meta.stackoverflow.com/tags/link-only-answers/info)。答案是「幾乎不超過一個鏈接到外部網站」[可能會被刪除](http://stackoverflow.com/help/deleted-answers)。 – Quentin