spring study (three) - mvc

1. Establish project: idea to choose springnvc
dispatcherservlet.xml 2. automatically generated spring configuration. If you want the xml name or location changes, you need to add additional code to indicate the name and location of your configuration file

 <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!--自动加载-->
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <!-- 拦截一切请求,交给springmvc  -->
        <url-pattern>/</url-pattern>
    </servlet-mapping>

Additional configuration

<init-param>
  			<param-name>contextConfigLocation</param-name>
  			<param-value>classpath:springmvc.xml</param-value>
  	</init-param>

3. mapping configuration
Here Insert Picture Description
disposed under test to test packet-based mapping
Here Insert Picture Description
using @Controller make this class of test classes into a controller of the controller springmvc
Next, a method to configure

 @RequestMapping(value = "welcome")
    public String login(){
     
        return "success";

    }

When a form submit the following, the method will be performed automatically login

 <form action="/welcome1">
      name<input  type="text"name="name" >
      psw<input  type="text"name="password" >
        <input  type="submit">
  </form>

Prior to this, it is necessary Here Insert Picture Description
to configure the
a. @Controller configuration of the scanning type

 <!--  视图解析器 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">

b. Configure view resolver, adding a prefix, suffix

 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <!--增加前缀-->
         <property name="prefix" value="/view/"/>
        <!--增加后缀-->
        <property name="suffix"  value=".jsp"/>

    </bean>

Here Insert Picture Description

Here, the configuration of the mapping method above in connection with the return value String success
now add the prefix and suffix, became / view / success / jsp can access to the folder view success.jsp following
pages of
4. collective mapping parameters
can be by way method designation request (GET post Delete PUT)
@RequestMapping (value = "available for purchase", method = RequestMethod.POST) // mapping
so that the method can only accept the request of the post. Further parameters may be added directly, such as the following, it can only be accepted with the following parameters: name is "zs" password is requested 123

@RequestMapping(value = "welcome1/*/rua" ,
            method = RequestMethod.GET,
            params = {"name=zs","password=123"} )
    public  String welcome1(){
        return "success";
    }
  1. ant-style request path
    ? single character * any number of characters (0 or more) ** any directory

6. Summary:
SPRINGMVC various process parameters process / logic:
Request: requesting distal a-> @RequestMappting ( "a")
parameter of the processing request xyz:
@RequestMappting ( "A")
public String AA (@Xxx annotations ( "XYZ") XYZ)
{

}

Using the object (entity class Student) accepts request parameters

Entity class code sample
Here Insert Picture Description

Here Insert Picture Description
Form data

<form action="/welcome"  method="post">
      id<input  type="text"name="id" ><br/>
      name<input type="text" name ="name"><br/>
      address<<input type=" text" name="address"><br/>
      <input  type="submit">
  </form>

The method of mapping


    @RequestMapping(value = "welcome")
    public String getstudent(student student){
            System.out.println(student);
            return "success";

    }

Here Insert Picture Description
frequencyHere forget to change the character encoding,
but the student can use the generated object method

spring 二:
https://blog.csdn.net/Hqxcsdn/article/details/88533640

Published 68 original articles · won praise 3 · Views 5219

Guess you like

Origin blog.csdn.net/Hqxcsdn/article/details/88702862