JAVA Framework-springmvc

Source: Internet
Author: User

Web. XML configuration file

<?xml version= "1.0" encoding= "UTF-8"? ><web-app xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns= "Http://xmlns.jcp.org/xml/ns/javaee" xsi:schemalocation= "Http://xmlns.jcp.org/xml/ns/javaee/http Xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd "id=" webapp_id "version=" 3.1 "><!--configuration Spring-mvc file-->< Servlet><servlet-name>springmvc</servlet-name><servlet-class> org.springframework.web.servlet.dispatcherservlet</servlet-class><init-param><!-- If you do not write this spring will automatically go to Web-inf to find Springmvc-servlet.xml file--><param-name>contextconfiglocation</param-name ><!--fixed name, cannot change--><param-value>classpath:spring-mvc.xml</param-value></init-param> <load-on-startup>1</load-on-startup>< load--></servlet><servlet-mapping> When the server starts!-- <servlet-name>springmvc</servlet-name><url-pattern>*.do</url-pattern><!-- Receive all requests with the. Do end--></servlet-mapping><!--Configure the Chinese filter--><fIlter><filter-name>characterencodingfilter</filter-name><filter-class> Org.springframework.web.filter.characterencodingfilter</filter-class><init-param><param-name >encoding</param-name><param-value>UTF-8</param-value></init-param></filter> <filter-mapping><filter-name>characterencodingfilter</filter-name><url-pattern>/*</ Url-pattern></filter-mapping></web-app>

 spring-mvc.xml configuration file

<?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" Xmlns:mvc= "Http://www.springframework.org/schema/mvc" xsi:schemalocation= "Http://www.springframework.org/schema /mvc Http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsdhttp://www.springframework.org/schema/beans Http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http ://www.springframework.org/schema/context/spring-context-4.3.xsd "><context:component-scan base-package=" Com.hanqi.controller "></context:component-scan><!--Note scanner--><bean class=" Org.springframework.web.servlet.view.InternalResourceViewResolver "><!--View resolver--><property name=" Prefix "value="/web-inf/page/"></property><property name=" suffix "value=". JSP ></property> </bean><mvc:annotation-driven><!--Configuring Spring message Converter--><mvc:message-converters register-defaults= "false" ><!-- Do not use the spring default supplied converter--><bean class= "Org.springframework.http.converter.StringHttpMessageConverter" ></ Bean><bean class= "Org.springframework.http.converter.ByteArrayHttpMessageConverter" ></bean>< Bean class= "Org.springframework.http.converter.BufferedImageHttpMessageConverter" ></bean><bean class = "Com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter" ><property name= "Supportedmediatypes" ><!--join supported media types: Return to contenttype--><list><value>text/html;charset=utf-8</value><!-- Here the order can not be reversed, must first write text/html, or IE will appear under the download prompt--><value>application/json;charset=utf-8</value></list> </property></bean></mvc:message-converters></mvc:annotation-driven></beans>

Appuser entity class files

Appusercontroller class:

Package Com.hanqi.controller;import Static Org.springframework.test.web.client.response.mockrestresponsecreators.withbadrequest;import Static Org.springframework.test.web.servlet.result.mockmvcresultmatchers.request;import Org.springframework.format.annotation.datetimeformat;import Org.springframework.stereotype.controller;import Org.springframework.ui.model;import Org.springframework.web.bind.annotation.modelattribute;import Org.springframework.web.bind.annotation.pathvariable;import Org.springframework.web.bind.annotation.requestmapping;import Org.springframework.web.bind.annotation.requestmethod;import Org.springframework.web.bind.annotation.requestparam;import Org.springframework.web.bind.annotation.responsebody;import Org.springframework.web.bind.annotation.sessionattributes;import Org.springframework.web.servlet.ModelAndView; Import Com.hanqi.model.Appuser; @SessionAttributes ("Orguser")//The annotation can only be decorated with the class, when the model is used to set the property, will automatically put the value of the model in the name of the Orguser in the session//can pass multiple ({"", "}) model also need to set multiple @controllEr//declares that the current class is the Controller Class @requestmapping ("/appuser")//defines the received request public class Appusercontroller {///Parameter Pass @requestmapping ("/login") Defines the accepted request public string login (@RequestParam ("uname") string uname,string Pword, @DateTimeFormat (pattern= "YYYY-MM-DD") String birthday) {System.out.println ("user name" + uname); SYSTEM.OUT.PRINTLN ("Password is" + Pword); System.out.println ("Birthday is" + birthday); return "index";} Entity passing, when passing an entity class with a date parameter, you need to annotate @datetimeformat (pattern= "Yyyy-mm-dd") on the member variable of the class//otherwise report 400 error @requestmapping ("/register") Public String Register (appuser user) {System.out.println ("username" +user.getuname ()); SYSTEM.OUT.PRINTLN ("Password is" + User.getpword ()); return "index";} Page Jump/* @RequestMapping (value= "/{path}")//with path as request parameter public string topage (@PathVariable ("path") string page) {return page;} */@ResponseBody//When you return a string to the foreground, you need to add this annotation public string select () {return null;} With MODELANDVIEW//SPRINGMVC, if the name of the view is not set, the request path is defaulted as the view name @requestmapping ("Login1")//defaultvalue if the value is not found to be the default value, The required default is true, which indicates that the request parameter must exist, can be an empty string but cannot be nullpublic Modelandview login1 (@RequestParam (valuE= "uname", defaultvalue= "uname", Required=false) String uname,string pword,modelandview mav) {appuser user = new Appuser ( ), User.setuname (uname), User.setpword (Pword), if ("admin". Equals (uname) && "123". Equals (Pword)) { Mav.addobject ("Orguser", user); Mav.setviewname ("Index");} return MAV;} Use Model@requestmapping (value= "login2", method={requestmethod.post})//Set the received request as a POST request, often with get,delete,put, If the mismatch is reported 405 error public String login2 (string uname,string pword,model Model) {//can also pass in the Session,request object for property settings Appuser user = new Appuser (); User.setuname (uname); User.setpword (Pword); if ("admin". Equals (uname) && "123". Equals (Pword)) {Model.addattribute ("Orguser", user);} Return "index";} You can get the value of Orguser at the view level}

JAVA Framework-springmvc

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.