1. 程式人生 > >url對映方式

url對映方式

檔案結構

web.xml

<?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/javaeehttp://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"

version="4.0">

<servlet

>

<servlet-name>innotation</servlet-name>

<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

<init-param>

<param-name>contextConfigLocation</param-name>

<param-value>classpath:application.xml</param-value>

</init-param

>

<load-on-startup>1</load-on-startup>

</servlet>

<servlet-mapping>

<servlet-name>innotation</servlet-name>

<url-pattern>/</url-pattern>

</servlet-mapping>

</web-app>

 

 

 

 

 

application.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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd

http://www.springframework.org/schema/jeehttp://www.springframework.org/schema/jee/spring-jee.xsd

">

<bean class="innotation.urlinnotation"></bean> <!--如果使用註解方式,則不需要使用name(不用為其配置url-->

</beans>

 

 

 

 

urlinnotation控制器

package innotation;

 

 

import com.sun.deploy.net.HttpResponse;

import org.springframework.http.HttpRequest;

 

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.servlet.ModelAndView;

 

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import javax.servlet.http.HttpSession;

 

@Controller

public class urlinnotation {

 

 

@RequestMapping("/hello")

public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {

ModelAndView mvc=new ModelAndView();

mvc.addObject("msg","今天下大雨");

mvc.setViewName("/innotion.jsp");

return mvc;

}

@RequestMapping("/innotation")

public ModelAndView weather(HttpSession session){

ModelAndView mvc=new ModelAndView();

System.out.println(session);

mvc.addObject("msg","今天下大雨"+session);

mvc.setViewName("/innotion.jsp");

return mvc;

}

@RequestMapping("/innotation2")

public ModelAndView weather1(String name){

ModelAndView mvc=new ModelAndView();

System.out.println(name);

mvc.addObject("msg","今天下大雨"+name);

mvc.setViewName("/innotion.jsp");

return mvc;

}

@RequestMapping("/innotation4")

//ModelAndView和HttpServletRequest和HttpServletResponse必須得要有其中的一個,否則不能執行

public void weather2(HttpServletRequest req,HttpServletResponse resp)throws Exception{

System.out.println(1125464);

}

}

 

 

innotion.jsp

<%--

Created by IntelliJ IDEA.

User: Lenovo

Date: 2018/11/15

Time: 18:02

To change this template use File | Settings | File Templates.

--%>

<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<html>

<head>

${msg}

</head>

<body>

 

</body>

</html>