因爲項目現在每天需要統計網站登錄 註冊的數量變化,領導讓我做一個統計的系統。我對於功能強大性能穩定的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下