springmvc笔记(一)

    xiaoxiao2021-03-25  48

    MVC的思想是业务数据抽取同业务数据呈现分离。

    View是视图层,重点关注数据的呈现。

    Model 模型层,业务数据的信息表示,关注支撑业务信息的构成,通常是多个业务实体的组合。

    Controller 控制层,调用业务逻辑产生合适的数据。同时传递数据给视图层用于呈现。

     

    Springmvc提供了线程的实现,调用即可。

    DispatcherServelet是前端控制器

     

    HandlerAdapter相当于controller,DispatcherServelet调用各种HandlerAdapter来实现任务分发给相关的业务逻辑

     

    HandlerInterceptor是一个接口,可以用来在Handler调用之前,之后,以及view呈现后可以做很多事情

     

    HandlerMapping是负责确定DispatcherServelet与controller之间映射的类,告诉DispatcherServelet,在请求到来后,由哪个controller来响应这个请求

    Springmvc+hibernate+spring数据绑定笔记

    (1) 首先配置好jar包,放到lib下边。(注意:导出文件的时候类库是没有导出来的,因此建议把jar包添加好)。

    (1) 写好配置文件。

    springmvc-servlet.xml

    <?xml version="1.0"encoding="UTF-8"?>

    <beans xmlns="http://www.springframework.org/schema/beans"

        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

      xmlns:context="http://www.springframework.org/schema/context"

        xmlns:mvc="http://www.springframework.org/schema/mvc"

       xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd

            http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.1.xsd

           http://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">                   

     

      <!-- 激活标签,生命周期的管理-->

       <context:annotation-config/>

       <!-- dispatcherServlet去在这里搜索com.hmy.ssh.Controller包下边的东西控制器和请求联系-->

       <context:component-scan base-package="com.hmy.ssh.controller"/>

       <mvc:default-servlet-handler />

      

     <!-- 自动扫描 -->

       <context:component-scan base-package="com.hmy.ssh.service"></context:component-scan>

       <context:component-scan base-package="com.hmy.ssh.dao"></context:component-scan>

     <context:component-scan base-package="com.hmy.ssh.bean"></context:component-scan>

       <!-- 不处理这个cONTRoller

       <context:exclude-filter type="annotation"expression="org.springframework.Controller"/>

         -->

       <!-- 默认的注解映射的支持,扩充了注解驱动-->

       <mvc:annotation-driven />

       <!-- 视图解释类configure the InternalResourceViewResolver,告诉用哪个viewresolver获取view -->

       <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"

               id="internalResourceViewResolver">

            <!-- 前缀 ,jsp文件在当前根目录下。可指定路径-->

            <property name="prefix"value="/" />

            <!-- 后缀 -->

            <property name="suffix"value=".jsp" /><!--可为空,方便实现自已的依据扩展名来选择视图解释类的逻辑  -->

       </bean> 

       <!-- 可能这个标签的真谛就是为了引用资源的访问不会类似CONTROLLER一样被拦截,区分出关注的资源的访问,

       一般我们在springMVC里面的拦截都会配置为"/",拦截所有的。  -->

        <mvc:resources location="/images/" mapping="/images/**"/> 

        <mvc:resources location="/js/"mapping="/js/**"/>

        <mvc:resources location="/css/"mapping="/css/**"/>

        <mvc:resources location="/data/"mapping="/data/**"/> 

        

       <!-- 注解驱动,避免乱码 -->

       <mvc:annotation-driven>

          <mvc:message-converters>

                <bean class="org.springframework.http.converter.StringHttpMessageConverter">

                    <constructor-arg value="UTF-8"/>

                </bean>

            </mvc:message-converters>

       </mvc:annotation-driven>

      

       <!--  -->

       <!-- 文件上传解析器 -->

       <bean id="multipartResolver"

          class="org.springframework.web.multipart.commons.CommonsMultipartResolver">

          <!-- 支持的最大文件大小,以字节为单位 -->

          <property name="maxUploadSize" value="6291456" /> <!-- 6M -->

       </bean>

    </beans>

     

    <!-- 拦截器 --> 

    <!--

      <mvc:interceptors> 

       <bean class="com.hmy.ssh.MyInteceptor" /> 

    </mvc:interceptors>   -->

     

    (2) 写好web配置,添加以下代码:

     

    <?xml version="1.0"encoding="UTF-8"?>

    <web-app version="2.5"

       xmlns="http://java.sun.com/xml/ns/javaee"

       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

       xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

       http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

     <welcome-file-list>

       <welcome-file>index.jsp</welcome-file>

     </welcome-file-list>

     <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:springmvc-servlet.xml</param-value>

            </init-param>

            <!-- <load-on-startup>1</load-on-startup> -->

     </servlet>

     <servlet-mapping>

         <servlet-name>springmvc</servlet-name>

         <url-pattern>/</url-pattern>

     </servlet-mapping>

    </web-app>

     

    (3) 写好hibernate,以及实体类Users.

    package com.hmy.ssh.bean;

    import java.util.HashSet;

    import java.util.Set;

    /**

     * User entity.@author MyEclipse Persistence Tools

     */

    public class User implements java.io.Serializable {

     

           // Fields

           privateString userName;

           privateInteger userid;

           privateString userPwd;

           privateString userEmail;

           privateString userPhone;

           privateString userType;

           privateString trueName;

           privateInteger authority;

           privateSet homeWorks = new HashSet(0);

           privateSet comments = new HashSet(0);

           privateSet statisticses = new HashSet(0);

     

           //Constructors

     

           /**default constructor */

           publicUser() {

           }

     

           /**minimal constructor */

           publicUser(String userName, String userPwd, String userEmail,

                         StringuserPhone, String trueName) {

                  this.userName= userName;

                  this.userPwd= userPwd;

                  this.userEmail= userEmail;

                  this.userPhone= userPhone;

                  this.trueName= trueName;

           }

     

           /** fullconstructor */

           publicUser(String userName, Integer userid, String userPwd,

                         StringuserEmail, String userPhone, String userType,

                         StringtrueName, Integer authority, Set homeWorks, Set comments,

                         Setstatisticses) {

                  this.userName= userName;

                  this.userid= userid;

                  this.userPwd= userPwd;

                  this.userEmail= userEmail;

                  this.userPhone= userPhone;

                  this.userType= userType;

                  this.trueName= trueName;

                  this.authority= authority;

                  this.homeWorks= homeWorks;

                  this.comments= comments;

                  this.statisticses= statisticses;

           }

     

           //Property accessors

     

           publicString getUserName() {

                  returnthis.userName;

           }

     

           publicvoid setUserName(String userName) {

                  this.userName= userName;

           }

     

           publicInteger getUserid() {

                  returnthis.userid;

           }

     

           publicvoid setUserid(Integer userid) {

                  this.userid= userid;

           }

     

           publicString getUserPwd() {

                  returnthis.userPwd;

           }

     

           publicvoid setUserPwd(String userPwd) {

                  this.userPwd= userPwd;

           }

     

           publicString getUserEmail() {

                  returnthis.userEmail;

           }

     

           publicvoid setUserEmail(String userEmail) {

                  this.userEmail= userEmail;

           }

     

           publicString getUserPhone() {

                  returnthis.userPhone;

           }

     

           publicvoid setUserPhone(String userPhone) {

                  this.userPhone= userPhone;

           }

     

           publicString getUserType() {

                  returnthis.userType;

           }

     

           publicvoid setUserType(String userType) {

                  this.userType= userType;

           }

     

           publicString getTrueName() {

                  returnthis.trueName;

           }

     

           publicvoid setTrueName(String trueName) {

                  this.trueName= trueName;

           }

     

           publicInteger getAuthority() {

                  returnthis.authority;

           }

     

           publicvoid setAuthority(Integer authority) {

                  this.authority= authority;

           }

     

           public SetgetHomeWorks() {

                  returnthis.homeWorks;

           }

     

           publicvoid setHomeWorks(Set homeWorks) {

                  this.homeWorks= homeWorks;

           }

     

           public SetgetComments() {

                  returnthis.comments;

           }

     

           publicvoid setComments(Set comments) {

                  this.comments= comments;

           }

     

           public SetgetStatisticses() {

                  returnthis.statisticses;

           }

     

           publicvoid setStatisticses(Set statisticses) {

                  this.statisticses= statisticses;

           }

     

           @Override

           publicString toString() {

                  return"User [authority=" + authority + ", comments=" + comments

                                +", homeWorks=" + homeWorks + ", statisticses=" +statisticses

                                +", trueName=" + trueName + ", userEmail=" + userEmail

                                +", userName=" + userName + ", userPhone=" + userPhone

                                + ", userPwd=" +userPwd + ", userType=" + userType

                                +", userid=" + userid + "]";

           }

     

    }

     

    (4)    写好dao层和业务逻辑层,此处省去一部分。

    (5)写好controller层,并调用业务逻辑层的方法查询

    package com.hmy.ssh.controller;

    import java.util.ArrayList;

    import java.util.List;

    import org.springframework.beans.factory.annotation.Autowired;

    importorg.springframework.stereotype.Controller;

    importorg.springframework.web.bind.annotation.ModelAttribute;

    importorg.springframework.web.bind.annotation.RequestMapping;

    importorg.springframework.web.bind.annotation.RequestParam;

    importorg.springframework.web.bind.annotation.ResponseBody;

     

     

    import com.hmy.ssh.bean.User;

    importcom.hmy.ssh.controller.UserForm.UserListForm;

    import com.hmy.ssh.dao.daoImpl.*;

    import com.hmy.ssh.service.UserService;

    importcom.hmy.ssh.service.serviceImpl.UserServiceImpl;

     

    @Controller

    public class UserController {

       @Autowired

       privateUserService userService;

      

       //------------查询模块-----------------

     

      

       //http://localhost:8888/CourseOnline/selectOneUseByAuthority?authority=1   ,更改用户权限

       @ResponseBody

       @RequestMapping("/selectOneUseByAuthority")

       public  List<User>selectOneUseByAuthority(@RequestParam("authority") String authority){

              //1.根据权限,查询正在考试的用户

              System.out.println("权限:"+authority);

              Integerauthority1=Integer.parseInt(authority);

          List<User>user=userService.selectOneUseByAuthority(authority1);

              System.out.println(user);

              returnuser;//单个用户的信息

       }

    @ResponseBody

    //这里是返回给前端json,必须导入jason的jar包

       @RequestMapping("/test")

       //测试类

       public String test(){

          //UserService userService1 = new UserServiceImpl();

          List<User>userList = userService.selectUsers();

          System.out.println(userList.size());

          return "success";

    }

    }

    运行成功。打开网址

    转载请注明原文地址: https://ju.6miu.com/read-150314.html

    最新回复(0)