Spring MVC Summary One (Spring MVC basic example)

Source: Internet
Author: User

I. Directory structure

  

Ii. Concrete Steps

2.1 Applicationcontext.xml Configuration

<?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"
xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans.xsd
Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd ">

<context:annotation-config/>
</beans>

2.2 Mvc.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"
xmlns:context= "Http://www.springframework.org/schema/context"
Xmlns:mvc= "Http://www.springframework.org/schema/mvc"
Xsi:schemalocation= "Http://www.springframework.org/schema/beans
Http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
Http://www.springframework.org/schema/context
Http://www.springframework.org/schema/context/spring-context-3.0.xsd
Http://www.springframework.org/schema/mvc
Http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd ">

<mvc:annotation-driven/>
<!--auto-Scan Package--
<context:component-scan base-package= "Pr.cgl.controller"/>

<!--MVC returns the configuration of the page--
<bean id= "Viewresolver" class= "Org.springframework.web.servlet.view.InternalResourceViewResolver" >
<property name= "Viewclass" value= "Org.springframework.web.servlet.view.JstlView"/>
<!--template path is web-inf/pages/--
<property name= "prefix" value= "/web-inf/"/>
<!--view template suffix is. JSP--
<property name= "suffix" value= ". jsp"/>
</bean>

</beans>

2.3 Web

<?xml version= "1.0" encoding= "UTF-8"?>
<web-app xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"
Xmlns= "Http://java.sun.com/xml/ns/javaee"
xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version = "2.5" >


<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>SpringWebT1.root</param-value>
</context-param>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
Classpath:applicationContext.xml
</param-value>
</context-param>

<!--start Spring load-
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:log4j.properties</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>

<!--load Springmvc--
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

<!--end With. htm is blocked by MVC--
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/SpringWebT1/*</url-pattern>
</servlet-mapping>

</web-app>

2.4 Adding logs

2.4.1 Configuration Log4j.properties

# # # log4j Properties # # #

# Options are <debug, INFO, WARN, ERROR or fatal>

# Use-appenders, one-to-log to console, another-to-log to a file
Log4j.rootlogger=all,debug,info,error,console
#log4j. Rootlogger=debug,error,console
#log4j. Rootlogger=error,console
Log4j.threshold=all

Log4j.category.com.jlt.mis=debug,adapterfile
Log4j.appender.adapterfile=org.apache.log4j.rollingfileappender
log4j.appender.adapterfile.maxfilesize=46mb
Log4j.appender.adapterfile.maxbackupindex=3
Log4j.appender.adapterfile.file=${springwebt1.root}/logs/jltshop.log
Log4j.appender.adapterfile.append=true
Log4j.appender.adapterfile.layout=org.apache.log4j.patternlayout
Log4j.appender.adapterfile.layout.conversionpattern=%t%d{date}%c{3}%p-%m%n

# WRITE LOG to A CONSOLE
Log4j.appender.console=org.apache.log4j.consoleappender
Log4j.appender.console.target=system.out
Log4j.appender.console.layout=org.apache.log4j.patternlayout
#log4j. appender.console.layout.conversionpattern=[springwebt1]%d{mm/dd/yyyy Hh:mm:ss,sss}-%C{1}.%M (%F:%L) > >%p:%m%n
LOG4J.APPENDER.CONSOLE.LAYOUT.CONVERSIONPATTERN=%D{MM Hh:mm:ss,sss}-%M (%f:%l) >>%p:%m%n
Log4j.appender.console.threshold=debug

# WRITE LOG to A file, roll the file every day
Log4j.appender.info=org.apache.log4j.dailyrollingfileappender
Log4j.appender.info.layout=org.apache.log4j.patternlayout
Log4j.appender.info.layout.conversionpattern=%d{mm/dd/yyyy hh:mm:ss,sss}-%c{1}.%m (%F:%L) >>%p:%m%n
Log4j.appender.info.datepattern= '. ' Yyyy-mm-dd
Log4j.appender.info.threshold=info
Log4j.appender.info.append=true
Log4j.appender.info.file=${springwebt1.root}/logs/infologfile.log

Log4j.appender.debug=org.apache.log4j.dailyrollingfileappender
Log4j.appender.debug.layout=org.apache.log4j.patternlayout
Log4j.appender.debug.layout.conversionpattern=%d{mm/dd/yyyy Hh:mm:ss,sss}-%c{1}.%m (%F:%L) >>%p:%m%n
Log4j.appender.debug.datepattern= '. ' Yyyy-mm-dd
Log4j.appender.debug.threshold=debug
Log4j.appender.debug.append=true
Log4j.appender.debug.file=${springwebt1.root}/logs/debuglogfile.log

Log4j.appender.error=org.apache.log4j.dailyrollingfileappender
Log4j.appender.error.layout=org.apache.log4j.patternlayout
Log4j.appender.error.layout.conversionpattern=%d{mm/dd/yyyy Hh:mm:ss,sss}-%c{1}.%m (%F:%L) >>%p:%m%n
Log4j.appender.error.datepattern= '. ' Yyyy-mm-dd
Log4j.appender.error.threshold=error
Log4j.appender.error.append=true
Log4j.appender.error.file=${springwebt1.root}/logs/errorlogfile.log
2.4.2 Configuring Web. Xml
Add the following code snippet
    <context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:log4j.properties</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>

2.5 Controller

Package Pr.cgl.controller;

Import Org.springframework.stereotype.Controller;
Import Org.springframework.ui.Model;
Import org.springframework.web.bind.annotation.RequestMapping;

/**
* Created by CGL on 2015/9/10.
*/
@Controller
@RequestMapping ("/main")
public class TestController {

@RequestMapping ("/index.html")
Public String Index (Model m) {
M.addattribute ("name", "CLG");
Return "index";
}
}
2.6 Testing
Address bar input http://localhost:8080/SpringWebT1/main/index.html, get Hello, CLG

Spring MVC Summary One (Spring MVC basic example)

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.