1、如何快速學習springmvc
首先,我們需要在複制spring相關的jar包到web-inf/lib裡面去,然後在web.xml裡面加入以下代碼,相當于springmvc裡面的servlet,這裡隻說明了一些常見的用法,如果要了解springmvc裡面的控制器這些詳細原理可以到網上再去找好詳細學習。
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.json</url-pattern>
</servlet-mapping>
然後再在application.xml裡面配置下面代碼,
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:task="http://www.springframework.org/schema/task"
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
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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task.xsd">
<!-- 自動掃描的包名 -->
<context:component-scan base-package="com.shishuo.studio"></context:component-scan>
<mvc:annotation-driven />
<task:annotation-driven />
<tx:annotation-driven />
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
<bean
class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
<!-- 在XML配置檔案中加入外部屬性檔案,當然也可以指定外部檔案的編碼 -->
<bean id="propertyConfigurer" class="com.shishuo.studio.util.PropertyUtils">
<property name="locations">
<list>
<value>classpath:shishuo.studio.properties</value> <!-- 指定外部檔案的編碼 -->
</list>
</property>
</bean>
<!-- FreeMarker的配置 -->
<bean id="freeMarkerConfigurer"
class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<property name="templateLoaderPath" value="/WEB-INF/ftl" /><!--
指定路徑 -->
<property name="defaultEncoding" value="UTF-8" /><!-- 指定編碼格式 -->
<property name="freemarkerSettings">
<props>
<prop key="template_update_delay">10</prop>
<prop key="defaultEncoding">UTF-8</prop>
<prop key="url_escaping_charset">UTF-8</prop>
<prop key="locale">zh_CN</prop>
<prop key="boolean_format">true,false</prop>
<prop key="time_format">HH:mm:ss</prop>
<prop key="datetime_format">yyyy-MM-dd HH:mm:ss</prop>
<prop key="date_format">yyyy-MM-dd</prop>
<prop key="number_format">#.##</prop>
<prop key="whitespace_stripping">true</prop>
<prop key="classic_compatible">true</prop>
</props>
</property>
</bean>
<!-- 配置 FreeMarker視圖解析器 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.freemarker.FreeMarkerView"></property>
<property name="cache" value="false" />
<property name="prefix" value="/" />
<property name="suffix" value=".ftl" /><!--可為空,友善實作自已的依據擴充名來選擇視圖解釋類的邏輯 -->
<property name="contentType" value="text/html;charset=utf-8" />
<property name="exposeRequestAttributes" value="true" />
<property name="exposeSessionAttributes" value="true" />
<property name="exposeSpringMacroHelpers" value="true" />
</bean>
我們試圖模闆用的是freemarker,是以字尾名是以.ftl結束,如果是用的jsp,那這個配置檔案裡面的
<property name="suffix" value=".ftl" />valeu改為.jsp
然後我再給出類給大家看見
package com.shishuo.studio.action;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import com.shishuo.studio.constant.SystemConstant;
import com.shishuo.studio.entity.Category;
import com.shishuo.studio.entity.vo.CourseVo;
import com.shishuo.studio.entity.vo.PageVo;
import com.shishuo.studio.exception.CategoryNotFoundException;
import com.shishuo.studio.exception.notfound.StorageNotFoundException;
import com.shishuo.studio.service.CategoryService;
import com.shishuo.studio.service.UserService;
/**
* @author Herbert
*
*/
@Controller
@RequestMapping("/category")
public class CategoryAction extends BaseAction {
protected final Logger logger = Logger.getLogger(this.getClass());
@Autowired
protected CategoryService categoryService;
@Autowired
protected UserService userService;
/**
* 首頁
*
* @param modelMap
* @return
*/
@RequestMapping(value = "/{categoryId}.htm", method = RequestMethod.GET)
public String category(@PathVariable long categoryId, ModelMap modelMap,
@RequestParam(value = "p", defaultValue = "1") int p) {
try {
// 獲得資料
Category category = categoryService.getCategoryById(categoryId);
// 擷取目前目錄下的所有課程
PageVo<CourseVo> coursePageVo = courseService
.getCoursePageByIdForUser(categoryId, p, 24);
// 增加屬性
modelMap.addAttribute("category", category);
modelMap.put("coursePageVo", coursePageVo);
return "category";
} catch (CategoryNotFoundException e) {
return SystemConstant.PAGE_404;
} catch (StorageNotFoundException e) {
// TODO Auto-generated catch block
return SystemConstant.PAGE_404;
}
}
}
package com.shishuo.studio.action;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.shishuo.studio.action.auth.AuthBaseAction;
@Controller
@RequestMapping("/about")
public class AboutAction extends AuthBaseAction {
/**
* 跳轉到關于我們頁面
*
* @param modelMap
* @param request
* @return
*/
@RequestMapping(value = "/about.htm", method = RequestMethod.GET)
public String about(ModelMap modelMap, HttpServletRequest request) {
return "/about/about";
}
/**
* 跳轉到服務協定頁面
*
* @param modelMap
* @param request
* @return
*/
@RequestMapping(value = "/service.htm", method = RequestMethod.GET)
public String service(ModelMap modelMap, HttpServletRequest request) {
return "/about/service";
}
/**
* 跳轉到投訴舉報頁面
*
* @param modelMap
* @param request
* @return
*/
@RequestMapping(value = "/complain.htm", method = RequestMethod.GET)
public String complain(ModelMap modelMap, HttpServletRequest request) {
return "/about/complain";
}
/**
* 跳轉到版權聲明頁面
*
* @param modelMap
* @param request
* @return
*/
@RequestMapping(value = "/copyright.htm", method = RequestMethod.GET)
public String copyright(ModelMap modelMap, HttpServletRequest request) {
return "/about/copyright";
}
/**
* 跳轉到聯系我們頁面
*
* @param modelMap
* @param request
* @return
*/
@RequestMapping(value = "/connect.htm", method = RequestMethod.GET)
public String connect(ModelMap modelMap, HttpServletRequest request) {
return "/about/connect";
}
}