SPRINGMVC data exchange between the controller and the view

Source: Internet
Author: User

1, first create the spring master configuration file (Applicationcontaxt.xml if written in Web-inf, there is no need to configure the context, is not to tell it the path, Web-inf will automatically load), The listener is responsible for loading , importing the class library, adding spring support

2,STRU2 's core controller is the one I wrote. The action view is the JSP model: The data access layer, the business logic layer, the entity class Strutsprepareandexecutefilter

3,SPRINGMVC: First configure the core controller in Web. XML: Add Servlet,dispatcherservlet class needs to load SPRINGMVC itself configuration file pseudo static *.html called suffix match

4,SPRINGMVC profile : The SPRINGMVC configuration file is loaded automatically under the Load master configuration file. must be built under Web-inf , file name {Servletname}-servlet.xml One can not be written wrong, Servletname is my name in Web. XML, the core controller is responsible for loading

5, two profiles will produce two IOC containers Applicationcontaxt.xml is the primary container;The SPRINGMVC configuration file is a child container

The public bean can be configured in the main container, the child can use the master, but the master cannot use the child

The  first step is to configure the Controller as a spring-managed bean @Controller ("Hellocontroller")
The second step is to call the @RequestMapping ("/") @RequestMapping ("Hello")


The 1th difference:


The method that handles the request in struts2 must be public, return a string, and have no argument
Public StringExecute ()Throws Exception {//from session to get the user who is currently adding the machine to the fault (username) sessionmap.get ("user");//Add User Problem.setuser for problem ( user);//Call service to save Problemproblemservice.saveproblem (problem);return SUCCESS;}
The 2nd difference:
A class can only write a method, and then go to Struts.xml to configure the namespace additions and deletions four x acction

<!--background Failure registration-
<package name= "Problem-add"namespace= "/background/regist" extends= "Default" >
<!--add a failure check-in
<action name= "Add" class= "Cn.bdqn.problem.action.problem.AddProblemAction" >
         if the Succuss result is returned, the instruction in result is executed
<result type= "Redirectaction" >
<param name= "namespace" >/background/reply</param>
<param name= "ActionName" >list</param>
</result>
</action>
</package>


SPRINGMVC can return multiple types of results, the method can have parameters, very flexible
The controller must be added to the IOC container to become a bean in spring----> annotations
A class can add multiple methods, one method is a URL path
//
  the 3rd difference: All go first the controller, the controller processes the request, and then calls the view display and avoids the browser to call the view directly.  Create a new directory view under Web-inf because Action,   
struts2: JSP written under Webroot, mybatis:jsp written under Web-int
package Cn.bdqn.mvc.controller;import Org.springframework.stereotype.controller;import Org.springframework.web.bind.annotation.requestmapping; @Controller ("Hellocontroller")   @RequestMapping ("/") //write/ The methods represented in Hellocontroller are public class Hellocontroller {//http://localhost:8080/mvc/hello.html @RequestMapping ("Hello") //Access public String Hello () {return "by the name of Hello   Hello "; The return string is called  Returns the view name  } 
One, passing data from the controller to the view
pass data to the view by returning Modelandview object ------ Each time a different content is passed to the page
public Modelandview Hello1 () {
        modelandview Mav=new modelandview ("Hello");
        mav=mav.addobject ("Message", "helloworld!"); /Such a write, automatically exists in the request scope
        
        return Mav;
    }
2 . To manipulate the request scope directly by passing in the HttpServletRequest object------each time a different content is passed to the page
//can also be stored directly in the request scope, not through the Modelandview object
    public String Hello (httpservletrequest request) {//due to the flexibility of spring, it is only required to put the object in the method as a parameter, it will automatically become an object
        request.setattribute ("Message", "helloworld!");
        return "Hello";
    }
3 . By adding a @modelattribute annotation to the controller's method
  Adding the method's return value to the model
Public Hellocontroller () {//controller only creates one object by default, because spring defaults to Singlton.
System.out.println ("called the Hellocontroller No-argument construction method");
}
Each time a fixed pass of data is requested, you can define a GET method for the controller class, such as pinning the title of your visit to the page regardless of where you visit, and each time you pass the same content to the page
The @ModelAttribute ("title")//is called first by SPRINGMVC, adding the return value of the method to the model (which means adding to the request scope)
Public String GetTitle () {
Return "SPRINGMVC Tutorial";
}

<bean class= "Org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix= "/web-inf/view/"
p:suffix= ". JSP"

/>

Ii. submitting data from a view to a controller
1. Receive form data via the controller's method parameters

@RequestMapping(value= "login", Method=requestmethod.get)public String Showlogin () {return "login";} /** But now there's a problem with the path of showing and committing, when executing the first (with GET request), when executing the second (with a POST request) *///form submission  does not need to define another commit path, but also let him submit back to login.html. is the default   form field less can accept @requestmapping (value= "login", Method=requestmethod.post)public String dologin in this way ( String loginname,string password) {System.out.println ("Login name:" +loginname); System.out.println ("Password:" +password); return "Hello";}


2. Encapsulating data submitted by a form through Java objects

We recommend that you define a form class @requestmapping (value= "login", method=requestmethod.post) public String doLogin2 ( in the case of an entity class) without accepting requests. LoginForm loginform) {System.out.println ("Login name:" +loginform.getloginname ()); System.out.println ("Password:" +loginform.getpassword ()); return "Hello";}


In struts2, the attribute that receives the data is declared in the class, and the SPRINGMVC is passed the parameter of the method

SPRINGMVC data exchange between the controller and the view

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.