学习整合hibernate springmvc spring的 心得(3)

来源:互联网 发布:左下角windows 编辑:程序博客网 时间:2024/09/21 06:42

上面构建了两篇写了该有的方法 也就是说准备工作准备好了

下面我是将创建一个jsp文件夹 ,将他放入web-inf下 这意味着jsp页面都需要控制器跳转才能访问

很有必要先展示下我的文件结构


下面是web.xml的代码

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">  <display-name>SSH_USER</display-name>  <!-- opsessioninview -->  <filter>    <filter-name>openSessionInview</filter-name>    <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter </filter-class>    <init-param>      <param-name>sessionFactoryBeanName</param-name>      <param-value> sessionFactory</param-value>    </init-param>    <init-param>       <param-name>singleSession</param-name>      <param-value>true</param-value>    </init-param>  </filter>  <filter-mapping>    <filter-name>openSessionInview</filter-name>    <url-pattern>/*</url-pattern>  </filter-mapping>  <!-- spring -->  <listener>    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  </listener>  <context-param>    <param-name>contextConfigLocation</param-name>    <param-value>classpath:applicationContext.xml</param-value>  </context-param>    <!-- 配置springmvc -->  <servlet>    <servlet-name>springMvc</servlet-name>    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>    <init-param>      <param-name>contextConfigLocation</param-name>      <param-value>/WEB-INF/spring-mvc/*.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>      <!-- 解决乱码 -->  <filter>    <filter-name>encodingFilter</filter-name>    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>    <init-param>      <param-name>encoding</param-name>      <param-value>UTF-8</param-value>    </init-param>    <init-param>      <param-name>forceEncoding</param-name>      <param-value>true</param-value>    </init-param>  </filter>  <filter-mapping>    <filter-name>encodingFilter</filter-name>    <url-pattern>/*</url-pattern>  </filter-mapping>  <!-- 验证用户是否登录 -->  <filter>    <filter-name>LoginFilter</filter-name>    <filter-class>com.caicai.interceptor.MyLoginFilter</filter-class>  </filter>  <filter-mapping>    <filter-name>LoginFilter</filter-name>    <url-pattern>/*</url-pattern>  </filter-mapping>      <error-page>   <error-code>404</error-code>    <location>/error2.jsp</location></error-page></web-app>

以下是springmvc的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:mvc="http://www.springframework.org/schema/mvc"       xmlns:p="http://www.springframework.org/schema/p"       xmlns:context="http://www.springframework.org/schema/context"       xmlns:aop="http://www.springframework.org/schema/aop"       xmlns:tx="http://www.springframework.org/schema/tx"       xsi:schemaLocation="http://www.springframework.org/schema/beans            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd            http://www.springframework.org/schema/context             http://www.springframework.org/schema/context/spring-context-3.0.xsd            http://www.springframework.org/schema/aop             http://www.springframework.org/schema/aop/spring-aop-3.0.xsd            http://www.springframework.org/schema/tx             http://www.springframework.org/schema/tx/spring-tx-3.0.xsd            http://www.springframework.org/schema/mvc             http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd            http://www.springframework.org/schema/context             http://www.springframework.org/schema/context/spring-context-3.0.xsd"><!-- 启用注解 --> <mvc:annotation-driven/>          <!--使Spring支持自动检测组件,如注解的Controller--><context:component-scan base-package="com.minx.crm.web.controller"/><!-- 扫描注解包 扫描controller--><context:component-scan base-package="com.caicai.web.controller"/><!-- 配置视图解析器 --><bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">          <!-- 前缀 -->          <property name="prefix" value="/WEB-INF/jsp/"></property>          <!--<property name="prefix" value="/"></property>-->          <!-- 后缀 -->          <property name="suffix" value=".jsp"></property> </bean>  <!-- 配置文件解析器 --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">       <property name="defaultEncoding" value="UTF-8"></property>       <property name="maxUploadSize" value="10485760000"></property>       <property name="maxInMemorySize" value="40960"></property> </bean>   <!-- 这个请求不过滤 比如静态资源-->     <mvc:resources location="/Images/" mapping="/Images/**"/>   <mvc:resources location="/bootstrap/" mapping="/bootstrap/**"/> <mvc:resources location="/css/" mapping="/css/**"/> <mvc:resources location="/iconfont/" mapping="/iconfont/**"/>  <!-- 国际化 --> <bean id="messageSource"    class="org.springframework.context.support.ResourceBundleMessageSource"    p:basename="message"></bean> <!-- 拦截器 --><mvc:interceptors>    <mvc:interceptor>        <mvc:mapping path="/User/findbypage" />        <mvc:mapping path="/User/beforeupdate" />        <mvc:mapping path="/User/update" />        <mvc:mapping path="/User/delete" />        <mvc:mapping path="/User/photoupload" />        <bean class="com.caicai.interceptor.MyInteceptorfindbypage" />    </mvc:interceptor> </mvc:interceptors></beans>
至此配置算是完事了

然后可以写控制器了

这里就写登录为例子

package com.caicai.web.controller;import java.io.File;import java.io.IOException;import java.io.PrintWriter;import java.util.Date;import java.util.List;import java.util.Set;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.commons.io.FileUtils;import org.hibernate.Hibernate;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.multipart.MultipartFile;import com.alibaba.fastjson.JSONArray;import com.alibaba.fastjson.JSONObject;import com.caicai.entity.Authority;import com.caicai.entity.Group;import com.caicai.entity.Information;import com.caicai.entity.Role;import com.caicai.entity.User;import com.caicai.service.inter.GroupServiceinter;import com.caicai.service.inter.RoleServiceinter;import com.caicai.service.inter.UserServiceinter;@Controller@RequestMapping("/User")public class UserController {@Autowiredprivate UserServiceinter userService;@Autowiredprivate RoleServiceinter roleService;@Autowiredprivate GroupServiceinter groupService; @RequestMapping("/logincheck") public void logincheck(User u,String code,HttpServletRequest request,HttpServletResponse response)  {    //String path=request.getSession().getServletContext().getRealPath("/Images/lin.gif"); //System.out.println("path"+path); User ru=this.userService.Userlogin(u); String randStr=(String) request.getSession().getAttribute("randStr"); String json=""; if(ru==null||!code.equals(randStr)) { json="登录失败"; } else { json="登录成功"; Hibernate.initialize(ru.getUrole().getAuthoritys()); Hibernate.initialize(ru.getUgroup()); request.getSession().setAttribute("user", ru); //对权限初始化 取消懒加载,关联的关联 我也不知道为什么opensessioninview不好用     //request.getSession().setAttribute("As", ru.getUrole().getAuthoritys());/*Set<Authority> list=(Set<Authority>)ru.getUrole().getAuthoritys();   System.out.println("listsize"+list.size());*/  } response.setContentType("application/json; charset=utf-8");   JSONObject jo=new JSONObject(); jo.put("json", json); PrintWriter out = null;   try { out = response.getWriter(); out.write(jo.toString());} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}   finally{ if(out != null) {      out.flush();            out.close();      }  } } }
登录页面省略至此 框架算是搭建完毕了

细节方面以后会继续发博客写的 

最后展示下使用这三大框架所做的页面






0 0