标签:java www class ext control dispatch import 新建 bean
1.新建工程
New Project->勾选Spring、SpringMVC
2.
在src目录里新建一个包,新建HiController.java(作为controller部分)
import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; @Controller @RequestMapping("/hi") public class HiController { @RequestMapping("/say") public String say(Model model){ model.addAttribute("name","hyf"); model.addAttribute("url","fucker"); return "say"; } }
3.
在\web\WEB-INF\jsp目录里新建文件say.jsp(作为Controller部分)
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>title</title> </head> <body> hello world,${name} <br> ${url} </body> </html>
4.配置dispatcher-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: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="wormday.springmvc.helloworld"/> <!--指定视图解析器--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- 视图的路径 --> <property name="prefix" value="/WEB-INF/jsp/"/> <!-- 视图名称后缀 --> <property name="suffix" value=".jsp"/> </bean> </beans>
5.配置web.xml(改一下url-pattern)
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
6.配置Tomcat
7.运行,浏览器中输入http://localhost:8080/hi/say,即可看到效果
标签:java www class ext control dispatch import 新建 bean
原文地址:https://www.cnblogs.com/hyfer/p/10455545.html