因为项目现在每天需要统计网站登录 注册的数量变化,领导让我做一个统计的系统。我对于功能强大性能稳定的spring框架一直仰望,很早就想试一试。就捉摸着这个项目使用spring框架。在搭建的过程中遇到了一些问题,现在记录一下:我用的ide是intellij idea.idea在建立工程时有建立spring工程的选项,选spring mvc,它就会自动帮你下载spring 所需的jar包,所以还是很方便的。
当项目建好以后,你下一步要做的就是配置spring
1.web.xml配置
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> </web-app> 这里是默认配置,要注意的地方在 servlet-name 这个名称和XX-servlet.xml里的XX是一样的 2.servlet-name-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:p="http://www.springframework.org/schema/p" 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.xsd"> <!-- 启用spring mvc注解 --> <context:annotation-config></context:annotation-config> <!-- 扫描包 --> <context:component-scan base-package="com"></context:component-scan> <!-- 对转向页面的路径解析。prefix:前缀, suffix:后缀 如:http://127.0.0.1:8080/springmvc/jsp/****.jsp--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/" p:suffix=".jsp"></bean> </beans>
3.applicationContext.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:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"><bean id="person" class="com.test.bean.Person"> <property name="name" value="xingoo"/> <property name="age" value="12"/> </bean> <bean id="daoImpl" class="com.test.impl.DaoImpl"> </bean> <!-- service对象 --> <bean id="service" class="com.test.impl.ServiceImpl"> <!-- 设置DAO属性 --> <property name="dao" ref="daoImpl"></property> </bean></beans>
这里的文件名称和servlet-name-servlet.xml一样名称也可以改,并且位置可以放到根目录下。它的配置是由一个个bean组成的,bean由id唯一标识,用它来关联一个类。
bean下面有property name代表类的属性,value可以给它赋值,ref为他指定具体类。
4.在接收请求,可以用注解的方式来实现@Controller public class LoginAction { @RequestMapping("login.do") public String login(String username, String password) { if ("admol".equals(username)&&"123".equals(password)){ System.out.println(username + " 登录成功"); return "loginSuccess";//逻辑视图名 跳转页面默认为转发 } return "loginError"; } }
跳转页面时 路径配置根据servlet-name-servlet.xml:来配置<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/" p:suffix=".jsp"></bean>
在建立这个项目的时候,有的时候会报找不到org.springframework.web.context.ContextLoaderListener的错误,这是因为我的spring jar包没有放入web-info下