您好,欢迎来到尔游网。
搜索
您的当前位置:首页springmvc创建代码

springmvc创建代码

来源:尔游网
创建第一个简单springmvc步骤。 (一)首先配置好jdk、tomcat。 (二)导入jar包

(三)添加配置文件:springmvc.xml

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

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

http://www.springframework.org/schema/mvc

http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd\">

base-package=\"com.atguigu.springmvc\">

class=\"org.springframework.web.servlet.view.InternalResourceViewResolver\">

value=\"/WEB-INF/views/\">

class=\"org.springframework.web.servlet.view.BeanNameViewResolver\">

class=\"org.springframework.context.support.ResourceBundleMessageSource\">

(四)配置web文档

xmlns:web=\"http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd\" xsi:schemaLocation=\"http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd\" id=\"WebApp_ID\" version=\"3.0\">

SpringMVC_Demo0621a

index.html index.htm index.jsp default.html default.htm default.jsp

dispatcherServlet org.springframework.web.servlet.DispatcherServlet contextConfigLocation /WEB-INF/springmvc.xml 1

HiddenHttpMethodFilter

org.springframework.web.filter.HiddenHttpMethodFilter

HiddenHttpMethodFilter /*

dispatcherServlet /

(五)创建controller

package com.atguigu.springmvc.handler;

import org.springframework.stereotype.Controller;

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

/** *

* @author HH

* @date 2017-6-21 * @version jdk 1.7.0

* @function springmvc测试1 */

@Controller

