๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

์Šคํ”„๋ง Spring/๐Ÿงถ ์Šคํ”„๋ง ํ”„๋ ˆ์ž„์›Œํฌ Spring

์Šคํ”„๋ง ํ”„๋ ˆ์ž„์›Œํฌ - ๊ฐ’ ๋„˜๊ธฐ๊ธฐ ๊ธฐ๋ณธ

by Meteora_ 2021. 4. 19.
728x90

index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>

Hello Spring

<br>
<a href="hello">hello๋กœ ์ด๋™</a> 
<a href="home.do">home์œผ๋กœ์ด๋™</a>

</body>
</html>

home.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>


<h2>home.jsp</h2>


<%
String name = (String)request.getAttribute("_name");

%>
์ด๋ฆ„:<%=name %>

์ด๋ฆ„:${_name }

<br>
<form action="world.do">
์ด๋ฆ„์ž…๋ ฅ:<input type="text" value="" name="name">
<br>
๋‚˜์ด์ž…๋ ฅ:<input type="text"  name="age">

<input type="submit" value="์ „์†ก">
</form>

<a href="world.do?age=56&name=๊ณฐ๋Œ์ด">world๋กœ ์ด๋™</a>


</body>
</html>

dispatcherServlet.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"
	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-4.3.xsd">
	
	<!-- spring MVC annotation(์ฃผ์„๋ฌธ,์ง€์‹œ๋ฌธ)์„ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•œ ์„ค์ • -->
	<context:annotation-config/>
	
	
	<!-- viewResolver ์„ค์ • ์‚ฌ์šฉ์ž์˜ view์˜์œ„์น˜์™€ ํ™•์žฅ์ž๋ช…์„ค์ • -->
	<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
	
		<property name="prefix" value="/WEB-INF/views/"></property> <!-- view์˜ ๊ฒฝ๋กœ -->
		<property name="suffix" value=".jsp"></property><!-- ํ™•์žฅ์ž ๋ช… -->
		
	</bean>
	<!--
	
	์œ„์™€๋™์ผ
	InternalResourceViewResolver viewResolver = new  InternalResourceViewResolver();
	viewResolver.prefix = "/WEB-INF/views/";
	viewResolver.suffix = ".jsp";
	
	
	  -->
	  
	  
	  <!-- java ๊ณตํ†ต ํŒจํ‚ค์ง€ -->
	<context:component-scan base-package="bit.com.a"/>
	
	

</beans>

HelloController.java

package bit.com.a;

import java.util.Date;

import org.slf4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import bit.com.a.dto.Human;



@Controller
public class HelloController {
	
	
	private static Logger logger = org.slf4j.LoggerFactory.getLogger(HelloController.class);
	
	
	@RequestMapping("hello")
	public String helloMethod() {
		//System.out.println("HelloController helloMethod()");
		
		logger.info("HelloController helloMethod()" + new Date());
		
		return "hello";// hello.jsp๋กœ ๊ฐ€๋ผ
	}
	
	@RequestMapping(value = "home.do", method = RequestMethod.GET)
	public String home(Model model) {
		logger.info("helloController home()"+ new Date());
		
		//์ง์‹ธ~
		String name ="์˜ค๋ Œ์ง€";
		model.addAttribute("_name", name);//์ง์‹ธ~ == req.setAttribute
		
		return "home";
		
	}
	/*
	@RequestMapping(value = "world.do", method = RequestMethod.GET)
	public String world(String name, int age) {
		logger.info("HelloController world()" + new Date());
		
		System.out.println("name:"+name);
		System.out.println("age:"+age);
		
		return "home";
		
		//์œ„๋ฐฉ์‹์€ ์ด๋ฆ„์ด ๋™์ผํ•ด์•ผ๋œ๋‹ค
		// ๋ฐ‘ ๋ฐฉ์‹์€ ์Šคํ”„๋ง ํ”„๋ ˆ์ž„์›Œํฌ๊ฐ€ dto์—๋‹ค๊ฐ€ ์•Œ์•„์„œ ๋„ฃ์–ด์ค€๋‹ค
	}
	*/
	
	//์˜์กด์„ฑ
	@RequestMapping(value = "world.do", method = RequestMethod.GET)
	public String world(Human ha) {
		// ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ์•Œ์•„์„œ ๋„ฃ์–ด์คŒ
		logger.info("HelloController world()" + new Date());
		
		System.out.println(ha.toString());
		
		return "home";
	}
	
	
	
	
}

๋Œ“๊ธ€