DTO
package cohttp://m.app.test;
import org.springframework.beans.factory.annotation.Autowired;
public class Person {
String name;
int age;
public Person() {
super();
// TODO Auto-generated constructor stub
}
@Override
public String toString() {
return "person [name=" + name + ", age=" + age + "]";
}
public Person(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
Service
package cohttp://m.app.test;
public class TestService {
public String xx() {
System.out.println("service.xx()");
return "xx";
}
}
Controller
package cohttp://m.app.test;
import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;
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.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class TestController {
@Autowired
Person person; DTO
@Autowired
TestService service; Service
@RequestMapping("/xxx")
public String xxx(){
System.out.println("Person" + person.toString());
System.out.println("Service" + service.xx());
return "login";
}
public TestController() {
super();
}
public TestController(Person person, TestService service) {
super();
this.person = person;
this.service = service;
}
@Override
public String toString() {
return "TestController [person=" + person + ", service=" + service + "]";
}
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
public TestService getService() {
return service;
}
public void setService(TestService service) {
this.service = service;
}
}
Servlet-context.xml - 컨트롤러와 그 함수를 id로 가져옴
<?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">
<context:annotation-config></context:annotation-config>
<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>
<beans:bean class="com.app.test.TestController" id="xxx"/>
</beans:beans>
root-context.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">
<bean class="com.app.test.TestService" id="testService"></bean>
<bean class="com.app.test.Person" id="person">
<property name="name" value="홍길동"></property>
<property name="age" value="10"></property>
</bean>
</beans>
'단순 코드 기록 > Spring' 카테고리의 다른 글
Spring_Parsing (0) | 2024.02.08 |
---|---|
Spring_Multi_controller (0) | 2024.02.08 |
Spring_MVC_Repository (0) | 2024.02.08 |
Spring_MVC_Autowired (0) | 2024.02.08 |
Spring_MVC_Annotation (0) | 2024.02.08 |