@RequestMapping(\"/HelloWork\")

public class HelloWork { @ResponseBody @RequestMapping(value=\"/helloword\

public String hello(){

//在控制台后边打印出下边这句话,并跳转到success页面, System.out.println(\"hello,create a world!\"); return \"success\"; } @RequestMapping(\"/helloword2\") @ResponseBody

//在控制台后边打印出下边这句话,并输出success

}

public String sayHello(){ System.out.println(\"say hello to springmvc\"); return \"success\"; }

//06月22日方法

//必须使用post提交方法

Springmvc搭建框架初步(完成以上的springmvc.xml配置和web.xml配置后)

(一)springmvc通过get方法跳转。

@RequestMapping(value=\"testMethod\",method=RequestMethod.GET)

public String testMethod(){

System.out.println(\"this is 06 月22日测试\"); return \"success\"; }

在jsp中: href=\"<%=basePath %>SpringMVC6_22/testMethod\">06月22日测试

(二)通过params传参,把age判断不为10才能正常跳转

@RequestMapping(value=\"testMethod02\",params={\"username\",\"age!=10\"},method=RequestMethod.GET) public String testMethod02(){

System.out.println(\"this is 06 月22日testMethod02测试\"); return \"success\"; } 在jsp中: href=\"<%=basePath %>SpringMVC6_22/testMethod02?username=aaa&&age=12\">06月22日testparam测试

(三)在通过使用占位符。/*/表示上一层任意的字符,也可以匹配到。

/* //*匹配任何字符 ,但是必须是该路径下,requestmapping 层必须要写上才能找到*/ /* 2个*表示任意层*/

@RequestMapping(\"/*/testAntString03\") //只能是一层,上一层是任意的也可以 public String testAntString03(){

System.out.println(\"testAntString03\");

return \"success\"; } 在jsp中: href=\"<%=basePath %>SpringMVC6_22/year/testAntString03\" >通过*访问

(四)可以是任意多层。

@RequestMapping(\"/**/testAntString04\") //可以是多层任意的

public String testAntString04(){

System.out.println(\"testAntString04\"); return \"success\"; }

在jsp中:(year/fdf/fsdfd为任意多层) href=\"<%=basePath %>SpringMVC6_22/year/fdf/fsdfd/testAntString04\" >通过多层**访问

(五)通过PathVariable传参

@RequestMapping(\"/testAntString05/{id}/{name

}\")

//可以是多层任意的 public String

testAntString05(@PathVariable(\"id\") Integer id,@PathVariable(\"name\") String name){ System.out.println(\"testAntString05的id:\"+id+\+name); return \"success\"; }

在jsp中: href=\"<%=basePath %>SpringMVC6_22/testAntString05/25/popo\" >通过参数去访问

(五)通过rest,method传参

@RequestMapping(value=\"order/{id}\",method=RequestMethod.DELETE) //可以是多层任意的 public String

testAntString06Delete(@PathVariable(\"id\")Integer id){ System.out.println(\"testAntString06Delete的id:

\"+id);

return \"success\"; }

//查询用户

@RequestMapping(value=\"/order/{id}\",method=RequestMethod.GET)

//可以是多层任意的 public String

testAntString06GET(@PathVariable(\"id\")Integer id){ System.out.println(\"查询用户的id:\"+id); return \"success\"; }

//post 询无id //添加户

@RequestMapping(value=\"/order/{id}\",method=RequestMethod.PUT)

//可以是多层任意的 public String

testAntString06PUT(@PathVariable(\"id\")Integer id){ System.out.println(\"testAntString06PUT添加用户的id:\"+id);

//HiddenHttpMethodFilter? return \"success\"; }

在jsp中:


通过rest删除添加用户

SpringMVC6_22/order/22\" method=\"post\">

SpringMVC6_22/order/22\" method=\"post\">

SpringMVC6_22/order/22\" method=\"post\">

(六)通过RequestParam绑定参数。(required=false表示允许无参数)

@RequestMapping(value=\"/testAfternoon0622Test02\",method=RequestMethod.POST) public String

testAfternoon0622Test02(@RequestParam(\"username\")String

username,@RequestParam(value=\"age\",required=false)Integer age){

System.out.println(\"this is http请求处理方法签名测试!\");

System.out.println(\"username:\"+username+\",age:\"+age);

return SUCCESS; }

在jsp中:

无参数测试

action=\"<%=basePath%>SpringMVC6_22/testAfternoon0622Test02?username=kaka\" method=\"post\">


(七)通过对象绑定参数 在controller中代码:

@RequestMapping(value=\"/testAfternoon0622Test\",method=RequestMethod.POST) public String

testAfternoon0622Test(@RequestParam(value=\"username\",

defaultValue=\"play\",required=false)String

username,@RequestParam(value=\"age\",defaultValue=\"20\",required=false)Integer age){

System.out.println(\"this is http请求处理方法签名测试!\");

System.out.println(\"username:\"+username+\",age:\"+age);

return SUCCESS; }

@RequestMapping(value=\"/testPOJO\",method=RequestMethod.POST)

public String testPOJO(@ModelAttribute(\"user\")User user){

System.out.println(\"user的员工信息:\\n\"+user); return SUCCESS; } 创建对象:

package com.atguigu.springmvc.entity; public class User {

private String name;

private String password; private String email; private Integer age;

}

public String getName() { return name; }

public void setName(String name) { this.name = name; }

public String getPassword() { return password; }

public void setPassword(String password) { this.password = password; }

public String getEmail() { return email; }

public void setEmail(String email) { this.email = email; }

public Integer getAge() { return age; }

public void setAge(Integer age) { this.age = age; }

@Override

public String toString() {

return \"User [name=\" + name + \ + password + \ + email + \ + age + \"]\"; }

在jsp中:


action=\"<%=basePath%>SpringMVC6_22/testAfternoon0622Test?username=rtrd&age=43\" method=\"post\">


(八)通过嵌套对象测试练习 创建对象Address,嵌套对象MyUser

package com.atguigu.springmvc.entity; public class Address { private String line; private String city;

private String province; private MyUser myUser;

// HiddenHttpMethodFilter

public MyUser getMyUser() { return myUser; }

public void setMyUser(MyUser myUser) { this.myUser = myUser; }

public String getLine() { return line; }

public void setLine(String line) { this.line = line; }

public String getCity() { return city; }

public void setCity(String city) { this.city = city; }

public String getProvince() { return province; }

public void setProvince(String province) { this.province = province; }

@Override

public String toString() {

return \"Address [line=\" + line + \ + city + \province=\"

+ province + \ + myUser + \"]\"; } }

@RequestMapping(value=\"/testPOJOnoObject\",method=RequestMethod.POST)

public String testPOJOnoObject(Address address){ //本类的不需要加上对象名,而如果是其他类的需要写上对象名字

System.out.println(\"user的员工信息:\\n\"+address); System.out.println(\"address.getMyUser().getName():\"+address.getMyUser().getName()); return SUCCESS; } 在jsp中:

POJO测试练习没有对象的

SpringMVC6_22/testPOJOnoObject\" method=\"POST\">

this is test province:

Username:
password:
email:
age:
line:
city:

province:


(九)writer对象使用练习测试

@RequestMapping(\"/testServletAPI\")

public void testServletAPI(HttpServletRequest request,HttpServletResponse response ,Writer writer) throws IOException{ System.out.println(response.toString()); writer.write(\"hello world\"); }

在jsp中:


testServletAPI测试练习

href=\"<%=basePath %>SpringMVC6_22/testServletAPI\">testServletAPI测试

(十)springmvc的Map对象使用 @RequestMapping(\"/testMapTest\") public String testMapTest(User

user,Map map){

map.put(\"time\", new Date()); map.put(\"user\", user); System.out.println(user); // user.setEmail(\"abc@qq.com\"); return \"result\";//跳转这个页面 } 在jsp中

测试requestSCOPE的map测试练习

SpringMVC6_22/testMapTest\" method=\"POST\">

this is test province:

Username:
password:
email:
age:

跳转到了result.jsp页面,因此,根据配置文档,创建页面。

result.jsp页面如下:

<%@ page language=\"java\" import=\"java.util.*\" pageEncoding=\"utf-8\"%> <%

String path = request.getContextPath(); String basePath =

request.getScheme()+\"://\"+request.getServerName()+\":\"+request.getServerPort()+path+\"/\"; %>

\">

My JSP 'result.jsp' starting page

content=\"keyword1,keyword2,keyword3\">

success!

email:${requestScope.user.email}

time:${requestScope.time}

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- axer.cn 版权所有 湘ICP备2023022495号-12

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务