天天看点

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

如有问题欢迎私信,大家一起学习~