Web层SpringMVC开发
在WEB-INF下的web.xml中配置
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--配置springmvc需要加载的配置文件-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/spring-*.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
在resources下的spring下新建spring-web.xml
spring-web.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"
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 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--配置SpringMVC-->
<!--开启springmvc注解 配置处理器映射器和处理器适配器-->
<mvc:annotation-driven/>
<!--处理静态资源-->
<mvc:default-servlet-handler/>
<!--配置视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!--扫描包-->
<context:component-scan base-package="me.debugjoker.web"/>
</beans>
新建me.debugjoker.web包
CourseController.java
package me.debugjoker.web;
import me.debugjoker.dto.ChooseResult;
import me.debugjoker.dto.CourseExecution;
import me.debugjoker.dto.Exposer;
import me.debugjoker.entity.Course;
import me.debugjoker.enums.CourseStateEnum;
import me.debugjoker.exception.ChooseCloseException;
import me.debugjoker.exception.RepeatChooseException;
import me.debugjoker.service.CourseService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import java.util.Date;
import java.util.List;
@Controller
@RequestMapping(value = "/course")
public class CourseController {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
@Autowired
private CourseService courseService;
@RequestMapping(value = "/list", method = RequestMethod.GET)
public String list(Model model) {
List<Course> courseList = courseService.getCourseList();
model.addAttribute("list", courseList);
return "list";
}
@RequestMapping(value = "/{courseId}/detail", method = RequestMethod.GET)
public String detail(@PathVariable("courseId") Long courseId, Model model) {
if (courseId == null) {
return "redirect:/course/list";
}
Course course = courseService.queryCourseById(courseId);
if (course == null) {
return "forward:/course/list";
}
model.addAttribute("course", course);
return "detail";
}
@RequestMapping(value = "/{courseId}/exposer",
method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
@ResponseBody
public ChooseResult<Exposer> exposer(@PathVariable("courseId") Long courseId) {
ChooseResult<Exposer> result;
try {
Exposer exposer = courseService.exportCourseUrl(courseId);
result = new ChooseResult<Exposer>(true, exposer);
} catch (Exception e) {
logger.error(e.getMessage(), e);
result = new ChooseResult<Exposer>(false, e.getMessage());
}
return result;
}
@RequestMapping(value = "/{courseId}/{md5}/execution",
method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
@ResponseBody
public ChooseResult<CourseExecution> execute(@PathVariable("courseId") Long courseId,
@PathVariable("md5") String md5,
@CookieValue(value = "studentId",required = false) Long studentId) {
ChooseResult<CourseExecution> result;
if (studentId == null){
return new ChooseResult<CourseExecution>(false,"未登录");
}
try {
CourseExecution execution = courseService.executeCourseChoose(courseId,studentId,md5);
return new ChooseResult<CourseExecution>(true,execution);
}catch (RepeatChooseException e){
CourseExecution execution = new CourseExecution(courseId,CourseStateEnum.REPEAT_CHOOSE);
return new ChooseResult<CourseExecution>(false,execution);
}catch (ChooseCloseException e){
CourseExecution execution = new CourseExecution(courseId,CourseStateEnum.END);
return new ChooseResult<CourseExecution>(false,execution);
}catch (Exception e){
logger.error(e.getMessage(),e);
CourseExecution execution = new CourseExecution(courseId,CourseStateEnum.INNER_ERROR);
return new ChooseResult<CourseExecution>(false,execution);
}
}
@RequestMapping(value = "/time/now",method = RequestMethod.GET)
public ChooseResult<Long> time(){
Date nowTime = new Date();
return new ChooseResult<Long>(true,nowTime.getTime());
}
}
开始写jsp页面