标签:springmvc haha 学习 int test model servlet public
springmvc 学习2
controller
public class ControllerTest01 implements Controller {
@Override
public ModelAndView handleRequest(javax.servlet.http.HttpServletRequest httpServletRequest, javax.servlet.http.HttpServletResponse httpServletResponse) throws Exception {
ModelAndView mv=new ModelAndView();
mv.addObject("msg","wodiaonimade");
mv.setViewName("test");
return mv;
}
}
servletmvc-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"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 扫描包后就可以使用注解-->
<!-- <context:component-scan base-package="cn.xiong.controller"></context:component-scan>-->
<!-- <!– 让过滤器不过滤静态资源–>-->
<!-- <mvc:default-servlet-handler/>-->
<!-- <!–替代最开始的两个驱动类–>-->
<!-- <mvc:annotation-driven/>-->
<!--视图解析器:DispatcherServlet给他的ModelAndView-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="InternalResourceViewResolver">
<!--前缀-->
<property name="prefix" value="/WEB-INF/jsp/"/>
<!--后缀-->
<property name="suffix" value=".jsp"/>
</bean>
<bean id="/t" class="cn.xiong.controller.ControllerTest01"></bean>
</beans>
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/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet"</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
${msg}
</body>
</html>
缺点:一个控制器只能写一种方法,如果需要写多个方法则需要写更多的controller类
controller
@Controller
public class ControllerTest01{
//请求的地址
@RequestMapping(name = "/t")
public String test(Model model){
model.addAttribute("msg","wodiaonimade");
return "test";
}
}
servletmvc-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"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 扫描包后就可以使用注解-->
<context:component-scan base-package="cn.xiong.controller"></context:component-scan>
<!-- 让过滤器不过滤静态资源-->
<mvc:default-servlet-handler/>
<!--替代最开始的两个驱动类-->
<mvc:annotation-driven/>
<!--视图解析器:DispatcherServlet给他的ModelAndView-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="InternalResourceViewResolver">
<!--前缀-->
<property name="prefix" value="/WEB-INF/jsp/"/>
<!--后缀-->
<property name="suffix" value=".jsp"/>
</bean>
<bean id="/t" class="cn.xiong.controller.ControllerTest01"></bean>
</beans>
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/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet"</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
${msg}
</body>
</html>
注解在方法上面的访问路径
http://localhost:8080/项目名/注解名 作用:用户模式
同时注解在类和方法上面的访问路径
http://localhost:8080/项目名/加在类上面的注解名/方法上的注解名 作用:管理员模式-
访问常见种类:
post 对应mapper @PostMapping
get 对应mapper @GetMapping @RequestMapping(name="/haha/{a}/{b}",method=RequestMethod.GET)
put 对应mapper @PutMapping
delete 对应mapper @DeleteMapping
**:地址栏相同的情况下可以通过不同的提交访问方式实现不同的效果,功能就会不同
@GetMapping("/haha/{a}/{b}") /haha/1/2 提交方式为get
@PostMapping("/haha/{a}/{b}") /haha/1/2 提交方式为post
@Controller
public class ControllerTest01{
//请求的地址
@RequestMapping("/haha/{a}/{b}")
public String test(@PathVariable int a,@PathVariable int b, Model model){
int ab=a+b;
model.addAttribute("msg","wodiaonimade"+ab);
return "test";
}
}
访问路径为http://localhost:8080/项目名/注解名/?/?
@Controller
public class ControllerTest01{
//请求的地址
@RequestMapping(name="/haha/{a}/{b}",method=RequestMethod.GET)
public String test(@PathVariable int a,@PathVariable int b, Model model){
int ab=a+b;
model.addAttribute("msg","wodiaonimade"+ab);
return "test";
}
}
-------------------------------------------
简化版本
@Controller
public class ControllerTest01{
//请求的地址
@GetMapping("/haha/{a}/{b}")
public String test(@PathVariable int a,@PathVariable int b, Model model){
int ab=a+b;
model.addAttribute("msg","wodiaonimade"+ab);
return "test";
}
}
原始风格
@Controller
public class ControllerTest01{
//请求的地址
@RequestMapping("/haha")
public String test(int a,int b, Model model){
int ab=a+b;
model.addAttribute("msg","wodiaonimade"+ab);
return "test";
}
}
原始访问路径为http://localhost:8080/项目名/注解名/a=?/b=?
标签:springmvc,haha,学习,int,test,model,servlet,public 来源: https://blog.csdn.net/Dhxy1030/article/details/116562892
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。