天天看點

SSM轉springboot,使用springboot傳回ModelAndView配置設定

因為之前一直使用eclipse而且這個項目之前一直是SSM架構,現在因為公司要求使用了IDEA,順便就想把之前eclipse上的SSM項目放到IDEA上套上springboot跑起來,調試了一下午之後終于跑了起來

因為不再使用WebContent/WEB-INF/springmvc-config.xml這樣的配置檔案而是用springboot來完成通過maven管理的項目中用pom檔案中配置依賴關系。因為自己的項目是從eclipse上移植過來的是以路徑什麼的也都要修改。

接下來說一下我遇到的坑

一、首先不再有WebContent這樣的web項目的檔案夾以及web.xml是以我們需要在src下的main下建立webapp來存放這些靜态檔案包括html、jsp、js等。而且注意配置檔案的路徑都對不對

SSM轉springboot,使用springboot傳回ModelAndView配置設定

二、!!!!!pom.xml中的依賴配置,如果你要使用jsp那麼一定要加上以下依賴

<!--jsp視圖jar包-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- 傳回jsp頁面還需要這個依賴 -->
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <scope>provided</scope>
        </dependency>
           

三、最後就是你的springboot啟動器了,尤其是此處的掃描包的配置不要忘了,雖然我查了說

@SpringBootApplication

這個注解可以自動掃描同一層已經同一層的下一層但是我試了并不可以還是老老實實的配路徑

@MapperScan("com.包名.dao")

以下是我非常簡單的啟動器

package com.schelp;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.support.SpringBootServletInitializer;


// 是 Springboot 啟動類, 啟動内嵌 Tomcat 的應用程式,并給出Web通路的借口節目
@SpringBootApplication
//mapper包掃描
@MapperScan("com.schelp.dao")
public class SpringbootApplication extends SpringBootServletInitializer {


	// 啟動Springboot 容器
	public static void main(String[] args) {
		SpringApplication.run(SpringbootApplication.class, args);
	}
}
           

四、最後就是controller的配置了,既然說明了要使用jsp的modelandview那就不要傳json串了傳回視圖就好了

例如:

@Controller
public class UserController {
	
	@RequestMapping("/login")
	public ModelAndView login(){
		ModelAndView mv = new ModelAndView();
		mv.setViewName("login");
		System.out.println(">>> UserController - login!");
		return mv;
	}
	
	@RequestMapping("/register")
	public ModelAndView register(){
		ModelAndView mv = new ModelAndView();
		mv.setViewName("register");
		System.out.println(">>> UserController - register!");
		return mv;
	}
	
	@Autowired
	UserService userService;
	@Autowired
	ProductService productService;
	
	@RequestMapping("/userLogin")
	public ModelAndView userLogin(String username,String password,HttpSession session) {
		ModelAndView mv = new ModelAndView();
		List<User> users = userService.userLogin(username,password);
		if (users.size()>0) {
			session.setAttribute("user", users.get(0));
			
			mv.setViewName("redirect:/index.jsp");
			System.out.println("UserController -- 登入成功!");
			return mv;

		} else {
			mv.addObject("msg","賬号或密碼錯誤!");
			mv.setViewName("login");
			System.out.println("UserController -- 登入失敗!");
			return mv;
		}
	}
	
	@RequestMapping("/userRegister")
	public ModelAndView userRegister(String username,String password,String phonenumber,String email){
		ModelAndView mv = new ModelAndView();
		
		int user = userService.userRegister(username, password, phonenumber, email);
		System.out.println("UserController -- userRegister -- "+user);
		
		mv.addObject("msg", "注冊成功!");
		mv.setViewName("login");
		return mv;
	}
	
}
           

如有問題歡迎私信,大家一起學習~