美文网首页
SpringMVC-入门

SpringMVC-入门

作者: Dane_404 | 来源:发表于2019-09-25 14:47 被阅读0次

    Hello SpringMVC

    引用

    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>4.0.1</version>
        <scope>provided</scope>
    </dependency>
    
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>5.1.8.RELEASE</version>
    </dependency>
    

    HelloController:写类实现Controller,重写handleRequest

    public class HelloController implements Controller {
    
    
        @Override
        public ModelAndView handleRequest(HttpServletRequest httpServletRequest,
                                          HttpServletResponse httpServletResponse) throws Exception {
    
    
            ModelAndView modelAndView = new ModelAndView();
            //相当于setAttribute
            modelAndView.addObject("name","dane");
            //返回的jsp界面
            modelAndView.setViewName("/index");
            return modelAndView;
        }
    }
    

    spring.xml中配置

    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    
        <bean name="/hello" class="com.dane.springmvc.controller.HomeController"/>
    
    </beans>
    

    jsp页面

    <%@ page contentType="text/html;charset=utf-8" language="java"
             isELIgnored="false" %>
    <html>
    <body>
    <h2>Hello World! ${name}</h2>
    </body>
    </html>
    

    编写web.xml

    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>classpath:spring.xml</param-value>
        </init-param>
    </servlet>
    
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    

    如果jsp放在WEB-INF里,需要在spring.xml中配置,添加了后缀suffix,ModelAndView setViewName就不需要xxx.jsp了,xxx即可

    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    
        <bean name="/hello" class="com.dane.springmvc.controller.HelloController"/>
    
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/jsps/"></property>
            <property name="suffix" value=".jsp"></property>
        </bean>
    </beans>
    

    SpringMVC注解形式开发

    使用@Controller和 @RequestMapping

    @Controller
    public class HelloController {
    
        @RequestMapping("test")
        public ModelAndView test1(){
            ModelAndView modelAndView = new ModelAndView();
            //相当于setAttribute
            modelAndView.addObject("name","dane");
            //返回的jsp界面
            modelAndView.setViewName("index");
            return modelAndView;
        }
    
    }
    

    编写配置文件:

    <context:component-scan base-package="com.dane.springmvc.controller"/>
    <mvc:annotation-driven/>
    
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsps/"></property>
        <property name="suffix" value=".jsp"></property>
        <property name="order" value="10"></property>
    </bean>
    

    @RequestMapping可以用于类上,例如:这样访问路径就是.../test/test1

    @Controller
    @RequestMapping(value = "/test", method = RequestMethod.GET)
    public class HelloController {
    
        @RequestMapping("/test1")
        public ModelAndView test1(){
            ModelAndView modelAndView = new ModelAndView();
            //相当于setAttribute
            modelAndView.addObject("name","dane");
            //返回的jsp界面
            modelAndView.setViewName("index");
            return modelAndView;
        }
    
    }
    

    @ModelAttribute

    @ModelAttribute注解的方法将在这个Controller每次处理请求前被调用,可以用于权限控制等。

    @ModelAttribute
    public  void isLogin(HttpSession session){
            
    }
    

    Controller返回类型

    string类型,表示返回某个jsp页面

    @Controller
    @RequestMapping("/user")
    public class UserController {
    
    
        @RequestMapping("/tologin")
        public String login() {
            
            return "login";
        }
    }
    

    也可以进行重定向和转发处理,重定向:redirect:login,转发:forward:login

    json类型

    引入jackson
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>2.9.9.3</version>
    </dependency>
    
    使用@ResponseBody
    public class JsonResult<T> {
    
        private int errorCode;
    
        private  String message;
    
        private T data;
    }
    
    @CrossOrigin  //解决跨越问题
    @Controller
    @RequestMapping("/user")
    public class UserController {
    
        @Autowired
        UserService userService;
    
        @RequestMapping(value = "/login",method = RequestMethod.POST)
        @ResponseBody
        public JsonResult login(@RequestBody User user) throws Exception {
    
            JsonResult jsonResult = userService.login(user);
    
            return jsonResult;
    
        }  
    }
    
    使用@ExceptionHandler统一处理异常
    @RestControllerAdvice
    public class ExceptionHandler {
    
        private static final Logger log = LoggerFactory.getLogger(SSMExceptionHandler.class);
    
        @ExceptionHandler(BaseException.class)
        public JsonResult unknownException(BaseException e) {
    
            JsonResult jsonResult=new JsonResult();
            jsonResult.setErrorCode(e.getErrorCode());
            jsonResult.setMessage(e.getMessage());
            return jsonResult;
        }
    }
    

    Controller接收请求参数

    通过形式参数接收

    请求上来的参数要和行参完全相同:

    @RequestMapping("/login")
    public String login(String account,String password) {
    
        if("test".equals(account) && "123456".equals(password)){
            return "main";
        }
    
        return "login";
    }
    

    其中,形参可以添加@RequestParam,没有加和有加的区别是:通过@RequestParam接收的请求参数,若参数名错误时,会报404错误,没有使用@RequestParam,则不会报404。

    @RequestMapping("/login")
    public String login(@RequestParam String account,@RequestParam String password) {
    
        if("test".equals(account) && "123456".equals(password)){
            return "main";
        }
    
        return "login";
    }
    

    通过bean参数接收

    请求上来的参数要和bean的属性名完全相同:

    public class User{
        private String account;
        private String password;
    }
    
    @RequestMapping("/login")
    public String login(User user) {
    
        if("test".equals(user.account) && "123456".equals(user.password)){
            return "main";
        }
    
        return "login";
    }
    

    通过@PathVariable接收URL中的请求参数

    注意必须加method属性

    @RequestMapping(value = "/register/{account}/{password}",method = RequestMethod.GET)
    public String register(@PathVariable String account, @PathVariable String password) {
    
        if("test".equals(account) && "123456".equals(password)){
            return "login";
        }
    
        return "register";
    }
    

    拦截器

    SpringMVC定义拦截器有两种方式,一种是实现HandlerInterceptor,另一种是实现WebRequestInterceptor。

    实现HandlerInterceptor

    public class TestInterceptor implements HandlerInterceptor {
    
        @Override
        public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) 
                throws Exception {
            
            //该方法在Controller的处理请求方法前执行,其返回值表示是否中断后续操作
            //true表示继续向下执行,false表示中断后续操作
            
            return true;
        }
    
    
        @Override
        public void postHandle(HttpServletRequest request, HttpServletResponse response,
                               Object handler, ModelAndView modelAndView) 
                throws Exception {
            
            //该方法在Controller的处理请求方法之后,解析视图之前执行,可以通过此方法对请求域中
            //的模型和视图进行修改
            
        }
    
        @Override
        public void afterCompletion(HttpServletRequest request, HttpServletResponse response, 
                                    Object handler, Exception ex) 
                throws Exception {
    
            //该方法在Controller的处理请求方法之后,解析视图之后执行
            
        }
    }
    

    配置拦截器:

    <mvc:interceptors>
        <!--全局拦截器-->
        <bean class="com.dane.springmvc.interceptor.TestInterceptor"/>
    
        <!--单独配置特定的拦截器-->
        <mvc:interceptor>
            <!--拦截的路径 /**表示所有-->
            <mvc:mapping path="/**"/>
            <!--不拦截的路径 /**表示所有-->
            <mvc:exclude-mapping path="/"/>
            <bean class="com.dane.springmvc.interceptor.TestInterceptor"/>
        </mvc:interceptor>
    </mvc:interceptors>
    

    国际化

    Java的国际化

    在resource下新建xxxx_zh_CN.properties,中文要转成Unicode:
    hello=hello
    hi=hi,{0}
    
    然后可以这样测试:
    //获取系统默认的国家语言环境
    Locale locale = Locale.getDefault();
    
    ResourceBundle resourceBundle 
      = ResourceBundle.getBundle("messageResource",locale);
    
    System.out.println(resourceBundle.getString("hello"));
    
    String hi = resourceBundle.getString("hi");
    hi = MessageFormat.format(hi,"Dane");
    System.out.println(hi);
    
    输出:
    hello
    hi,Dane
    

    SpringMVC国际化

    配置资源路径
    <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basename" value="message.messageResource"/>
    </bean>
    
    语言选择

    AcceptHeaderLocaleResolver
    根据浏览器的Http Header中的accept-language域设定的语言。

    <bean id="localResolver" class="org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver">
        <property name="defaultLocale" value="zh_CN"/>
    </bean>
    

    SessionLocaleResolver和CookieLocaleResolver
    SessionLocaleResolver根据用户每次会话过程中的语言设定。
    CookieLocaleResolver根据Cookie判断用户的语言。

    <bean id="localResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
        <property name="defaultLocale" value="zh_CN"/>
    </bean>
    <!--<bean id="localResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver">-->
        <!--<property name="defaultLocale" value="zh_CN"/>-->
    <!--</bean>-->
    <!--SessionLocaleResolver 和 CookieLocaleResolver 必须配-->
    <mvc:interceptors>
       <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"/>
    </mvc:interceptors>
    

    文件上传、下载

    上传

    引入jar
    <dependency>
      <groupId>commons-fileupload</groupId>
      <artifactId>commons-fileupload</artifactId>
      <version>1.4</version>
    </dependency>
    
    配置文件

    其中,p:uploadTempDir="file:temp"是resources下的temp目录

    <bean id="multipartResolver"
      class="org.springframework.web.multipart.commons.CommonsMultipartResolver"
      p:defaultEncoding="UTF-8" p:maxUploadSize="5400000" p:uploadTempDir="file:temp"/>
    
    最后Controller
    @CrossOrigin
    @Controller
    public class FileController {
        
        @RequestMapping(value = "/onefile",method = RequestMethod.POST)
        @ResponseBody
        public JsonResult onefile(UploadFile file, HttpServletRequest request) throws Exception {
    
            JsonResult jsonResult = new JsonResult();
    
            String realPath = request.getServletContext().getRealPath("uploadfils");
            String filename = file.getMyFile().getOriginalFilename();
            File targetFile = new File(realPath,filename);
            if (!targetFile.exists()){
                targetFile.mkdirs();
            }
    
            file.getMyFile().transferTo(targetFile);
            jsonResult.setErrorCode(0);
    
            return jsonResult;
    
        }
    }
    

    下载

     @RequestMapping(value = "/down",method = RequestMethod.GET)
        public String down(String filename,  HttpServletRequest request,
                           HttpServletResponse response) throws Exception {
    
            String realPath = request.getServletContext().getRealPath("uploadfils");
            response.setHeader("Content-Type","application/x-msdownload");
            response.setHeader("Content-Disposition","attachment;filename="+filename);
    
            FileInputStream in = new FileInputStream(realPath + "/" + filename);
            ServletOutputStream out = response.getOutputStream();
            out.flush();
            int read = 0;
            byte[] b = new byte[1024];
            while (in!=null && (read = in.read(b)) != -1){
                out.write(b,0,read);
            }
            out.flush();
            in.close();
            out.close();
    
            return  null;
    
        }
    

    相关文章

      网友评论

          本文标题:SpringMVC-入门

          本文链接:https://www.haomeiwen.com/subject/wtfksctx.html