springmvc基礎配置-02
阿新 • • 發佈:2022-03-10
註解開發
spring-servlet簡化
<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" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd"> <context:component-scan base-package="com.dronff.controller"/>
<!--需要讓spring掃描註解的包-->
<mvc:default-servlet-handler/> <mvc:annotation-driven /> <bean id="InternalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"/> <property name="suffix" value=".jsp"/> </bean> </beans>
HelloController.java
package com.dronff.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMapping; @Controller
//@Controller == @Service == @Repository == @component 不同層用不同的註解 public class HelloController{ @RequestMapping("/s/{a}") public String test1(@PathVariable int a, Model m){ m.addAttribute("z",a); return "test"; } }