上面構建了兩篇寫了該有的方法 也就是說準備工作準備好了
下面我是將創建一個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 {
@Autowired
private UserServiceinter userService;
@Autowired
private RoleServiceinter roleService;
@Autowired
private 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 block
e.printStackTrace();
}
finally{
if(out != null) {
out.flush();
out.close();
}
}
} }
登錄頁面省略至此 框架算是搭建完畢了
細節方面以後會繼續發博客寫的
最後展示下使用這三大框架所做的頁面