SPRINGMVC Configure the default path and JSP include jump controller and URL get access to Chinese garbled

Source: Internet
Author: User


One, SPRINGMVC configuration default Path



1. Default page for the default Tomcat container.
/index.html
This is a good way to access static pages (including JSPs) or pages without any parameters.



Or configure the welcome-file-list in Web.xml





<welcome-file-list>
		<welcome-file>index.html</welcome-file>
                <welcome-file> Index.jsp</welcome-file>
</welcome-file-list>






2. Configure Web.xml





<servlet>
<servlet-name> vanclshare </ servlet-name>
<servlet-class> org.springframework.web.servlet.DispatcherServlet </ servlet-class>
<init-param>
<param-name> contextConfigLocation </ param-name>
<param-value> classpath: vanclshare-servlet.xml </ param-value>
</ init-param>
<!-Initialize the servlet when starting the container->
<load-on-startup> 1 </ load-on-startup>
</ servlet>
<servlet-mapping>
<servlet-name> vanclshare </ servlet-name>
<!-Define which requests are handled by springmvc. "* .Html" indicates that the servlet is mapped by default, meaning that all mappings will be intercepted->
<url-pattern> *. html </ url-pattern>
</ servlet-mapping>
<!-Intercept request->
<servlet-mapping>
<servlet-name> vanclshare </ servlet-name>
<url-pattern> / index </ url-pattern>
</ servlet-mapping>
<!-Default welcome page->
<welcome-file-list>
<welcome-file> index </ welcome-file>
</ welcome-file-list>
Requestmapping to configure index in controller







@Controller public
class Indexcontroller {
	@RequestMapping ("/index") public
	Modelandview Index ()
	{
		Modelandview Modelandview = new Modelandview ("/index");
		return modelandview;
	}
	@RequestMapping ("/index1") public
	Modelandview Index1 ()
	{
		Modelandview modelandview = new Modelandview ( "/index1");
		Return Modelandview
	}
}








Direct access to Http://localhost:8080/VanclShare provides access to controller that is mapped to index.



Two, include jump controller
Add in index.jsp:





<body>
	This is my index
	<jsp:include page= "/index1.html" ></jsp:include>
</body>
All requests for *.html configuration are blocked according to the mapping interceptor in Web.xml. Then it jumps to the @requestmapping ("/index1") request and returns a view named Index1





Index1.jsp as follows:


<body>
	 This is index1
</body>
Direct access to Http://localhost:8080/VanclShare gets the following results:








Third, URL get access to Chinese garbled



In order to prevent Chinese view garbled, request garbled and so on, generally we need to configure three places.



Here we are uniformly configured for the UTF-8 pattern

<% @ page language = "java" contentType = "text / html; charset = UTF-8"
    pageEncoding = "UTF-8"%>
<! DOCTYPE html PUBLIC "-// W3C // DTD HTML 4.01 Transitional // EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv = "Content-Type" content = "text / html; charset = UTF-8">
<title> Insert title here </ title>
</ head>
<body>
This is index1
</ body>
</ html>
2, web.xml configuration character set filter
The configuration is as follows:

<!-Configure character set->
<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>
<init-param>
<param-name> forceEncoding </ param-name>
<param-value> true </ param-value>
</ init-param>
</ filter>
<filter-mapping>
<filter-name> CharacterEncodingFilter </ filter-name>
<url-pattern> / * </ url-pattern>
</ filter-mapping>
3. Configure the character set of the web server. Here tomcat as an example
Find the server.xml file under conf and add the character set URIEncoding = "UTF-8"

<Connector connectionTimeout = "20000" port = "8080" protocol = "HTTP / 1.1" redirectPort = "8443" URIEncoding = "UTF-8" />
Fourth, configure domain name access

The first is <Engine defaultHost = "localhost" name = "Catalina"> Change the value of defaultHost to your domain name

<Engine defaultHost = "www.test.com" name = "Catalina">

The second is <Host appBase = "webapps" autoDeploy = "true" name = "localhost" unpackWARs = "true" xmlNamespaceAware = "false" xmlValidation = "false">

 Change the value of name to your domain name
<Host appBase = "webapps" autoDeploy = "true" name = "www.test.com" unpackWARs = "true" xmlNamespaceAware = "false" xmlValidation = "false"> www.test.com: For a good domain name or a valid domain name IP address for access

Related Article

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.