Spring4mvc Controller Service map转json json转string

来源:互联网 发布:织梦淘宝客模板 编辑:程序博客网 时间:2024/06/08 12:10

1.web.xm配置

<listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><servlet><servlet-name>spring</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><load-on-startup>1</load-on-startup><init-param>        <param-name>contextConfigLocation</param-name>        <param-value>classpath:mvc/spring-servlet.xml</param-value>    </init-param></servlet><servlet-mapping><servlet-name>spring</servlet-name><url-pattern>*.action</url-pattern></servlet-mapping><servlet-mapping><servlet-name>spring</servlet-name><url-pattern>/</url-pattern></servlet-mapping>

init-param指定了spring-servlet文件的位置和名称,否则自动去web-inf下寻找spring-servlet.xml

监听必须配置,listener和servlet顺序不要乱

因为项目原因,在spring的url-pattern配置多增加了“/”

3.spring-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:aop="http://www.springframework.org/schema/aop"      xmlns:context="http://www.springframework.org/schema/context"      xmlns:jpa="http://www.springframework.org/schema/data/jpa"      xmlns:mvc="http://www.springframework.org/schema/mvc"      xmlns:tx="http://www.springframework.org/schema/tx"      xmlns:util="http://www.springframework.org/schema/util"      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd          http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd          http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd          http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd          http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd          http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd          http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd">    <!-- 只扫描Controller 注解 --><context:component-scan base-package="com.controller"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />     </context:component-scan>    <context:component-scan base-package="com.service" /><bean id="jspViewResolver"class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="viewClass"value="org.springframework.web.servlet.view.JstlView" /><property name="prefix" value="/WEB-INF/jsp/" /><property name="suffix" value=".jsp" /><property name="order" value="1" /></bean></beans>
项目原因,dao层有其他内容,而且本次没有用到,就没有扫描

4.LoginTestController

package com.controller;import java.util.HashMap;import java.util.Map;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;import com.service.LoginTestService;import net.sf.json.JSONObject;/** * 测试登录Controller * @author kkkder * */@Controllerpublic class LoginTestController{@Autowired  private LoginTestService userService;/** * 请求测试 * @param str1 * @param str2 * @return */@RequestMapping("/hello.action")@ResponseBodypublic String loginTest(String str1,String str2){Map<String,String> map = new HashMap<String,String>();//调用service 拼接字符串String str = userService.addMethod(str1, str2);if(null == str){map.put("status", "fail");map.put("str1", str1);map.put("str2", str2);map.put("message", "All parameters are must not null");}else{map.put("status", "success");map.put("str", str);}JSONObject  jasonObject = JSONObject.fromObject(map);return jasonObject.toString();}@RequestMapping("/")public String index(){return "index";}}
5.service

package com.service;public interface LoginTestService{public String addMethod(String str1,String str2);}
6.serviceImpl

package com.service.impl;import org.springframework.stereotype.Service;import com.service.LoginTestService;@Service("loginTestService") public class LoginTestServiceImpl implements LoginTestService{@Overridepublic String addMethod(String str1, String str2){if(null == str1|| null == str2){//不可有空字符串return null;}//字符串拼接return str1 +str2;}}
7.在web-info下新建inex.jsp

8.测试访问 / 和/hello.action

随笔,spring4mvc需要的jar包自行需找,并注意注解



原创粉丝点击