IT&코딩/자바 프로젝트
자바 프로젝트 18일차 - Maven 1 (mvnStudent)
솔론
2023. 2. 12. 12:04
728x90
반응형
■ com.kgitabnk.mvnStudent
□ StudentController.java
package com.kgitbank.mvnStudent;
import java.io.File;
import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.kgitbank.mvnStudent.dto.StudentDTO;
import com.kgitbank.mvnStudent.service.StudentMapper;
@Controller
public class StudentController {
@Autowired
StudentMapper studentMapper;
@RequestMapping(value = {"/","index.do"}, method = RequestMethod.GET)
public String index() {
return "index";
}
@RequestMapping("student.do")
public String student() {
return "student/student";
}
@RequestMapping("list.do")
public String listStudent(HttpServletRequest req) {
List<StudentDTO> list = studentMapper.listStudent();
req.setAttribute("listStudent", list);
return "student/list";
}
@RequestMapping("/insert.do")
public String insertStudent(HttpServletRequest req, @ModelAttribute StudentDTO dto, BindingResult result) {
if(result.hasErrors()) {
dto.setImage("");
}
MultipartHttpServletRequest mr = (MultipartHttpServletRequest)req;
MultipartFile file = mr.getFile("filename");
String filename = file.getOriginalFilename();
System.out.println(file.getSize());
File target = new File(filename);
int filesize = 0;
try {
file.transferTo(target);
filesize = (int)file.getSize();
System.out.println(target.getAbsolutePath());
} catch(IOException e) {
e.printStackTrace();
}
System.out.println("filename = " + filename);
System.out.println("filesize = " + filesize);
dto.setImage(filename);
int res = studentMapper.insertStudent(dto);
if (res>0) {
req.setAttribute("msg", "학생등록 성공!! 학생목록페이지로 이동합니다.");
req.setAttribute("url", "list.do");
}else {
req.setAttribute("msg", "학생등록 실패!! 학생관리페이지로 이동합니다.");
req.setAttribute("url", "student.do");
}
return "message";
}
@RequestMapping("/delete.do")
public ModelAndView deleteStudent(@RequestParam String id) {
int res = studentMapper.deleteStudent(id);
ModelAndView mav = new ModelAndView();
if (res > 0) {
mav.addObject("msg", "학생삭제 성공!! 학생목록페이지로 이동합니다.");
mav.addObject("url", "list.do");
}else {
mav.addObject("msg", "학생삭제 실패!! 학생관리페이지로 이동합니다.");
mav.addObject("url", "student.do");
}
mav.setViewName("message");
return mav;
}
@RequestMapping("/find.do")
public String fintStudent(HttpServletRequest req, @RequestParam String cname) {
List<StudentDTO> find = studentMapper.findStudent(cname);
req.setAttribute("listStudent", find);
return "student/list";
}
}
■ com.kgitabnk.mvnStudent.dto
□ StudentDTO.java
package com.kgitbank.mvnStudent.dto;
public class StudentDTO {
private String id;
private String name;
private String cname;
private String image;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCname() {
return cname;
}
public void setCname(String cname) {
this.cname = cname;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
}
□ studentMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.kgitbank.mvnStudent.dto.studentMapper">
<select id="listStudent" resultType="studentDTO">
select * from student
</select>
<insert id="insertStudent" parameterType="studentDTO">
insert into student values(#{id}, #{name}, #{cname})
</insert>
<delete id="deleteStudent" parameterType="String">
delete from student where id = #{id}
</delete>
<select id="findStudent" parameterType="String" resultType="studentDTO">
select * from student where cname = #{cname}
</select>
</mapper>
■ com.kgitabnk.mvnStudent.service
□ StudentMapper.java
package com.kgitbank.mvnStudent.service;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.kgitbank.mvnStudent.dto.StudentDTO;
@Service
public class StudentMapper {
@Autowired
private SqlSession sqlSession;
public List<StudentDTO> listStudent(){
return sqlSession.selectList("listStudent");
}
public int insertStudent(StudentDTO dto) {
return sqlSession.insert("insertStudent", dto);
}
public int deleteStudent(String id) {
return sqlSession.delete("deleteStudent", id);
}
public List<StudentDTO> findStudent(String cname){
return sqlSession.selectList("findStudent", cname);
}
}
■ webapp/WEB-INF/spring
□ root-contenxt.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- Root Context: defines shared resources visible to all other web components -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:@localhost:1521:xe"/>
<property name="username" value="web01" />
<property name="password" value="web01" />
</bean>
<bean id="sqlSessionFactory"
class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="typeAliasesPackage"
value="com.kgitbank.mvnStudent.dto"/>
<property name="mapperLocations"
value="classpath:com/kgitbank/mvnStudent/dto/*.xml"/>
</bean>
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
</beans>
■ webapp/WEB-INF/spring/appServlet
□ servlet-context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />
<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<context:component-scan base-package="com.kgitbank.mvnStudent" />
<beans:bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<beans:property name="maxUploadSize" value="10485760" />
</beans:bean>
</beans:beans>
■ webapp/WEB-INF/views
□ index.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!-- index.jsp -->
<html>
<head>
<title>spring학생관리</title>
</head>
<body>
<h1 align="center">스프링으로 연습하는 학생관리 프로그램</h1>
<ul>
<li><a href="student.do">학생관리페이지로 이동</a>
</ul>
</body>
</html>
□ message.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!-- message.jsp -->
<script type="text/javascript">
alert("${msg}")
location.href="${url}"
</script>
■ webapp/WEB-INF/views/student
□ list.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!-- list.jsp -->
<html>
<head>
<title>학생목록</title>
</head>
<body>
<div align="center">
<hr color="green" width="300">
<h2>학 생 목 록 페 이 지</h2>
<hr color="green" width="300">
<table border="1" width="500">
<tr>
<th>아이디</th>
<th>학생명</th>
<th>학급명</th>
</tr>
<c:if test="${empty listStudent}">
<tr>
<td colspan="3">등록된 학생이 없습니다.</td>
</tr>
</c:if>
<c:forEach var="dto" items="${listStudent}">
<tr>
<td>${dto.id}</td>
<td>${dto.name}</td>
<td>${dto.cname}</td>
</tr>
</c:forEach>
</table>
</div>
</body>
</html>
□ student.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!-- student.jsp -->
<html>
<head>
<title>학생관리</title>
</head>
<body>
<div align="center">
<hr color="green" width="300">
<h2>학 생 등 록 페 이 지</h2>
<hr color="green" width="300">
<form name="f" action="insert.do" method="post" enctype="multipart/form-data">
<table border="1">
<tr>
<td align="center">
아이디 : <input type="text" name="id"><br>
학생명 : <input type="text" name="name"><br>
학급명 : <input type="text" name="cname"><br>
파일명 : <input type="file" name="filename"><br>
<input type="submit" value="전송">
<input type="reset" value="취소">
</td>
</tr>
</table>
</form>
<hr color="green" width="300">
<h2>학 생 삭 제 페 이 지</h2>
<hr color="green" width="300">
<form name="f" action="delete.do" method="post">
<table border="1">
<tr>
<td align="center">
아이디 : <input type="text" name="id">
<input type="submit" value="삭제">
</td>
</tr>
</table>
</form>
<hr color="green" width="300">
<h2>학 생 찾 기 페 이 지</h2>
<hr color="green" width="300">
<form name="f" action="find.do" method="post">
<table border="1">
<tr>
<td align="center">
학급명 : <input type="text" name="cname">
<input type="submit" value="찾기">
</td>
</tr>
</table>
</form>
<hr color="green" width="300">
<h2>학 생 목 록 페 이 지</h2>
<hr color="green" width="300">
<form name="f" action="list.do" method="post">
<table border="1">
<tr>
<td align="center">
<input type="submit" value="학생목록페이지로 이동">
</td>
</tr>
</table>
</form>
</div>
</body>
</html>
■ web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee https://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>
<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Processes application requests -->
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>euc-kr</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
■ pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.kgitbank</groupId>
<artifactId>mvnStudent</artifactId>
<name>mvnStudent</name>
<packaging>war</packaging>
<version>1.0.0-BUILD-SNAPSHOT</version>
<properties>
<java-version>1.6</java-version>
<org.springframework-version>3.1.1.RELEASE</org.springframework-version>
<org.aspectj-version>1.6.10</org.aspectj-version>
<org.slf4j-version>1.6.6</org.slf4j-version>
</properties>
<dependencies>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${org.springframework-version}</version>
<exclusions>
<!-- Exclude Commons Logging in favor of SLF4j -->
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<!-- AspectJ -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${org.aspectj-version}</version>
</dependency>
<!-- Logging -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${org.slf4j-version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>${org.slf4j-version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${org.slf4j-version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.15</version>
<exclusions>
<exclusion>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
</exclusion>
<exclusion>
<groupId>javax.jms</groupId>
<artifactId>jms</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.jdmk</groupId>
<artifactId>jmxtools</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.jmx</groupId>
<artifactId>jmxri</artifactId>
</exclusion>
</exclusions>
<scope>runtime</scope>
</dependency>
<!-- @Inject -->
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
</dependency>
<!-- Servlet -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- oracle -->
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc8</artifactId>
<version>18.0.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.2</version>
</dependency>
<!-- fileUpload -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.2</version>
</dependency>
<!-- Test -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.7</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.9</version>
<configuration>
<additionalProjectnatures>
<projectnature>org.springframework.ide.eclipse.core.springnature</projectnature>
</additionalProjectnatures>
<additionalBuildcommands>
<buildcommand>org.springframework.ide.eclipse.core.springbuilder</buildcommand>
</additionalBuildcommands>
<downloadSources>true</downloadSources>
<downloadJavadocs>true</downloadJavadocs>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<compilerArgument>-Xlint:all</compilerArgument>
<showWarnings>true</showWarnings>
<showDeprecation>true</showDeprecation>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<configuration>
<mainClass>org.test.int1.Main</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
728x90
반응형