springmvc的注解式开发
1 简写前后缀
public class Frist implements Controller { public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception { ModelAndView mx=new ModelAndView(); mx.setViewName("index"); return mx; } }
在spring中加入了
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/"></property> <property name="suffix" value=".jsp"></property> </bean>
这么写就行了
关于静态资源无法访问的问题
3种解决方案
1 web.xml中加mapping
<!--<servlet-mapping>--> <!--<servlet-name>default</servlet-name>--> <!--<url-pattern>*.PNG</url-pattern>--> <!--</servlet-mapping>--> <!--<servlet-mapping>--> <!--<servlet-name>default</servlet-name>--> <!--<url-pattern>*.jpg</url-pattern>--> <!--</servlet-mapping>-->
直接加入mapping 什么类型就写什么 .什么结尾
2
<mvc:default-servlet-handler></mvc:default-servlet-handler>
在spring中加入这一行就行
3
<mvc:resources mapping="/img/**" location="/img/"></mvc:resources>
加入这个 后面陪路径
现在来讲简写类的id
<bean id="send" class="springmmv.cn.dao.Frist"></bean>
这个是哪个类的bean节点 ,这里的id改为了send ,有两种
1
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="urlMap"> <map> <entry key="/la" value="send"></entry> </map> </property> </bean>
2
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <!--<property name="mappings">--> <!--<props>--> <!--<prop key="/la">df</prop>--> <!--</props>--> <!--</property>--> </bean>
urlMap mappings
我们访问是,还是写/la就行
现在我们来改写类
我们让她继承一下
AbstractController
public class Frist extends AbstractController { protected ModelAndView handleRequestInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception { ModelAndView mx=new ModelAndView(); mx.setViewName("index"); return mx; } }
之后 ,spring的配置文件 要改写
<bean id="send" class="springmmv.cn.dao.Frist"> <property name="supportedMethods" value="GET,POST"></property> </bean>
改成这样就可以了
参数自动装配(四种)
package demo08ParamAuto01.AutoWire; import java.util.List; /** * Created by mycom on 2018/3/26. */ public class UserInfo { private String username; private String password; private Car car; private List<Car> list; public List<Car> getList() { return list; } public void setList(List<Car> list) { this.list = list; } public Car getCar() { return car; } public void setCar(Car car) { this.car = car; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
package demo08ParamAuto01.AutoWire; /** * Created by mycom on 2018/3/26. */ public class Car { private String brand; public String getBrand() { return brand; } public void setBrand(String brand) { this.brand = brand; } }
package demo08ParamAuto01.AutoWire; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; /** * Created by mycom on 2018/3/26. */ @Controller public class UserInfoController { @RequestMapping("/login") public String doLogin(String username, String password, Model model){ model.addAttribute("username",username); System.out.println(username); System.out.println(password); return "success"; } @RequestMapping("/loginUserInfo") public String doLoginUserInfo(UserInfo info, Model model){ model.addAttribute("username",info.getUsername()); System.out.println(info.getUsername()); System.out.println(info.getPassword()); return "success"; } @RequestMapping("/loginUserInfoCar") public String doLoginUserInfoCar(UserInfo info, Model model){ model.addAttribute("username",info.getUsername()); System.out.println(info.getUsername()); System.out.println(info.getPassword()); System.out.println(info.getCar().getBrand()); return "success"; } @RequestMapping("/loginUserInfoList") public String doLoginUserInfoList(UserInfo info, Model model){ model.addAttribute("username",info.getUsername()); System.out.println(info.getUsername()); System.out.println(info.getPassword()); System.out.println(info.getCar().getBrand()); System.out.println(info.getList().get(0).getBrand()); System.out.println(info.getList().get(1).getBrand()); return "success"; } }
<?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:mvc="http://www.springframework.org/schema/mvc" 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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="demo08ParamAuto01.AutoWire"></context:component-scan> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/"></property> <property name="suffix" value=".jsp"></property> </bean> </beans>
配置文件的扫描,之前都有
现在给一下,页面 jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %> <html> <head> <title>Title</title> </head> <body> <form action="${pageContext.request.contextPath}/loginUserInfoList" method="post"> 用户名:<input name="username"/> 密码:<input name="password"/> 车:<input name="car.brand"> 车1:<input name="list[0].brand"> 车2:<input name="list[1].brand"> 名:<input name="uname"> <input type="submit" value="提交"> </form> </body> </html>
2.路径变量 @PathVariable
路径参数类似请求参数,但没有key部分,只是一个值。例如下面的URL:
http://localhost:9090/showUser/spring
其中的spring是表示用户的密码字符串。在Spring MVC中,spring被作为路径变量用来发送一个值到服务器。Sping 3以后Spring 3以后支持注解@PathVariable用来接收路径参数。为了使用路径变量,首先需要在RequestMapping注解的值属性中添加一个变量,该变量必须放在花括号之间
@RequestMapping("/loginUserInfoPath/{username}") public String doLoginUserInfoPath(@PathVariable("username") String uname,String username){ System.out.println(uname); System.out.println(username); return "success"; }
不要忘记修改web.xml和springmvc.xml的配置文件的路径和包的路径
在页面上
<%-- Created by IntelliJ IDEA. User: mycom Date: 2018/3/26 Time: 11:57 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %> <html> <head> <title>Title</title> </head> <body> <form action="${pageContext.request.contextPath}/loginUserInfoPath/happy" method="post"> 用户名:<input name="username"/> <input type="submit" value="提交"> </form> </body> </html>