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"; } }