<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>LCore</display-name>
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
<!-- 加载所有的配置文件
这里我将配置文件置于源码包中
-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:config/spring/spring-*.xml</param-value>
</context-param>
<!-- 配置Spring-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 配置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>classpath*:config/spring/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>
<!-- 配置Session -->
<filter>
<filter-name>openSession</filter-name>
<filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>openSession</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app><?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/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
<!-- 注解扫描包 -->
<context:component-scan base-package="com.lcore.ctp.mng" />
<!-- 开启注解 -->
<!-- 启动spring mvc的注解功能,完成请求和注解POJO的映射 -->
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<!-- 配置信息转换,将用@responsebody注解的返回值转换为json返回前台,编码为utf-8-->
<property name="messageConverters">
<list>
</list>
</property>
</bean>
<mvc:interceptors>
<mvc:interceptor>
<!-- 需拦截的地址 -->
<!-- 一级目录 -->
<mvc:mapping path="/*" />
<mvc:mapping path="/*.do" />
<mvc:mapping path="/*.ajax" />
<mvc:mapping path="/*.htm" />
<!-- 二级目录 -->
<mvc:mapping path="/*/*" />
<mvc:mapping path="/*/*.do" />
<mvc:mapping path="/*/*.ajax" />
<mvc:mapping path="/*/*.htm" />
<!-- 需排除拦截的地址 -->
<mvc:exclude-mapping path="/login" />
<bean class="com.lcore.ctp.mng.Interceptor.SecurityInterceptor" />
</mvc:interceptor>
</mvc:interceptors>
<mvc:annotation-driven />
<!-- 静态资源(js/image)的访问 -->
<mvc:resources mapping="/res/**" location="/res/" />
<!-- 定义视图解析器 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/admin/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
上述的配置中笔者加了拦截器的配置,避免尚未登录的用户直接访问其他页面。<?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/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- 配置数据源 --> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" > <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> <property name="url" value="jdbc:mysql://localhost/ScoreMN"></property> <property name="username" value="root"></property> <property name="password" value="root"></property> </bean> <!-- 配置SessionFactory --> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> <!-- <prop key="hibernate.hbm2ddl.auto">update</prop> --> <!-- <prop key="hibernate.show_sql">true</prop> --> <!-- <prop key="hibernate.format_sql">true</prop> --> </props> </property> <property name="annotatedClasses"> <list> <value>com.lcore.ctp.mng.domain.User</value> </list> </property> </bean> <!-- 配置一个事务管理器 --> <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"/> </bean> <!-- 配置事务,使用代理的方式 --> <bean id="transactionProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" abstract="true"> <property name="transactionManager" ref="transactionManager"></property> <property name="transactionAttributes"> <props> <prop key="add*">PROPAGATION_REQUIRED,-Exception</prop> <prop key="modify*">PROPAGATION_REQUIRED,-myException</prop> <prop key="del*">PROPAGATION_REQUIRED</prop> <prop key="*">PROPAGATION_REQUIRED</prop> </props> </property> </bean> </beans>4、配置spring-beans.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/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="userDao" class="com.lcore.ctp.mng.dao.UserDaoImpl"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <bean id="userServiceBase" class="com.lcore.ctp.mng.service.UserServiceImpl"> <property name="userDao" ref="userDao"></property> </bean> <!-- 此处为代理 --> <bean name="userService" parent="transactionProxy"> <property name="target" ref="userServiceBase"></property> </bean> </beans>
package com.lcore.ctp.mng.domain;
import java.io.Serializable;
import java.util.Date;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.Table;
import org.hibernate.annotations.GenericGenerator;
@Entity
@Table(name = "user")
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public class User implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private String id;
private String name;
private String password;
@Id
@GeneratedValue(generator = "systemUUid")
@GenericGenerator(name = "systemUUid", strategy = "uuid")
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}package com.lcore.ctp.mng.controller.login;
import java.io.IOException;
import java.io.PrintWriter;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.lcore.ctp.mng.controller.BaseController;
import com.lcore.ctp.mng.domain.User;
import com.lcore.ctp.mng.service.UserService;
@Controller
public class UserController extends BaseController{
@Resource(name="userService")
private UserService userService;
@RequestMapping("/login")//Controller访问url
public String login(User user,HttpServletRequest request)
{
user.setPassword(request.getParameter("password"));
user.setName(request.getParameter("username"));
System.out.println(user.getName()+user.getPassword());
if(userService.login(user)){
request.getSession().setAttribute("user", user);
return "redirect:index";
}
else
return "/login";//登录失败则返回login.jsp界面
}
@RequestMapping("/loginOut")
public String loginOut(User user,HttpServletRequest request)
{
return "login";
}
}action="<%=request.getContextPath()%>/login"至此一个简单的Spring MVC工程就完成了,可以看到的是对于Dao层,Servcie层这里我们仍使用的是用XML文件进行配置的,感觉十分的不灵活,实际上也可以使用注解的方式来实现,不过这里就不去具体实现了。
原文:http://blog.csdn.net/lcore/article/details/23133579