springMVC——Xml配置方式实现Helloworld
Xml配置helloworld
?
?
?
?
<?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"
?????????version="3.1">
</web-app>
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
??xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
??<modelVersion>4.0.0</modelVersion>
??<groupId>com.henu</groupId>
??<artifactId>springmvc_helloword_xml</artifactId>
??<version>1.0-SNAPSHOT</version>
??<packaging>war</packaging>
??<name>springmvc_helloword_xml Maven Webapp</name>
??<!-- FIXME change it to the project's website -->
??<url>http://www.example.com</url>
??<properties>
????<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
????<maven.compiler.source>1.8</maven.compiler.source>
????<maven.compiler.target>1.8</maven.compiler.target>
??</properties>
??<dependencies>
????<!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
????<dependency>
??????<groupId>org.springframework</groupId>
??????<artifactId>spring-web</artifactId>
??????<version>4.3.20.RELEASE</version>
????</dependency>
????<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
????<dependency>
??????<groupId>org.springframework</groupId>
??????<artifactId>spring-webmvc</artifactId>
??????<version>4.3.20.RELEASE</version>
????</dependency>
????<!-- 配置servlet -->
????<dependency>
??????<groupId>javax.servlet</groupId>
??????<artifactId>javax.servlet-api</artifactId>
??????<version>4.0.1</version>
??????<scope>provided</scope>
????</dependency>
????<!-- 配置jsp -->
????<dependency>
??????<groupId>javax.servlet.jsp</groupId>
??????<artifactId>javax.servlet.jsp-api</artifactId>
??????<version>2.3.3</version>
??????<scope>provided</scope>
????</dependency>
??</dependencies>
</project>
package com.henu.controller;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
?* @author George
?* @description
?**/
public class UserController implements Controller {
????/**
?????* 處理器方法
?????*
?????* @param httpServletRequest
?????* @param httpServletResponse
?????* @return
?????* @throws Exception
?????*/
????@Override
????public ModelAndView handleRequest(HttpServletRequest httpServletRequest,
??????????HttpServletResponse httpServletResponse) throws Exception {
????????//創建模型視圖對象
????????ModelAndView mav = new ModelAndView();
????????//把數據綁定到模型對象
????????mav.addObject("name","admin");
????????//設置跳轉的試圖對象
????????mav.setViewName("success");
????????return mav;
????}
}
?
<?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"
?????????version="3.1">
????<!--配置前端控制器-->
????<servlet>
????????<servlet-name>DispatcherServlet</servlet-name>
????????<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
????????<!--加載自定義springmvc配置文件-->
????????<init-param>
????????????<param-name>contextConfigLocation</param-name>
????????????<param-value>classpath:springMVC.xml</param-value>
????????</init-param>
????????<!--配置自啟動-->
????????<!-- 配置springmvc什么時候啟動,參數必須為整數 -->
????????<!-- 如果為0或者大于0,則springMVC隨著容器啟動而啟動 -->
????????<!-- 如果小于0,則在第一次請求進來的時候啟動 -->
????????<load-on-startup>1</load-on-startup>
????</servlet>
????<servlet-mapping>
????????<servlet-name>DispatcherServlet</servlet-name>
????????<url-pattern>/</url-pattern>
????</servlet-mapping>
</web-app>
?
?
<!-- 配置springmvc什么時候啟動,參數必須為整數 -->
????????<!-- 如果為0或者大于0,則springMVC隨著容器啟動而啟動 -->
????????<!-- 如果小于0,則在第一次請求進來的時候啟動 -->
????????<load-on-startup>1</load-on-startup>
<!--DispatcherServlet-servlet.xml-->
<!-- 服務器啟動創建servlet對象 ,同時加載springmvc配置文件,默認是在WEB-INF下加載一個叫[servlet-name]-servlet.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:p="http://www.springframework.org/schema/p"
???????xmlns:aop="http://www.springframework.org/schema/aop"
???????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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
????<!--配置控制器-->
????<bean name="/login" class="com.henu.controller.UserController"></bean>
????<!--映射處理器 -->
????<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
????<!--處理器適配器-->
????<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
????<!--視圖解析器-->
????<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
????????<!--指定跳轉位置?/success.jsp-->
????????<property name="prefix" value="/"/>
????????<property name="suffix" value=".jsp"/>
????</bean>
</beans>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
????<title>Title</title>
</head>
<body>
Hello World<br>
${name}<br>
${requestScope.name}<br>
</body>
</html>
根據自己的配置,然后在瀏覽器中輸入路徑:
http://localhost:9999/springmvc_helloword_xml_war_exploded/login
<!-- POST中文亂碼過濾器 -->
<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>
pom.xml文件中配置tomcat插件:
<build>
??<!-- 配置tomcat插件,web端 -->
??<plugins>
????<plugin>
??????<groupId>org.apache.tomcat.maven</groupId>
??????<artifactId>tomcat7-maven-plugin</artifactId>
??????<version>2.2</version>
??????<configuration>
????????<!-- 配置項目的請求路徑 -->
????????<path>/mvc</path>
????????<!-- 配置服務器端口號 -->
????????<port>9091</port>
??????</configuration>
????</plugin>
??</plugins>
</build>
?
超強干貨來襲 云風專訪:近40年碼齡,通宵達旦的技術人生總結
以上是生活随笔為你收集整理的springMVC——Xml配置方式实现Helloworld的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: maven项目创建过慢解决
- 下一篇: springMVC——注解配置方式实现H