springmvc配置
2016-01-08 12:44
513 查看
引用博客:
http://www.xuebuyuan.com/1404908.html
/article/9443942.html
先注意以下几点问题:
(1)maven建立web项目出不来src/main/resources和src/main/test文件夹解决方法:
(2)maven的pom.xml更改了,最好update project
(3)tomcat对里面的加载顺序:Context-Param—->Listener—->filter—->servlet
(4)前面加了/的表示绝对路径,比如你是写的/app,那么访问的路径就是http://localhost:8080/app;前面不加/表示相对路径,比如你写app,那么访问的路径就是http://localhost:8080/……/app其中省略部分是你填写该路径所在文件夹的路径。
(5)最好在webapp根目录下建立一个resources文件夹管理静态(css/img/js)资源文件
(1) web.xml文件
(2)spring-servlet.xml文件
(3)spring-hibernate.xml文件
(6)将web.xml的头改为3.0,修改如下
(7)pom.xml加入一个插件,使maven项目更新时保持java版本为1.8而不是1.5,具体可以看下面pom.xml
web.xml
maven 不能设置为web3.0人解决方法:
首先在硬盘下找到 \项目名.setting\文件夹下的 org.eclipse.wst.common.project.facet.core.xml xml文件。
将jst.web,的version改成3.0.
然后在eclipse里右键该工程–maven–updateProject即可。
spring-servlet.xml
spring-hibernate.xml
实体测试类
控制器
业务逻辑接口
业务逻辑实现类
数据访问层没用接口
登录页面loginFrame.jsp
maven pom.xml文件
http://www.xuebuyuan.com/1404908.html
/article/9443942.html
先注意以下几点问题:
(1)maven建立web项目出不来src/main/resources和src/main/test文件夹解决方法:
在项目上右键选择properties,然后点击java build path,在Librarys下,编辑JRE System Library,选择workspace default jre就可以了。
(2)maven的pom.xml更改了,最好update project
(3)tomcat对里面的加载顺序:Context-Param—->Listener—->filter—->servlet
(4)前面加了/的表示绝对路径,比如你是写的/app,那么访问的路径就是http://localhost:8080/app;前面不加/表示相对路径,比如你写app,那么访问的路径就是http://localhost:8080/……/app其中省略部分是你填写该路径所在文件夹的路径。
(5)最好在webapp根目录下建立一个resources文件夹管理静态(css/img/js)资源文件
(1) web.xml文件
(2)spring-servlet.xml文件
(3)spring-hibernate.xml文件
(6)将web.xml的头改为3.0,修改如下
(7)pom.xml加入一个插件,使maven项目更新时保持java版本为1.8而不是1.5,具体可以看下面pom.xml
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>Archetype Created Web Application</display-name> <!-- 编码过滤器 --> <filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <async-supported>true</async-supported> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- Spring监听器 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 防止Spring内存溢出监听器 --> <listener> <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class> </listener> <!-- Spring MVC servlet --> <servlet> <servlet-name>spring</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <!-- 可以自定义servlet.xml配置文件的位置和名称,默认为WEB-INF目录下,名称为[<servlet-name>]-servlet.xml,如spring-servlet.xml <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring-servlet.xml</param-value> </init-param> --> <param-name>contextConfigLocation</param-name> <!--必须加*号,不然maven配置下找不到resources下的配置文件--> <param-value>classpath*:spring-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> <async-supported>true</async-supported> </servlet> <servlet-mapping> <servlet-name>spring</servlet-name> <!-- 此处可以可以配置成*.do,对应struts的后缀习惯,这里是对所有路径进行拦截 --> <url-pattern>/</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>/index.jsp</welcome-file> </welcome-file-list> <!-- 加载所有的配置文件 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:spring-*.xml</param-value> </context-param> <!-- 配置Session --> <filter> <filter-name>openSession</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> </filter> <filter-mapping> <filter-name>openSession</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
maven 不能设置为web3.0人解决方法:
首先在硬盘下找到 \项目名.setting\文件夹下的 org.eclipse.wst.common.project.facet.core.xml xml文件。
<?xml version="1.0" encoding="UTF-8"?> <faceted-project> <fixed facet="wst.jsdt.web"/> <installed facet="jst.web" version="2.3"/> <installed facet="wst.jsdt.web" version="1.0"/> <installed facet="java" version="1.7"/> </faceted-project>
将jst.web,的version改成3.0.
然后在eclipse里右键该工程–maven–updateProject即可。
spring-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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <!-- 注解扫描包 --> <context:component-scan base-package="com.xll" /> <!-- 开启注解 --> <mvc:annotation-driven /> <!-- 视图解析,控制器执行页面跳转,我的页面除了index.jsp,其他的页面都在WEB-INF下,所以需要通过访问控制器来执行页面跳转,而不能直接进行访问,会自动帮你加上前缀/WEB-INF/jsp/,和后缀.jsp --> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"></property> <property name="suffix" value=".jsp"></property> </bean> <!-- 完成请求和注解POJO的映射 --> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" /> <!-- 声明DispatcherServlet不要拦截下面声明的目录,因为我们上面配置时是拦截所有路径 --> <!-- 但是我们不希望静态资源(js/image/css)访问被拦截,因此要作如下配置,才能防止被DispatcherServlet拦截 --> <!--我的所有静态资源都在webapp/resources/下面--> <mvc:resources location="/resources/" mapping="/resources/**" /> </beans>
spring-hibernate.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" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <tx:annotation-driven transaction-manager="txManager" /> <!-- 配置数据源 --> <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:3306/website"></property> <property name="username" value="root"></property> <property name="password" value=""></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.xll.entity.Test</value> </list> </property> </bean> <!-- 配置一个事务管理器 --> <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <!-- 声明式容器事务管理 ,transaction-manager指定事务管理器为transactionManager --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="add*" propagation="REQUIRED" /> <tx:method name="get*" propagation="REQUIRED" /> <tx:method name="*" read-only="true" /> </tx:attributes> </tx:advice> </beans>
实体测试类
Test.java package com.xll.entity; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table(name = "test") public class Test { @Id @Column(length = 32) private String id; public String getId() { return id; } public void setId(String id) { this.id = id; } }
控制器
LoginController.java package com.xll.controller; import javax.annotation.Resource; import javax.servlet.http.HttpSession; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import com.xll.entity.User; import com.xll.service.UserService; @Controller @RequestMapping("login") public class LoginController { @Resource private UserService userService; //有接口写接口名 @RequestMapping(value = "loginFrame") public String loginFrame() { return "loginFrame"; } @RequestMapping(value = "login") //不是返回一个页面,需要加上@ResponseBody,@RequestParam传过来的参数接收 @ResponseBody public String login(@RequestParam String username, @RequestParam String password, HttpSession req) { User user = new User(); user.setUsername(username); user.setPassword(password); if (userService.checkUserLogin(user)) { //只返回一个字符串 return "success"; } else { //只返回一个字符串 return "fail"; } } }
业务逻辑接口
UserService.java package com.xll.service; import com.xll.entity.User; public interface UserService { boolean checkUserLogin(User user); }
业务逻辑实现类
package com.xll.impl; import javax.annotation.Resource; import org.springframework.stereotype.Service; import com.xll.dao.UserDao; import com.xll.entity.User; import com.xll.service.UserService; @Service public class UserServiceImp implements UserService { @Resource private UserDao userDao; @Override public boolean checkUserLogin(User user) { if (userDao.checkUserLogin(user)) { return true; } else return false; } }
数据访问层没用接口
package com.xll.dao; import javax.annotation.Resource; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.springframework.stereotype.Repository; import com.xll.entity.User; @Repository public class UserDao { @Resource private SessionFactory sessionFactory; public boolean checkUserLogin(User user) { Session session = sessionFactory.getCurrentSession(); //hibernae中不能写*,from 类名 User result = (User) session.createQuery("from User where username = ? and password = ?") .setString(0, user.getUsername()).setString(1, user.getPassword()).uniqueResult(); session.close(); if (result != null) return true; return false; } }
登录页面loginFrame.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" /> <title>登录界面</title> <link rel="stylesheet" type="text/css" href="../resources/css/login.css" /> </head> <body> <div class="login"> <div class="login-title">登录界面</div> <div class="login-main"> <label>博主名:</label><input class="uname" type="text" /><br /> <label>密码:</label><input class="pwd" type="text" /><br /> <input type="button" class="loginButton" value="登录" /> </div> </div> <div class="status"></div> <script src="../resources/js-extends/jquery.min.js"></script> <script type="text/javascript"> $(".loginButton").click( function() { $.ajax({ type : "POST", url : "login", data : "username=" + $(".uname").val() + "&password=" + $(".pwd").val(), success : callback或者function(result){ $(".status").html(result); } }); }); function callback(result) { //ajax请求刷新div,哪里改变刷新哪,什么都不写,页面保持不变 $(".status").html(result); } </script> </body> </html>
maven pom.xml文件
pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.xll</groupId> <artifactId>mavenproject</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>mavenproject Maven Webapp</name> <url>http://maven.apache.org</url> <build> <finalName>mavenproject</finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>2.4</version> <configuration> <failOnMissingWebXml>false</failOnMissingWebXml> </configuration> </plugin> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.2</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.38</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.2.4.RELEASE</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>4.2.15.Final</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.2.4.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>4.0.3.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>4.2.4.RELEASE</version> </dependency> </dependencies> </project>
<a href="login/loginFrame"></a>就可以直接访问控制器,进行页面跳转了!
相关文章推荐
- eclipse 中设置content assist 如何设置空格不上屏
- 第一章 Java常用的并发类
- Struts2 <s:doubleselect>级联下拉框 详解析
- Java新特性--可变参数
- Java多线程(一)Thread类与Runnable接口
- java 计算HH:mm:ss
- Java正确URL解码方式:URLDecoder.decode
- 使用gradle编译:重复jar包导致的错误Error:'...jdk1.8.0\bin\java.exe'' finished with non-zero exit value 2
- 浅析多租户在 Java 平台和某些 PaaS 上的实现
- 【第七章】 对JDBC的支持 之 7.1 概述 ——跟我学spring3
- Java Field[] Field理解
- spring注解(转载,自己学习)
- 尝试《Java Network Programming 4ed》的部分翻译
- 【第六章】 AOP 之 6.9 代理机制 ——跟我学spring3
- Java把Html格式文本转换成纯文本的问题
- java后台把数据类型装换为json类型(转载,自己学习)
- java 数组
- 【第六章】 AOP 之 6.8 切面实例化模型 ——跟我学spring3
- java.lang.IllegalStateException: The specified child already has a parent. You must call removeView(
- java 拦截器、过滤器、监听器 (转载,自己学习)