context.xml
The context.xml file is a file contained in Tomcat that specifies the resources of the web application.
context.xml
<!-- web application will be reloaded. -->
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<WatchedResource>WEB-INF/tomcat-web.xml</WatchedResource>
<WatchedResource>${catalina.base}/conf/web.xml</WatchedResource>
When the Tomcat server runs, it reads the source code of web.xml according to the path specified in the WatchedResource tag in the context.xml file.
web.xml
Web.xml is a file that allows you to set up multiple settings for a web application. The following setup codes are included and additional setup codes can be entered.
root-context path setting
<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>
<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
The above code specifies the root-context.xml location to be used as a setup file using the param-value tag. Therefore, the spring project reads the setup information in root-context.xml, but is currently empty because the database is not connected.
Register DispatcherServlet, settings
<!-- Processes application requests -->
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
The above code specifies a servlet named DispatcherServlet and a servlet settings file. Like root-context, spring project runs and reads the setup information in servlet-context.xml.
필터 설정
<!-- 한글 처리를 위한 인코딩 필터 -->
<filter>
<filter-name>encoding</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>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
It is not in the project that was created by default, but it is a necessary code to facilitate the processing of Hangul. Therefore, you need to add the code to web.xml that sets the encoding to utf-8 as shown above.
servlet-context.xml
servlet.xml is a file that allows you to set up a DispatchServlet.
servlet-context.xml
<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />
<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<context:component-scan base-package="com.example.spring01" />
Annotatio-driven is a tag that enables @Controller.
The blank setting in the middle attaches the path string "/WEB-INF/views/" as a prefix to the class with the @Controller, and as a suffix ". A setting that automatically pastes an extension string called jsp".
The blank setting in the middle attaches the path string "/WEB-INF/views/" as a prefix to the class with the @Controller, and as a suffix ". A setting that automatically pastes an extension string called jsp".
Controller
A controller is a Java class file with an annotation named @Controller.
예시
@Controller
public class HomeController {
private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
/**
* Simply selects the home view to render by returning its name.
*/
@RequestMapping(value = "/", method = RequestMethod.GET)
public String home(Locale locale, Model model) {
logger.info("Welcome home! The client locale is {}.", locale);
Date date = new Date();
DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
String formattedDate = dateFormat.format(date);
model.addAttribute("serverTime", formattedDate );
return "home";
}
}
First of all, home methods are the key functions of the controller. The string that home methods return to is a web page that appears when you run a project. Originally, it should be a string indicating the path /WEB-INF/views/home.jsp, but the servlet-context.xml setting will only work if you enter the page name. I will explain the controller in detail in MVC pattern.
JSP page
home.jsp
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page session="false" %>
<html>
<head>
<title>Home</title>
</head>
<body>
<h1>
Hello world!
</h1>
<P> The time on the server is ${serverTime}. </P>
</body>
</html>
The home.jsp page appears on the screen because the home method of the controller returned to the home.
No comments:
Post a Comment