본문 바로가기
Spring

59일차//spring framework//(1)값 넘겨보기 + home.jsp

by aesup 2021. 4. 12.
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=24&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";
	}
	
	
	
	
}

728x90