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";
}
}
'์คํ๋ง Spring > ๐งถ ์คํ๋ง ํ๋ ์์ํฌ Spring' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Spring MVC ํ๋ก์ ํธ์ ๊ธฐ๋ณธ๊ตฌ์กฐ (0) | 2021.04.27 |
---|---|
์คํ๋ง ํ๋ ์์ํฌ - ๋ก๊ทธ (Log4j) ๋ฐ ์ธํฐ์ ํฐ (Interceptor) ์ค์ ๋ฒ (0) | 2021.04.27 |
์คํ๋ง ํ๋ ์์ํฌ - Model ๊ฐ์ฒด (0) | 2021.04.19 |
์คํ๋ง ํ๋ ์์ํฌ - ์ด๊ธฐ ์ธํ , ๊ฐ๋ฐ ํ๊ฒฝ์ค์ (0) | 2021.04.19 |
Spring Framework์ ๊ฐ๋ ๊ณผ ๊ตฌ์กฐ (0) | 2021.04.19 |
๋๊ธ