spring mvc 配置文件

springmvc 工程搭建-----Dynamic Web project

java包添加:commons-logging-1.0.4.jar jstl.jar standard.jar mysql-connector-java-5.1.26-bin.jar

spring-framework-4.1.2-release-dist

参考mvc.html

web.xml

<?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" id="WebApp_ID" version="2.5">
  <display-name>springmvc</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
  </welcome-file-list>
  
  <servlet>
  <!-- 分发处理 -->
      <servlet-name>springmvc</servlet-name>
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
 <init-param>
 <!-- 分发到spring-mvc.xml处理 -->
    <param-name>contextConfigLocation</param-name>
     <param-value>class-path:spring-mvc.xml</param-value>
 </init-param>
  </servlet>
  <!-- 拦截*do的请求 -->
  <servlet-mapping>
      <servlet-name>springmvc</servlet-name>
      <url-pattern>*do</url-pattern>
  </servlet-mapping>
</web-app>

spring-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:p="http://www.springframework.org/schema/p"
    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.xsd">
<!--使用注解包-包括子集-->
    <context:component-scan base-package="com.huawei"/>


    <!--视图解析-->
    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.ResourceBundleViewResolver">
    <property name="prefix" value="/WEB-INF/jsp"/>
    <property name="suffix" value=".jsp"/>
</bean>


</beans>

controller

package com.huawei.controller;


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


@Controller
public class HelloWorldController {
@RequestMapping("/hello")
public String hello (Model model)
{
model.addAttribute("message", "SpringmVC 你好!");
return "helloye";
}

}

helloye.jsp

<%@ 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>
${message}
</body>

</html>

index.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="hello.do">hello</a>
</body>
</html>

猜你喜欢

转载自blog.csdn.net/heguiliang_123/article/details/80154270