Spring MVC - Spilna .Spring MVC Spring MV помогает в создании гибких и слабо

  • View
    214

  • Download
    0

Embed Size (px)

Text of Spring MVC - Spilna .Spring MVC Spring MV помогает в создании гибких и...

Spring MVC

Spring MVC

Spring MVC -. Model-View-Controller -, . . View . .

Architecture - modules

Core Container

Core Container Core, Beans, Context, and Expression Language :

- Core - Bean BeanFactory

factory.- Context

Core Beans, ApplicationContext .

- Expression Language

Data Access/Integration

:- JDBC JDBC- ,

JDBC- ORM

- (ORM), JPA, JDO, Hibernate IBATIS.

- OXM , / XML JAXB, Castor XMLBeans, JiBX XStream.

- Java Messaging Service - JMS .

- Transaction - , POJOs.

Web

Web, Web-, Web-Struts -,

:- Web - ,

, - .

- Web-Servlet Spring Model-View-Controller (MVC) -.

- Web-Struts Struts -

- Web-portlet MVC

Miscellaneous

- AOP aspect-oriented programming method-interceptors pointcuts

- Aspects AspectJ AOP framework.- Instrumentation

class loader - Test Spring

Junit TestNG- Integration- Batch

HelloWorldController

public class HelloWorldController extends AbstractController {

private String message;

@Override

protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {

return new ModelAndView("welcomePage","welcomeMessage", message);

// view name, model param name, model param

}

public void setMessage(String message) {

this.message = message;

}

}

web.xml

dispatcher org.springframework.web.servlet.DispatcherServlet 1

dispatcher*.htm

redirect.jsp

redirect.jsp

Redirect.jsp , Spring Web. JSP WEB-INF , , DispatcherServlet. view WEB-INF , .

dispatcher-servlet.xml

/WEB-INF/jsp/

Handler Mapping

DispatcherServlet BeanNameUrlHandlerMapping

/ex/view*.html=editAccountFormController/**/help.html=helpController

welcomePage.jsp

Welcome Page

${welcomeMessage}

Using annotations

dispatcher-servlet.xml

Using annotations

@Controller

@RequestMapping("/hello")

public class HelloController{

@RequestMapping(method = RequestMethod.GET)

public String printHello(ModelMap model) {

model.addAttribute("message", "Hello Spring MVC Framework!");

return "hello"; // name of the view

}

}

@RequestMapping(value = "/hello", method = RequestMethod.GET)

Form Handling

public class Student {private Integer age;private String name;private Integer id;public void setAge(Integer age) {

this.age = age;}public Integer getAge() {

return age;}public void setName(String name) { this.name = name; }public String getName() { return name;}public void setId(Integer id) {this.id = id;}public Integer getId() {return id; }

}

Form Handling

@Controllerpublic class StudentController {

@RequestMapping(value = "/student", method = RequestMethod.GET)public ModelAndView student() {

//"command" is required for tagsreturn new ModelAndView("student", "command", new Student());

}@RequestMapping(value = "/addStudent", method = RequestMethod.POST)public String addStudent(@ModelAttribute("SpringWeb")Student student,

ModelMap model) {model.addAttribute("name", student.getName());model.addAttribute("age", student.getAge());model.addAttribute("id", student.getId());return "result";

}}

student.jsp

Result.jsp

Spring IoC Containers

Spring IoC Containers - Spring Framework. , , , . Spring Container (DI) , . Spring Beans.

, . XML, Java , Java .

2

1) Spring BeanFactory Container , DI org.springframework.beans.factory.BeanFactory .BeanFactory , BeanFactoryAware, InitializingBean, DisposableBean2)Spring ApplicationContext Container enterprise ,

. org.springframework.context.ApplicationContext interface.

ApplicationContext BeanFactory

BeanFactory Container

public class HelloWorld {

private String message;

public void setMessage(String message){ this.message = message;}

public void getMessage(){System.out.println("Your Message : " + message);}

}

public class MainApp {

public static void main(String[] args) {

XmlBeanFactory factory = new XmlBeanFactory(new ClassPathResource("Beans.xml"));

HelloWorld obj = (HelloWorld) factory.getBean("helloWorld");

obj.getMessage();

}

}

ApplicationContext Container

import org.springframework.context.ApplicationContext;

Importorg.springframework.context.support.FileSystemXmlApplicationContext;

public class MainApp {

public static void main(String[] args) {

ApplicationContext context = new FileSystemXmlApplicationContext

("C:/src/Beans.xml");

HelloWorld obj = (HelloWorld) context.getBean("helloWorld");

obj.getMessage();

}

}

Spring Bean Scopes

singleton single instance per Spring IoC container (default).prototype a single bean definition to have any number of

instances.request HTTP request. Only valid in the context of a web-

aware Spring ApplicationContext.session HTTP session. Only valid in the context of a web-

aware Spring ApplicationContext.global-session global HTTP session. Only valid in the context

of a web-aware Spring ApplicationContext.

Autowiring

:no byName - property name.byType - by property datatype. constructor - similar to byType, but type applies to constructor

arguments. autodetect - Spring first tries to wire using autowire by constructor, if

it does not work, Spring tries to autowire by byType.

Annotation Based Config

@Required for settet methods and it indicates that the affected bean property must be populated in XML configuration file at

configuration time@Autowired -can be used to autowire bean @Qualifier - There may be a situation when you create more than one

bean of the same type and want to wire only one of them with a property

Autowiring

public class TextEditor {@Autowired(required=false)@Qualifier(check1")private SpellChecker spellChecker;public TextEditor() {

System.out.println("Inside TextEditor constructor." );}public SpellChecker getSpellChecker( ){ return spellChecker; }public void spellCheck(){spellChecker.checkSpelling();}

}

Autowiring

@Resource(name= "spellChecker")

public void setSpellChecker( SpellChecker spellChecker ){

this.spellChecker = spellChecker;

}

Q&A